将MailItem传递给函数

时间:2017-03-20 04:09:01

标签: vba outlook outlook-vba

我正在搜索如何通过函数传递MailItem。

例如:我想调用一个提取MailItem主题的函数。

Function testpassing(myMail As Outlook.MailItem) As Actions
    MsgBox (myMail.Subject)
End Function

Sub passing()
    Dim myItem As Outlook.MailItem
    Set myItem = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Items(1)

    MsgBox (myItem.Subject) '<~ Work correctly
    testpassing (Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Items(1))   '<~ Run-time error 438 Object doesn't support this property or method
    testpassing (myItem)      '<~ Run-time error 438 Object doesn't support this property or method
End Sub

我是VBA展望新手。请帮忙。谢谢:))

2 个答案:

答案 0 :(得分:2)

将其称为testpassing myItemtestpassing Application.GetNamespace("MAPI").GetDefaultFolder(olFolderIn‌​box).Items(1)

还会查看 ByRef and ByVal

您可以通过引用或值将参数传递给过程(函数或子)。

一如既往的快乐编码: - )

答案 1 :(得分:1)

该文件夹可能包含不同的项目类型。我建议先检查项目类型(或邮件类别):

 If Item(1).Class = OlObjectClass.olMail Then 
    '
 End If

有关详细信息,请参阅How to: Programmatically Determine the Current Outlook Item