access:vba代码无法编译

时间:2013-10-09 16:09:47

标签: vba ms-access access-vba

我的访问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

最终突出显示以下行Public Function GetFolderByName(strFolderName As String,Optional objFolder As Outlook.MAPIFolder,Optional intFolderCount)As MAPIFolder

1 个答案:

答案 0 :(得分:3)

由于编译器抱怨你的函数声明,我只是将它复制到Access标准模块,如下所示:

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 Editor的主菜单运行Debug-> Compile。如果编译器抱怨其他任何东西,请修复它并再次编译。根据需要重复,直到您不再收到编译器投诉。