2017-02-17 63 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

相關問題