Outlook 2007 VBA地址列表

Outlook 2007 VBA地址列表,vba,outlook,outlook-2007,Vba,Outlook,Outlook 2007,我正在尝试根据某个特定地址是否在出站邮件的“收件人”或“抄送”字段中设置出站电子邮件的回复地址。我走了这么远,却在“Set myCounter…”行中偶然发现了“需要对象”错误。如有任何协助,将不胜感激: Option Explicit Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean) Dim oMyItem As Outlook.MailItem Dim i As Integer Dim Add

我正在尝试根据某个特定地址是否在出站邮件的“收件人”或“抄送”字段中设置出站电子邮件的回复地址。我走了这么远,却在“Set myCounter…”行中偶然发现了“需要对象”错误。如有任何协助,将不胜感激:

Option Explicit

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
Dim oMyItem As Outlook.MailItem
Dim i As Integer
Dim AddressEntry As AddressEntry
Dim myCounter As Integer
Set oMyItem = Item
Set myCounter = oMyItem.Recipients.Count

For i = 1 To myCounter
    Set AddressEntry = oMyItem.Recipients(i).AddressEntry
    If (AddressEntry = "someuser@someaddress") Then
        oMyItem.ReplyRecipients.Add "replytouser@someaddress"
    End If
Next i
End Sub
你的错误是错误的

Set myCounter = oMyItem.Recipients.Count
因为VB使用
Set
分配一个对象(一个类),而您得到的是一个整数
所以你可以把它改成

Dim myCounter As Integer

myCounter = oMyItem.Recipients.Count

myCounter
已声明为整数,因此不需要设置

替换

Set myCounter = oMyItem.Recipients.Count 


您是否(通过调试器)检查了
myItem
myItem.Recipients
都不是空的(无)?是的,我实际上已经克服了这一点(就在您的回复出现时),现在有一个单独的问题,我可以很容易地解决。对于那些感兴趣的人,我删除了声明myCounter的行,并将For循环更改为:For I=1到myItem.Recipients.Count
myCounter = oMyItem.Recipients.Count