Outlook中的vba脚本不适用于内部电子邮件

时间:2015-10-25 23:13:06

标签: vba email permissions outlook

所以我想让一个脚本在每个发送的项目上运行到特定的内部邮箱,我在网上找到了这个代码;

Public Sub application_ItemSend(ByVal Item As Object, Cancel As Boolean)

'check for address
If InStr(LCase(Item.To), "relevant.email@outlook.com") Then
      'ask if we've added the date
      prompt$ = "You're sending this to " & Item.To & ". have you added the due date?"
       If MsgBox(prompt$, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Check Address") = vbNo Then
         Cancel = True
       End If
  End If

End Sub

因此脚本可以工作,但仅适用于外部电子邮件(我一直在使用我的个人电子邮件进行测试)但内部邮箱没有,当您将其发送到内部邮箱时,脚本甚至不会运行。

这似乎更像是一个持续性问题而不是其他任何事情,但我想看看你们中是否有人可以插入。我不确定这是否是一个常见的问题而不是它会出现但我一直无法在网上找到任何东西,我只能在一夜之间做头疼!

希望你能提供帮助。 :)

谢谢,

汤姆。

2 个答案:

答案 0 :(得分:1)

To属性只是使用“;”连接的所有To收件人的显示名称。它可能包含也可能不包含SMTP地址。

循环访问“收件人”集合中的所有收件人,阅读Recipient.Type属性以确保其为olTo。检索Recipient.AddressEntry属性(返回AddressEntry对象)。如果AddressEntry.Type = "SMTP",请使用AddressEntry.Address。如果AddressEntry.Type = "EX",请使用AddressEntry.GetExchangeUser.PrimarySmtpAddress

另请注意,Cancel参数必须声明为ByRef

dim addrType
dim addr
dim recip    
for each recip in item.Recipients
 if recip.Type = olTo Then
    addrType = recip.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x3002001F")
    if addrType = "EX" Then
      addr = recip.AddressEntry.GetExchangeUser.PrimarySmtpAddress
    Else
      addr = recip.Address
    End If
    if LCase(addr) = "relevant.email@outlook.com" Then
      MsgBox "got it"
      Exit for
    End If
  End If
next

答案 1 :(得分:0)

您使用了错误的功能" Instr"将返回一个字符串在另一个字符串内的位置。如果你想比较两个字符串,正确的函数是" StrComp"

Option Explicit

Public Sub application_ItemSend(ByVal Item As Object, Cancel As Boolean)

    Const strRELEVANT_EMAIL_ADDRESS As String = "relevant.email@outlook.com"
    Dim strPromp As String

    strPromp = "You're sending this to " & Item.To & ". have you added the due date?"

    'check for address
    If StrComp(LCase$(Item.To), strRELEVANT_EMAIL_ADDRESS) = 0 Then

        'ask if we've added the date
        If MsgBox(strPromp, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Check Address") = vbNo Then
            Cancel = True
        End If
    End If

End Sub

希望这能解决问题。

由于