我正在构建一个宏,当我进入电子邮件正文以检查收件人的电子邮件地址时,该宏将自动运行。
我无法将收件人的地址加载到变量中。
Sub BuildTable()
Dim myItem As Outlook.MailItem
Dim myRecipient As String
Set myItem = Application.CreateItem(olMailItem)
Set myRecipient = myItem.Recipient.Address
....
我不确定您使用的是什么版本的 Outlook,但根据 Microsoft(http://msdn.microsoft.com/en-us/library/office/aa211006 (v=office.11).aspx ),您需要使用 .Recipients(Index) 获取收件人。从那里你也许可以得到地址。我还提到了附加到.Recipients 的某种ResolveAll 方法,尽管它引用了Outlook 2000 (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 开始,如果您获得 IndexOutOfRange,请更改为 1)。如果你需要其他人,你将需要做一个循环。像这样的东西:
For Each Recipient in myItem.Recipients
// do some stuff here
Next Recipient
希望这可以帮助。
您似乎在 MS Outlook 和 Active Inspector 中运行,所以也许:
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