【问题标题】:vba script in outlook not working for internal emailsOutlook中的vba脚本不适用于内部电子邮件
【发布时间】:2015-10-25 23:13:06
【问题描述】:

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

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

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

这似乎更像是一个权限问题,但我想看看你们中是否有人可以参与进来。我不确定这是否比看起来更常见,但我一直无法在网上找到任何东西,我只能在一个晚上做这么多的挠头!

希望您能提供帮助。 :)

谢谢,

汤姆。

【问题讨论】:

    标签: vba email permissions outlook


    【解决方案1】:

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

    遍历 Recipients 集合中的所有收件人,读取 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
    

    【讨论】:

    • 嗨,Dmitry,你有没有机会给我看一个如何阅读类型等的例子? Outlook 中的 VBA 对我来说是新的,而 VBA 通常是我很少使用的东西,所以我很不确定你的意思。
    • 非常感谢,我查看了属性访问器的内容,但无法正确解决!现在我知道以供将来参考。 :)
    • 在这种特殊情况下,您不必使用 Recipient.PropertyAccessor - 我只是使用它从收件人那里检索 PR_ADDRTYPE,而不是使用 Recipient.AddressEntry.Type(更昂贵)。跨度>
    【解决方案2】:

    您为此“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
    

    希望这能解决问题。

    谢谢

    【讨论】:

      猜你喜欢
      • 2014-09-05
      • 2017-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-05
      相关资源
      最近更新 更多