我正在使用Outlook 2010,我正在尝试根据附件名称将电子邮件转发给特定用户。下面的代码有效,除了我必须在Outlook中为每个不同的附件名称创建一个规则(即使在交换控制台中将规则大小增加到256kb后,我的规则最终也会耗尽存储空间)。
我希望这个脚本在一个规则下运行。如果附件名称为“xxxxx.pdf”,则需要发送到“name@domain.com”,如果没有则跳过,如果附件名称是“xx.xls”则发送到“ name1@domain.com“如果没有,那么跳过......等等......
以下是我目前的情况,我希望这一切能够合并。
Outlook会话中粘贴的代码:
Sub SendOnMessage23(olItem As MailItem)
Dim olAtt As Attachment, yest As Boolean, ol_newmail As MailItem
For Each olAtt In olItem.Attachments
If InStr(1, olAtt.FileName, "Print_Ack_Rpt.rpt_1100023-1") > 0 Then yest = True: Exit For
NextIf yest = True Then
Set ol_newmail = olItem.Forward
ol_newmail.To = "user@domain.com"
ol_newmail.Send
End If
End Sub
Sub SendOnMessage23(olItem As MailItem)
Dim olAtt As Attachment, yest As Boolean, ol_newmail As MailItem
For Each olAtt In olItem.Attachments
If InStr(1, olAtt.FileName, "Print_Ack_Rpt.rpt_1100024-1") > 0 Then yest = True: Exit For
NextIf yest = True Then
Set ol_newmail = olItem.Forward
ol_newmail.To = "user1@domain.com"
ol_newmail.Send
End If
End Sub
Sub SendOnMessage23(olItem As MailItem)
Dim olAtt As Attachment, yest As Boolean, ol_newmail As MailItem
For Each olAtt In olItem.Attachments
If InStr(1, olAtt.FileName, "Print_Ack_Rpt.rpt_1100024-1") > 0 Then yest = True: Exit For
NextIf yest = True Then
Set ol_newmail = olItem.Forward
ol_newmail.To = "user2@domain.com"
ol_newmail.Send
End If
End Sub
Sub SendOnMessage23(olItem As MailItem)
Dim olAtt As Attachment, yest As Boolean, ol_newmail As MailItem
For Each olAtt In olItem.Attachments
If InStr(1, olAtt.FileName, "Print_Ack_Rpt.rpt_1100024-1") > 0 Then yest = True: Exit For
NextIf yest = True Then
Set ol_newmail = olItem.Forward
ol_newmail.To = "user3@domain.com"
ol_newmail.Send
End If
End Sub
答案 0 :(得分:0)
我相信您正在考虑使用ElseIf
条件。您似乎没有按照预期正确使用If
条件。
对您当前潜在客户的建议
在If
语句中,当参数满足时,其下方和EndIf
条件之前的代码将运行。因此,不是将yest
变量设置为True
并退出循环,而只需在If
语句下方插入参数为true时插入要运行的代码。如果条件为真,则电子邮件将转发,如果不是,则代码将移至下一个附件以查看它是否满足您的If
声明。
对于您的第一个子代码,代码看起来像这样:
Sub SendOnMessage23(olItem As MailItem)
Dim olAtt As Attachment, ol_newmail As MailItem
For Each olAtt In olItem.Attachments
If InStr(1, olAtt.FileName, "Print_Ack_Rpt.rpt_1100023-1") > 0 Then
Set ol_newmail = olItem.Forward
ol_newmail.To = "user@domain.com"
ol_newmail.Send
Exit Sub
End If
Next
End Sub
我发现一个令人满意的附件之后我还添加了一个Exit
子句,这样如果由于某种原因你有两个符合条件的附件,它就不会发送多封电子邮件。
将所有内容合并为一个子
现在我们已经有了If
语句,我们可以使用ElseIf
开始将您的潜艇组合成一个。基本上ElseIf
的工作原则是,当在它之前测试的条件为假时,它会在测试其条件时得到一个镜头。因此,在这种情况下,当您的If
语句不满足时,下面的ElseIf
会检查其条件是否满足。当它执行它的代码时,当它不是时代码进入下一行。您可以根据需要添加尽可能多的ElseIf
,并按顺序检查其条件。我们将利用这一点将所有潜艇组合成一个如此:
Sub SendOnMessage23(olItem As MailItem)
Dim olAtt As Attachment, ol_newmail As MailItem
For Each olAtt In olItem.Attachments
If InStr(1, olAtt.FileName, "Print_Ack_Rpt.rpt_1100023-1") > 0 Then
Set ol_newmail = olItem.Forward
ol_newmail.To = "user@domain.com"
ol_newmail.Send
Exit Sub
ElseIf InStr(1, olAtt.FileName, "Print_Ack_Rpt.rpt_1100024-1") > 0 Then
Set ol_newmail = olItem.Forward
ol_newmail.To = "user1@domain.com"
ol_newmail.Send
Exit Sub
'... keep adding your other subs following the ElseIf format above
End If
Next
End Sub
我没有在Outlook中测试过上面的任何代码,但是如果你的代码工作正常,那么它也应该可行。如果没有,请告诉我们,我们可以解决任何问题。