检查收件人和转发邮件的条件

时间:2018-05-08 07:47:41

标签: vba outlook-vba

我试图写条件,并根据哪个收件人有电子邮件转发给另一封电子邮件。在网上搜索后我发现了以下内容,但它不适合我:

Public Sub ConvertMeetingToEmail(ActiveFolder, Inbox As String)

    Dim myNamespace As Outlook.NameSpace
    Dim myFolder As Outlook.Folder
    Dim Subfolder As Outlook.Folder

    Dim Item As Object
    Dim myMtg As Outlook.MeetingItem

    Dim objMsg As MailItem
    Set objMsg = Application.CreateItem(olMailItem)

    Set myNamespace = Application.GetNamespace("MAPI")
    Set myFolder = myNamespace.Folders(ActiveFolder)
    Set Folders = myFolder.Folders
    Set Subfolder = Folders.Item(Inbox)

    For Each Item In Subfolder.Items

        If Item.MessageClass = "IPM.Schedule.Meeting.Request" Then
            Set Msg = Item
            Set recips = Msg.Recipients
            For Each recip In recips
                If (recip = "example@mail.domail" _
                    Or recip = "example1@mail.domail" _
                    Or recip = "example2@mail.domail" _
                    Or recip = "example3@mail.domail" _
                    Or recip = "example4@mail.domail") Then

                        objMsg.To = "example6@mail.domail"
                        objMsg.CC = recip
                        objMsg.Subject = Msg.Subject
                        objMsg.Body = Msg.Body
                        objMsg.Send
                Endif
            Next
        End If

    Next

End Sub

1 个答案:

答案 0 :(得分:0)

Recipients集合返回Recipient对象,而不是字符串,因此您的代码应该检查recip.Address属性。使用不区分大小写的字符串比较也是一个好主意:

 If ((StrComp(recip.Address, "example@mail.domail", 1) = 0) _
...