2013-10-09 123 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