2017-12-18 368 views
0

我有一段简短的代码,在我发送电子邮件时运行。它会查看收件人地址和主题以查看它是否包含某些单词,然后弹出消息框提醒我们更新绘图版本控制。它适用于内部电子邮件地址,似乎在某些外部电子邮件地址上工作,但出于某种原因,它不喜欢我实际需要它留意的电子邮件地址。Outlook VBA代码不适用于所有电子邮件地址

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean) 
Dim hismail As String 
Dim strSubject As String 
strSubject = Item.Subject 

Dim olObj As MailItem 


Set olObj = Application.ActiveInspector.CurrentItem 
hismail = olObj.Recipients.Item(1).AddressEntry.GetExchangeUser.PrimarySmtpAddress 
Set olObj = Nothing 

If hismail = "[email protected]" And strSubject Like "*update*" Or strSubject Like "*revision*" Then 


    MsgBox "Dont forget to update Drawing PDF`s if necessary", vbExclamation, "Have you updated the PDF`s?" 


End If 
End Sub 

我已将地址更改为帖子,但其格式和长度相同。如果任何人有任何想法,我真的很感激它,如我们的供应商谁拥有一个邮箱充满测试电子邮件和垃圾图片。

感谢

+1

你能澄清一下究竟发生了什么吗?它只是不认识电子邮件,还是它给你一个错误?你有没有试过测试你的'hisemail',以确保它获得了你从他的电子邮件期望的地址?我会建议编写一个简单的脚本来专门打印他的电子邮件,以便您能够看到代码所看到的内容。 –

+1

此外,只是一个想法,他的电子邮件可能不在Exchange服务器内,因此您无法以这种方式获得他的'PrimarySmtpAddress'。这可能就是你的大部分内部电子邮件和一些外部电子邮件正在工作的原因。请尝试访问“到”字段。或者看看你是否可以从另一个房产获得他的电子邮件。 –

+0

嗨,对不起,没有错误消息。电子邮件只是发送出去,显示消息框。我只是尝试将hismail发送到消息框。它在我的电子邮件地址上正常工作,并返回了正确的地址,但我试图发送的地址想出了一个调试框,指出“运行时错误91:对象变量或块变量未设置”adn debug突出显示此行hismail = olObj.Recipients.Item(1).AddressEntry.GetExchangeUser.PrimarySmtpAddress – mike

回答

0

后挖我的一点点找到了解决办法,应该让你指出正确的方向。这是基于怀疑您的问题是由于您的目标用户在您的组织的Exchange服务器中不可用导致的。这个解决方案应该解决这个问题,但如果它不,它至少会让你知道下一步的位置。

首先,我把代码示例从这个MSDN文章(https://msdn.microsoft.com/en-us/VBA/Outlook-VBA/articles/obtain-the-e-mail-address-of-a-recipient),并修改它,让它返回地址用户和他们的电子邮件的数组:

Private Const PR_SMTP_ADDRESS As String = "http://schemas.microsoft.com/mapi/proptag/0x39FE001E" 

Private Function GetSMTPAddressesForRecipients(ByVal MailItem As Outlook.MailItem) As Variant 
    Dim Recipients As Outlook.Recipients 
    Set Recipients = MailItem.Recipients 

    Dim Addresses As Variant 
    ReDim Addresses(0 To Recipients.Count - 1, 0 To 1) 

    Dim Accessor As Outlook.PropertyAccessor 

    Dim Recipient As Outlook.Recipient 
    For Each Recipient In Recipients 
     Set Accessor = Recipient.PropertyAccessor 

     Dim i As Long 
     Addresses(i, 0) = Recipient.Name 
     Addresses(i, 1) = Accessor.GetProperty(PR_SMTP_ADDRESS) 

     i = i + 1 
    Next 

    GetSMTPAddressesForRecipients = Addresses 
End Function 

通过电子邮件中的所有收件人这将循环,并捕获他们的姓名和电子邮件,将每个人放入阵列中的下一个位置。接下来,我们需要在日常工作中使用这些信息:

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean) 
    ' Note that I explicitly convert the subject to lowercase since the patterns use lowercase 
    Dim EmailSubject As String 
    EmailSubject = LCase(Item.Subject) 

    If EmailSubject Like "*update*" Or EmailSubject Like "*revision*" Then 
     Dim Addresses As Variant 
     Addresses = GetSMTPAddressesForRecipients(Item) 

     Dim i As Long 
     For i = LBound(Addresses, 1) To UBound(Addresses, 1) 
      If Addresses(i, 1) = "[email protected]" Then 
       MsgBox "Dont forget to update Drawing PDF`s if necessary", vbExclamation, "Have you updated the PDF`s?" 
       Exit For 
      End If 
     Next 
    End If 
End Sub 

有几件事要注意。首先,您的模式使用的是小写字母,因此您需要将主题转换为小写字母,因此,如果您有像“更新版本”这样的主题,您的模式仍然会捕获该主题。其次,我把最可能的情况放在前面,也就是说,你的大多数电子邮件主题不会包含“主题”或“修订”。然后无需向服务器询问收件人的地址。以前,您的代码会在检查它是否需要它之前获取地址。最好只要求我们需要的东西,它使您的代码更易于阅读和维护,同时还可以降低任何处理成本。

最后,这段代码将循环通过全部地址,而不只是看第一个。通过这样做,即使他是列表中的第二个,第三个或第五十个地址,您仍然会触发警报。

我希望这有助于!以下是完整的代码:

Option Explicit 

Private Const PR_SMTP_ADDRESS As String = "http://schemas.microsoft.com/mapi/proptag/0x39FE001E" 

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean) 
    ' Note that I explicitly convert the subject to lowercase since the patterns use lowercase 
    Dim EmailSubject As String 
    EmailSubject = LCase(Item.Subject) 

    If EmailSubject Like "*update*" Or EmailSubject Like "*revision*" Then 
     Dim Addresses As Variant 
     Addresses = GetSMTPAddressesForRecipients(Item) 

     Dim i As Long 
     For i = LBound(Addresses, 1) To UBound(Addresses, 1) 
      If Addresses(i, 1) = "[email protected]" Then 
       MsgBox "Dont forget to update Drawing PDF`s if necessary", vbExclamation, "Have you updated the PDF`s?" 
       Exit For 
      End If 
     Next 
    End If 
End Sub 

Private Function GetSMTPAddressesForRecipients(ByVal MailItem As Outlook.MailItem) As Variant 
    Dim Recipients As Outlook.Recipients 
    Set Recipients = MailItem.Recipients 

    Dim Addresses As Variant 
    ReDim Addresses(0 To Recipients.Count - 1, 0 To 1) 

    Dim Accessor As Outlook.PropertyAccessor 

    Dim Recipient As Outlook.Recipient 
    For Each Recipient In Recipients 
     Set Accessor = Recipient.PropertyAccessor 

     Dim i As Long 
     Addresses(i, 0) = Recipient.Name 
     Addresses(i, 1) = Accessor.GetProperty(PR_SMTP_ADDRESS) 

     i = i + 1 
    Next 

    GetSMTPAddressesForRecipients = Addresses 
End Function 
+0

真是太棒了。您确定了问题,重新编写了代码(以及它的工作原理),完整的解释和信息加载。这太棒了,非常感谢你 – mike

+0

不是一个问题,它是我们在这里。祝你好运! :) –

相关问题