Outlook“运行脚本”规则不触发传入邮件的VBA脚本

问题描述:

我在另一个成员的建议中创建这个新主题。有关事情如何到达的更多历史记录,请参阅this questionOutlook“运行脚本”规则不触发传入邮件的VBA脚本

我有这个VBA脚本,我知道工程如果它被触发。如果我使用TestLaunch子例程,并且在我的收件箱中已经有符合规则条件的消息(但当然不会被规则启动),它会激活我希望它完美启动的链接。如果我在创建规则时将其应用于收件箱中的所有现有邮件,它的工作原理完美无瑕。但是,在需要的地方,新消息到达它不。

我知道剧本没有被触发,因为如果我有这样一个规则:

Outlook "New Message" rule that has "play sound" enabled

与“播放声音”作为它的一部分,声音始终发挥当邮件到达来自两个指定发件人中的任何一个,所以规则正在被触发。我已删除的声音从规则玩的一部分,并将其集成到用于测试目的的VBA代码,而不是:

Option Explicit 

Private Declare Function ShellExecute _ 
    Lib "shell32.dll" Alias "ShellExecuteA" (_ 
    ByVal hWnd As Long, _ 
    ByVal Operation As String, _ 
    ByVal Filename As String, _ 
    Optional ByVal Parameters As String, _ 
    Optional ByVal Directory As String, _ 
    Optional ByVal WindowStyle As Long = vbMinimizedFocus _ 
) As Long 

Private Declare Function sndPlaySound32 _ 
    Lib "winmm.dll" _ 
    Alias "sndPlaySoundA" (_ 
     ByVal lpszSoundName As String, _ 
     ByVal uFlags As Long) As Long 

Sub PlayTheSound(ByVal WhatSound As String) 
    If Dir(WhatSound, vbNormal) = "" Then 
     ' WhatSound is not a file. Get the file named by 
     ' WhatSound from the Windows\Media directory. 
     WhatSound = Environ("SystemRoot") & "\Media\" & WhatSound 
     If InStr(1, WhatSound, ".") = 0 Then 
      ' if WhatSound does not have a .wav extension, 
      ' add one. 
      WhatSound = WhatSound & ".wav" 
     End If 
     If Dir(WhatSound, vbNormal) = vbNullString Then 
      Beep   ' Can't find the file. Do a simple Beep. 
      Exit Sub 
     End If 
    Else 
     ' WhatSound is a file. Use it. 
    End If 

    sndPlaySound32 WhatSound, 0& ' Finally, play the sound. 
End Sub 

Public Sub OpenLinksMessage(olMail As Outlook.MailItem) 

Dim Reg1 As RegExp 
Dim AllMatches As MatchCollection 
Dim M As Match 
Dim strURL As String 
Dim RetCode As Long 

Set Reg1 = New RegExp 

With Reg1 
.Pattern = "(https?[:]//([0-9a-z=\?:/\.&-^!#$;_])*)" 
.Global = True 
.IgnoreCase = True 
End With 

PlayTheSound "chimes.wav" 

' If the regular expression test for URLs in the message body finds one or more 
If Reg1.test(olMail.Body) Then 

'  Use the RegEx to return all instances that match it to the AllMatches group 
     Set AllMatches = Reg1.Execute(olMail.Body) 
     For Each M In AllMatches 
       strURL = M.SubMatches(0) 
'    Don't activate any URLs that are for unsubscribing; skip them 
       If InStr(1, strURL, "unsubscribe") Then GoTo NextURL 
'    If the URL ends with a > from being enclosed in darts, strip that > off 
       If Right(strURL, 1) = ">" Then strURL = Left(strURL, Len(strURL) - 1) 
'    The URL to activate to accept must contain both of the substrings in the IF statement 
       If InStr(1, strURL, ".com") Then 
        PlayTheSound "TrainWhistle.wav" 
'     Activate that link to accept the job 
        RetCode = ShellExecute(0, "Open", "http://nytimes.com") 
        Set Reg1 = Nothing 
        Exit Sub 
       End If 

NextURL: 
    Next 

End If 

Set Reg1 = Nothing 

End Sub 

Private Sub TestLaunchURL() 
    Dim currItem As MailItem 
    Set currItem = ActiveExplorer.Selection(1) 
    OpenLinksMessage currItem 
End Sub 

应发挥“CHIMES.WAV”如果VBA脚本在所有情况下触发,玩“ TrainWhistle.wav“,如果我的实际链接激活处理发生。当新消息到达时,两者都不会发生,但是如果Outlook规则中有一个“播放声音”,应该运行该声音播放的脚本。

目前我的信任中心设置为允许所有的宏,因为Outlook在测试过程中早些时候对使用selfcert.exe进行签名时很烦躁。我真的希望能够再次提升宏观保护,而不是在完成这些工作时让他们保持“全部运行”。

但是,首先,我不能为了我的生活找出为什么这个脚本可以通过调试器完美运行,或者如果应用于现有的消息,但不是由应用于现有消息的完全相同的Outlook规则触发的一个实际的新消息到达。在Outlook 2010中,我正在开发此脚本,并且也在Outlook 2016下,在正在部署它的朋友的计算机上,这种情况属实。

有关解决此问题的任何指导将不胜感激。

+0

从这个和以前的问题中表达的想法可能你不知道在规则向导中有一个“运行脚本”选项,你会选择OpenLinksMessage。 – niton

+0

呃,没有。我并不认为这是从我对文本和标题规则的扩展讨论中看不出来的。问题是,即使消息符合_should_导致它运行的规则标准,规则中的“运行脚本”实际上也不会运行该脚本。让我们不要进入我给出的显示“运行脚本”规则的屏幕截图。 – britechguy

+0

其他人是否会为传入消息运行脚本代码?一些简单的像MsgBox oMail.SenderEmailAddress作为唯一的代码行。在所有传入消息上运行,规则中没有地址条件。 – niton

这里是最后工作的代码。很明显,olMail的.Body成员在某种幕后处理已经有时间发生之前不可用,并且如果您没有等足够长时间,那么当您使用它进行测试时,它不会存在。关注公共子OpenLinksMessage

允许进行处理的主要变化显然是添加了代码行:Set InspectMail = olMail.GetInspector.CurrentItem。运行此set语句所需的时间允许.Body在Outlook规则传入的olMail参数上可用。有趣的是,如果你在set语句之后立即显示InspectMail.Body,它将显示为空,就像olMail.Body一样。

Option Explicit 

Private Declare Function ShellExecute _ 
    Lib "shell32.dll" Alias "ShellExecuteA" (_ 
    ByVal hWnd As Long, _ 
    ByVal Operation As String, _ 
    ByVal Filename As String, _ 
    Optional ByVal Parameters As String, _ 
    Optional ByVal Directory As String, _ 
    Optional ByVal WindowStyle As Long = vbMinimizedFocus _ 
) As Long 



Public Sub OpenLinksMessage(olMail As Outlook.MailItem) 

Dim InspectMail As Outlook.MailItem 
Dim Reg1 As RegExp 
Dim AllMatches As MatchCollection 
Dim M As Match 
Dim strURL As String 
Dim SnaggedBody As String 
Dim RetCode As Long 

' The purpose of the following Set statement is strictly to "burn time" so that the .Body member of 
' olMail is available by the time it is needed below. Without this statement the .Body is consistently 
' showing up as empty. What's interesting is if you use MsgBox to display InspectMail.Body immediately after 
' this Set statement it shows as empty. 
Set InspectMail = olMail.GetInspector.CurrentItem 

Set Reg1 = New RegExp 

With Reg1 
.Pattern = "(https?[:]//([0-9a-z=\?:/\.&-^!#$;_])*)" 
.Global = True 
.IgnoreCase = True 
End With 

RetCode = Reg1.Test(olMail.Body) 
' If the regular expression test for URLs in the message body finds one or more 
If RetCode Then 
'  Use the RegEx to return all instances that match it to the AllMatches group 
     Set AllMatches = Reg1.Execute(olMail.Body) 
     For Each M In AllMatches 
       strURL = M.SubMatches(0) 
'    Don't activate any URLs that are for unsubscribing; skip them 
       If InStr(1, strURL, "unsubscribe") Then GoTo NextURL 
'    If the URL ends with a > from being enclosed in darts, strip that > off 
       If Right(strURL, 1) = ">" Then strURL = Left(strURL, Len(strURL) - 1) 
'    The URL to activate to accept must contain both of the substrings in the IF statement 
       If InStr(1, strURL, ".com") Then 
'     Activate that link to accept the job 
        RetCode = ShellExecute(0, "Open", strURL) 
        Set InspectMail = Nothing 
        Set Reg1 = Nothing 
        Set AllMatches = Nothing 
        Set M = Nothing 
        Exit Sub 
       End If 

NextURL: 
    Next 

End If 

Set InspectMail = Nothing 
Set Reg1 = Nothing 
Set AllMatches = Nothing 
Set M = Nothing 

End Sub 

特别感谢他的耐心和帮助。