Vba 返回收件人的电子邮件地址

Vba 返回收件人的电子邮件地址,vba,outlook,Vba,Outlook,我正在构建一个宏,当我移动到电子邮件正文中检查收件人的电子邮件地址时,该宏将自动运行 我无法获取要加载到变量中的收件人地址 Sub BuildTable() Dim myItem As Outlook.MailItem Dim myRecipient As String Set myItem = Application.CreateItem(olMailItem) Set myRecipient = myItem.Recipient.Address .... 我不确定您使用的Outlook版本

我正在构建一个宏,当我移动到电子邮件正文中检查收件人的电子邮件地址时,该宏将自动运行

我无法获取要加载到变量中的收件人地址

Sub BuildTable()
Dim myItem As Outlook.MailItem
Dim myRecipient As String
Set myItem = Application.CreateItem(olMailItem)
Set myRecipient = myItem.Recipient.Address
....

我不确定您使用的Outlook版本,但根据Microsoft()的规定,您需要使用.Recipients(索引)来获取收件人。从那里你可以得到地址。我还提到了附加到.Recipients的某种ResolveAll方法,尽管它引用了Outlook2000(eww)

试着做

Dim myItem As Outlook.MailItem
Dim myRecipient as String
Set myItem = Application.CreateItem(olMailItem)
Set myRecipient = myItem.Recipients.Item(0).Address
这将为您提供第一个收件人的地址(注意,我不记得VBA是否从索引0或1开始,如果您获得IndexOutfrange,请更改为1)。如果你需要其他人,你需要做一个循环。大概是这样的:

For Each Recipient in myItem.Recipients
// do some stuff here
Next Recipient

希望这有帮助。

您似乎正在MS Outlook和活动检查器中运行,因此:

Sub CheckAddresses()
Dim oEmail As Outlook.MailItem
Dim r As Recipient
Dim rList As Recipients

Set oEmail = Application.ActiveInspector.CurrentItem

Set rList = oEmail.Recipients
rList.ResolveAll
For Each r In rList
    Debug.Print r.Address
Next
End Sub

这段代码最终是这样的:

Sub BuildTable1()

Dim oEmail As Outlook.MailItem

Set oEmail = Application.ActiveInspector.currentItem

Set xlApp = CreateObject("Excel.Application")
xlApp.Application.Visible = True
xlApp.workbooks.Open FileName:= file location

xlApp.WorkSheets("Contacts").Activate
xlApp.Range("A6").Value = oEmail.To


//filtering by value, copying, pasting, etc.
End Sub

-ZL

感谢您的快速回复。我用的是2010年。我认为让我感到不快的是,他们在指示我创建一个新项目,而不是简单地访问mailitem中的值。我现在不知道如何获取这些值(对不起,我只涉猎了Coldfusion/SQL Server)。我感谢你的帮助-ZLI不使用Outlook for VBA,只使用Excel。等一下,我会帮你想办法的。非常感谢你的帮助,我成功了。谢谢你的时间和关注,也非常感谢。我也使用了这种方法,稍微调整了一下用途。