2013-10-09 122 views
-2

我的訪問vba代碼已損壞,當我嘗試complile代碼,我得到編譯錯誤:用戶定義類型未定義。能否請你幫我,如果有任何事情出了毛病代碼訪問:vba代碼無法編譯

Option Compare Database 



Public Function GetFolderByName(strFolderName As String, Optional objFolder As   Outlook.MAPIFolder, Optional intFolderCount) As MAPIFolder 


Dim objApp As Outlook.Application 
Dim objNS As Outlook.Namespace 
Dim colStores As Outlook.Folders 
Dim objStore As Outlook.MAPIFolder 
Dim colFolders As Outlook.Folders 
Dim objResult As Outlook.MAPIFolder 
Dim I As Long 

On Error Resume Next 
Set objApp = CreateObject("Outlook.Application") 
Set objNS = objApp.GetNamespace("MAPI") 
Set colStores = objNS.Folders 

If objFolder Is Nothing Then 
'If objFolder is not passed, assume this is the initial call and cycle through stores 
    intFolderCount = 0 
For Each objStore In colStores 
    Set objResult = GetFolderByName(strFolderName, objStore, intFolderCount) 
    If Not objResult Is Nothing Then Set GetFolderByName = objResult 
Next 
Else 
'Test to see if this folder's name matches the search criteria 
If objFolder.Name = strFolderName Then 
    Set GetFolderByName = objFolder 
    intFolderCount = intFolderCount + 1 
End If 
Set colFolders = objFolder.Folders 
'Cycle through the sub folders with recursive calls to this function 
For Each objFolder In colFolders 
    Set objResult = GetFolderByName(strFolderName, objFolder, intFolderCount) 
    If Not objResult Is Nothing Then Set GetFolderByName = objResult 
Next 
End If 
'If two or more folders exist with the same name, set the function to Nothing 
If intFolderCount > 1 Then Set GetFolderByName = Nothing 

Set objResult = Nothing 
Set colFolders = Nothing 
Set objNS = Nothing 
Set objApp = Nothing 
End Function 

它結束了突出以下行公共功能GetFolderByName(strFolderName作爲字符串,可選objFolder作爲Outlook.MAPIFolder,可選intFolderCount)作爲MAPIFolder

+1

沒有看到代碼,這幾乎是不可能的。 「你能修好我的代碼嗎?我不會費心去展示給你,但告訴我它有什麼問題。」 –

+0

編譯器強調它認爲是「未定義」的用戶定義類型。它突出什麼? – HansUp

+2

添加對Outlook運行時庫的引用 – enderland

回答

3

由於編譯器抱怨你的函數的聲明,我只是複製了一個准入標準模塊,像這樣:

Public Function GetFolderByName(strFolderName As String, _ 
    Optional objFolder As Outlook.MAPIFolder, _ 
    Optional intFolderCount) As MAPIFolder 

End Function 

這給了我你報一樣的編譯錯誤。當我加入到爲@enderland建議Outlook對象庫的引用,它編譯沒有錯誤:

enter image description here

這同樣的變化可能治癒立即解決問題。但是,您還應該確保沒有其他未發現的問題等着咬你。添加Option Explicit到您的模塊的聲明節:

Option Compare Database 
Option Explicit 

然後運行調試 - >從VB編輯器的主菜單編譯。如果編譯器抱怨別的,修復並重新編譯。根據需要重複,直到你沒有更多的編譯器投訴。

+0

這比我的簡單評論,這是肯定多漂亮:) – enderland

+0

@enderland謝謝。我對這件事偷竊你的雷聲很敏感。如果你更願意自己提交答案,我會很高興地問codemacha接受你的答案,而不是這個答案。 – HansUp

+0

不用擔心。 – enderland