2017-02-17 75 views
0

我想使用这个从interweb中“偷走”的宏。 (至少我很诚实...)oItem_Reply在取消消息窗口后停止工作

它回复邮件时会在主题行添加一个关键字。

它发送电子邮件时按预期工作,但当我取消消息窗口功能停止触发,直到我重新启动Outlook。

为什么它的行为如此?我试图调试它没有任何运气... 任何人都可以帮我解决这个问题吗?

的代码ThisOutlookSession

Option Explicit 
    Private WithEvents oExpl As Explorer 
    Private WithEvents oItem As MailItem 
    Private bDiscardEvents As Boolean 
    '//slipstick.me/44b0w 

    Private Sub Application_Startup() 
     Set oExpl = Application.ActiveExplorer 
     bDiscardEvents = False 
    End Sub 

    Private Sub oExpl_SelectionChange() 
     On Error Resume Next 
     Set oItem = oExpl.Selection.Item(1) 
    End Sub 

    ' Reply 
    Private Sub oItem_Reply(ByVal Response As Object, Cancel As Boolean) 

     Cancel = True 
     bDiscardEvents = True 

     Dim oResponse As MailItem 
     Set oResponse = oItem.Reply 

    ' add the fields here 
     oResponse.Subject = "keyword " & oResponse.Subject 

     oResponse.Display 

     bDiscardEvents = False 
    Set oItem = Nothing 
    End Sub 

回答

0

不知道为什么你得到的错误;可能会出现一些问题 - 可能是在子文件的开头取消或使用“Set oItem = Nothing”。此外bDiscardEvents似乎没有做任何事情...

尝试以下操作:

在ThisOutlookSession

Private Sub olItem_Reply(ByVal Response As Object, Cancel As Boolean) 
    If AddKeywordToReply(olItem, "Test:") = True Then Cancel = True 
End Sub 

在模块

Function AddKeywordToReply(oItem As MailItem, KeyWord As String) As Boolean 
    AddKeywordToReply = False 

    If oItem.Class = olMail Then 
     If Not oItem.sender Is Nothing Then 
      Dim oMsgReply As Outlook.MailItem 
      Set oMsgReply = oItem.Reply 
      oMsgReply.Subject = KeyWord & " " & oItem.Subject 
      oMsgReply.Display 
      Set oMsgReply = Nothing 
      AddKeywordToReply = True 
     End If 
    End If 
End Function 
+0

哦 - 我只记得关于展望;如果出现错误或代码被更改,宏/ VBA将不再运行,并且需要重新启动程序才能重新启动VBA。我认为这是一种保护措施。 – Tragamor

+0

谢谢@Flephal!我自己也发现了这一点。 bDiscardEvents什么也没做,所以它可以被删除,并且'Set oItem = Nothing'取消了资源管理器中的邮件项目,所以它不会在下一次触发。如果我再次手动选择该项目,则脚本触发时无需重新启动Outlook。 – MrDark

相关问题