ByRef参数类型MIsmatch(编译错误) [英] ByRef Argument Type MIsmatch (Compile Error)
问题描述
我完全搞不清楚为什么在VBA模块编译过程中遇到ByRef Argument Type Mismatch错误ThisOutlookSession
I am completely confused why I am getting a ByRef Argument Type Mismatch error during compile in VBA module ThisOutlookSession
我有一个Sub(曾经是一个函数我可以没有工作)有两个参数。 Sub对所有顶级文件夹(代表电子邮件帐户/地址的文件夹)的所有文件夹进行递归搜索,搜索名称为
且已知的文件夹。 Sub将文件夹作为对象返回到其中一个参数中。 (请注意,这是作为一个函数尝试的,但是通过返回对象来使其工作的努力遇到了错误。网上的另一位评论者建议将其创建为Sub,其中
返回值放在参数中;那是有效的)
I have a Sub (which used to be a function I could not get to work) which has two parameters. The Sub does a recursive search of all folders for all top level folders (the folders representing the email accounts/addresses) for a folder whose name is searched and known. The Sub returns the folder as an object in one of the parameters. (Note this was tried as a function, but efforts to get it to work by returning the object were met with errors. Another commenter on the web suggested creating it as a Sub with the return value put in one on the arguments; that worked)
Arg 1接受一个字符串,该字符串代表位于"帐户"内的任何文件夹的名称。 (表示电子邮件地址的顶级文件夹)。
Arg 1 takes a string which represents the name of any folder located within an "account" (top level folder representing email address).
未设置Arg 2但是返回Folder对象的参数
Arg 2 is not set but is the parameter that returns the Folder object
下面的代码提取显示4个子。第一个Sub(准备好时将是Application_Startup)是产生"类型不匹配"的调用子。 GetFolderFromName调用的第二个参数中的错误。第二个Sub是另一个程序
,它可以进行调用并且实际上有效,并且实际上用于使GetFolderFromName正常工作。 第3个Sub是GetFolderFromName过程本身:它立即调用执行所有工作的过程,以在深度递归
搜索中找到该文件夹,并将其作为对象返回。
The code extract below shows 4 Subs. The first Sub (which will be Application_Startup when ready) is the calling sub that produces the "type mismatch" error in the 2nd argument of the call to GetFolderFromName. The 2nd Sub is another procedure that makes the call and actually works and was actually used to get the GetFolderFromName working. The 3rd Sub is the GetFolderFromName procedure itself: it immediately calls the procedure that does all the work to find the folder in a deep recursive search and return it as an object.
问题是为什么第一个sub在第二个sub没有时产生编译器错误,尽管所有声明似乎都是有序的!局部变量声明相同且正确。发生了什么?
The question is why the 1st sub produces the compiler error when the 2nd sub did not, despite all declarations seeming to be in order! The local variables are declared the same, and correctly. What is happening?
' The calling sub with the compile error
Sub TestStartCode()
Dim mainAccount, reportFolder As folder
Dim response As Integer
mainAccount = Nothing
' check for top level folder, exit with message if not present
Call GetFolderFromName(INCOMING_CORRESPONDENCE_ACCOUNT, mainAccount)
If mainAccount Is Nothing Then
MsgBox ("Account '" & INCOMING_CORRESPONDENCE_ACCOUNT & "' designated " & vbCrLf & _
"for incoming correspondence not found on this Outlook installation" & vbCrLf & vbCrLf & _
"Make sure you have this account installed to make use of the system")
Exit Sub
End If
End Sub
' Working test code that makes same call without a compile type mismatch error
Sub TestFindFolderCode()
Dim myFolderName As String
Dim resultFolder As folder
Let myFolderName = "Drafts"
Call GetFolderFromName(myFolderName, resultFolder)
If resultFolder Is Nothing Then
Debug.Print "not found"
Else
Debug.Print "found!"
Debug.Print " Entry ID: " & resultFolder.EntryID
Debug.Print " Description: " & resultFolder.Description
Debug.Print " Folder Path: " & resultFolder.FolderPath
Debug.Print " Web View URL: " & resultFolder.WebViewURL
Debug.Print "Default Message Class: " & resultFolder.DefaultMessageClass
End If
End Sub
' The called Sub, showing its declaration and working code
Private Sub GetFolderFromName(targetFolderName As String, ByRef foundFolder As folder)
Call ReadFoldersForTarget(targetFolderName, Application.GetNamespace("MAPI").Folders, foundFolder, -1)
End Sub
' The workhorse procedure that does the deep recursive search called by the wrapper Sub
' sub doubles as folder hiearchy lister if level is set to 0 on the call
Private Sub ReadFoldersForTarget(targetFolderName As String, _
foldersCollection As Folders, ByRef foundTargetFolder As folder, ByRef level As Integer)
Dim folder As folder
Dim i As Integer
Dim margin As String
Dim searching As Boolean
If level < 0 Then
searching = True
Else
searching = False
End If
If searching = True Then
For i = 0 To level
margin = margin & " "
Next i
Debug.Print margin & "|"
End If
For Each folder In foldersCollection
If searching = True Then
Debug.Print margin & "+--- " & folder.Name
'& (EntryID: " & folder.EntryID & "), (StoreID: " & folder.StoreID & ")"
Else
Debug.Print "folder.name: " & folder.Name
If InStr(folder.Name, targetFolderName) > 0 Then
Set foundTargetFolder = folder
Exit Sub
End If
End If
If folder.Folders.Count > 0 Then
Call ReadFoldersForTarget(targetFolderName, folder.Folders, foundTargetFolder, level + 1)
If Not foundTargetFolder Is Nothing Then
Exit Sub
End If
End If
Next folder
Set foundTargetFolder = Nothing
End Sub
推荐答案
问题可能是VBA不区分大小写,但.Net是 < span style ="">区分大小写。如果那是问题那么"文件夹"。与"文件夹"的类型不同。
Perhaps the problem is that VBA is not case-sensitive but .Net is case-sensitive. If that is the problem then "Folder" is not the same type as "folder".
这篇关于ByRef参数类型MIsmatch(编译错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!