2012-04-10 39 views
-1

我试图根据出站邮件的“收件人”或“抄送”字段中是否包含特定地址来设置出站电子邮件的回复地址。我已经得到了这么多,只是在“Set myCounter ...”行中偶然发现“Object required”错误。任何援助将不胜感激:Outlook 2007 VBA地址列表

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 = "[email protected]") Then 
     oMyItem.ReplyRecipients.Add "[email protected]" 
    End If 
Next i 
End Sub 
+0

你检查(使用调试器),这两个'oMyItem'和'oMyItem.Recipients'不为空(什么)? – Marco 2012-04-10 14:47:36

+0

是的,我实际上已经过去了(当你的答案出现时),现在有一个单独的问题,我可以很容易地解决。对于那些感兴趣的,我删除了声明myCounter的行,并将for循环更改为:For i = 1 To oMyItem.Recipients.Count – dmolavi 2012-04-10 14:52:44

回答

0

你的错误是在

Set myCounter = oMyItem.Recipients.Count 

因为VB使用Set分配对象(类),而你得到的整数!
所以,你可以把它变成

Dim myCounter As Integer 

myCounter = oMyItem.Recipients.Count 
0

myCounter已被宣布为一个整数,所以没有必要为Set

更换

Set myCounter = oMyItem.Recipients.Count 

myCounter = oMyItem.Recipients.Count