使用Excel VBA获取发件人的电子邮件地址

时间:2016-01-21 11:05:41

标签: excel vba excel-vba email outlook

我使用以下代码拉出主题,收到日期和发件人姓名:

Set InboxSelect = GetObject("", "Outlook.Application").GetNamespace("MAPI").PickFolder
i = 0: EmailCount = 0
EmailCount = InboxSelect.Items.Count
While i < EmailCount
    i = i + 1
    blastRow = Cells(Rows.Count, 1).End(xlUp).Offset(1).Row
    LastRow = Sheets("Body").Cells(Rows.Count, 1).End(xlUp).Offset(1).Row
    With InboxSelect.Items(i)
        MsgBox (SenderEmailAddress)
        'If .senderemailaddress = "*@somethingSpecific.co.uk" Then
            'EmailCount = EmailCount + 1
            Sheets("Import Data").Range("A" & blastRow).Formula = .SenderName
            Sheets("Import Data").Range("B" & blastRow).Formula = Format(.ReceivedTime, "dd/mm/yyyy")
            Sheets("Import Data").Range("C" & blastRow).Formula = .Subject
            Sheets("Body").Range("A" & LastRow).Formula = .Body
        'End If
    End With
Wend

我现在想要实现的是if语句,如果发件人的电子邮件地址是&#39; anything@somethingSpecific.co.uk'然后执行该代码。 我已经尝试了SenderEmailAddress,但在消息框中测试时它会返回空白。

编辑:/O=*SET1*/OU=FIRST ADMINISTRATIVE GROUP/CN=RECIPIENTS/CN=*VARIABLE1*现在每次都使用以下代码在即时窗口中返回:

Set InboxSelect = GetObject("", "Outlook.Application").GetNamespace("MAPI").PickFolder
i = 0: EmailCount = 0
EmailCount = InboxSelect.Items.Count
While i < EmailCount
    For Each Item In InboxSelect.Items
        Debug.Print Item.senderemailaddress
        If Item.senderemailaddress = "/O=SET1/OU=FIRST ADMINISTRATIVE GROUP/CN=RECIPIENTS/CN=*" Then
            i = i + 1
            blastRow = Cells(Rows.Count, 1).End(xlUp).Offset(1).Row
            LastRow = Sheets("Body").Cells(Rows.Count, 1).End(xlUp).Offset(1).Row
            With InboxSelect.Items(i)
                    Sheets("Import Data").Range("A" & blastRow).Formula = .SenderName
                    Sheets("Import Data").Range("B" & blastRow).Formula = Format(.ReceivedTime, "dd/mm/yyyy")
                    Sheets("Import Data").Range("C" & blastRow).Formula = .Subject
                    'PASTING BODY IS SLOW
                    Sheets("Body").Range("A" & LastRow).Formula = .Body
                'End If
            End With
        End If
    Next Item
Wend

我尝试做的是使用通配符(*)作为返回消息中的变体但是没有用,是否有更好的方法可以做到这一点?

5 个答案:

答案 0 :(得分:2)

使用SenderEmailAddress属性时的示例将根据需要返回电子邮件字符串。

Dim outlookApp As outlook.Application, oOutlook As Object
Dim oInbox As outlook.Folder, oMail As outlook.MailItem

Set outlookApp = New outlook.Application
Set oOutlook = outlookApp.GetNamespace("MAPI")
Set oInbox = oOutlook.GetDefaultFolder(olFolderInbox)

For Each oMail In oInbox.Items
    Debug.Print oMail.SenderEmailAddress
Next oMail

修改

问题是.SenderEmailAddress属性返回EX地址,而我们想要SMTP地址。对于任何内部电子邮件地址,它将返回EX类型地址。

要从内部电子邮件中获取SMTP地址,您可以使用以下内容。

Dim outlookApp As Outlook.Application, oOutlook As Object
Dim oInbox As Outlook.Folder, oMail As Outlook.MailItem

Dim strAddress As String, strEntryId As String, getSmtpMailAddress As String
Dim objAddressentry As Outlook.AddressEntry, objExchangeUser As Outlook.ExchangeUser
Dim objReply As Outlook.MailItem, objRecipient As Outlook.Recipient

Set outlookApp = New Outlook.Application
Set oOutlook = outlookApp.GetNamespace("MAPI")
Set oInbox = oOutlook.GetDefaultFolder(olFolderInbox)

For Each oMail In oInbox.Items
    If oMail.SenderEmailType = "SMTP" Then

        strAddress = oMail.SenderEmailAddress

    Else

        Set objReply = oMail.Reply()
        Set objRecipient = objReply.Recipients.Item(1)

        strEntryId = objRecipient.EntryID

        objReply.Close OlInspectorClose.olDiscard

        strEntryId = objRecipient.EntryID

        Set objAddressentry = oOutlook.GetAddressEntryFromID(strEntryId)
        Set objExchangeUser = objAddressentry.GetExchangeUser()

        strAddress = objExchangeUser.PrimarySmtpAddress()

    End If

    getSmtpMailAddress = strAddress
    Debug.Print getSmtpMailAddress

Next oMail

如果电子邮件已经SMTP,它将只使用.SenderEmailAddress属性返回地址。如果电子邮件为EX,则会使用SMTP方法找到.GetAddressEntryFromID()地址。

以上是我在this answer上找到的修改后的代码。 Here也是如何在C#中执行此操作的链接。

答案 1 :(得分:1)

在大多数情况下,发件人的SMTP地址将在单独的属性中提供,您可以使用ExchangeUser.PrimarySmtpAddress访问它 - 使用OutlookSpy查看现有邮件(单击IMessage按钮)。 / p>

否则您可以使用on error resume next 'PropertyAccessor can raise an exception if a property is not found if item.SenderEmailType = "SMTP" Then strAddress = item.SenderEmailAddress Else 'read PidTagSenderSmtpAddress strAddress = item.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x5D01001F") if Len(strAddress) = 0 Then set objSender = item.Sender if not (objSender Is Nothing) Then 'read PR_SMTP_ADDRESS_W strAddress = objSender.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x39FE001F") if Len(strAddress) = 0 Then 'last resort set exUser = objSender.GetExchangeUser if not (exUser Is Nothing) Then strAddress = exUser.PrimarySmtpAddress End If End If End If En If End If

脱离我的头顶:

.js

答案 2 :(得分:0)

你不能只使用发送键强制&#34;控制+ k&#34;在展望?似乎这样可以解决您的问题,并可能使代码变得简单。

尝试将其添加到某处?

 Application.SendKeys("^k")       'i believe this is correct syntax, never used this yet but i think it works

答案 3 :(得分:0)

我最终做了 varTest = Item.senderemailaddress If InStr(varTest, "BE WISER INSURANCE") > 0 Then 检测到不在我不想要的任何电子邮件中的设置部分。非常感谢你的帮助,@ Iturner!

答案 4 :(得分:0)

Public Function GetSenderAddrStr(objMail As Outlook.MailItem) As String
 If objMail.SenderEmailType = "SMTP" Then
        GetSenderAddrStr = objMail.SenderEmailAddress
 Else
        GetSenderAddrStr = objMail.Sender.GetExchangeUser().PrimarySmtpAddress
 End If
End Function