我已經搜索了好幾天,將 Stack Overflow 周圍其他帖子的代碼和 learn.microsoft.com 檔案中的代碼拼湊在一起以獲得解決方案,我正在努力......我相對較新,這無濟于事展望VBA。
我想要發生的是,當我使用默認功能區按鈕創建新電子郵件時,自動向正文添加問候語,同時保留添加的默認簽名。(也希望它適用于行內回復,但我首先關注這個)。
我已經有一個公共函式,用于TimeValue計算是添加“早上好”還是“下午好”,它可以作業并以字串形式回傳。我呼叫一個姊妹函式,它獲取該字串并將其添加到電子郵件中。這部分有效。
這兩個都在一個名為“AutoGreeting”的模塊中
Option Explicit
Public Function Greeting() As String
' Defines greeting by time of day '
' Used in with the AddGreeting() function and clsMailHandler '
' If before noon, greeting is Good Morning'
If Time >= TimeValue("8:00 AM") And Time <= TimeValue("11:59 AM") Then
Greeting = "Good morning,"
' If after noon and before work ends, greeting is Good Afternoon'
ElseIf Time >= TimeValue("12:00 PM") And Time <= TimeValue("5:10 PM") Then
Greeting = "Good afternoon,"
End If
End Function
' call this function to add the above calculated greeting to an email '
' i.e. Call AddGreeting(NewlyCreatedEmail)
Public Function AddGreeting(ByRef DraftEmail As mailItem)
' DraftEmail is used with reference to any MailItem object '
' like in clsMailHander > NewInspector > objCurrentItem / objMailNew '
With DraftEmail
' Temporarily editing the subject for testing/debugging to make sure this works
.Subject = "AddGreeting Function"
' This adds the greeting but isn't able to keep the OG body AKA the auto-signature
' Because newInspector fires before signature is added
.HTMLBody = Greeting() & DraftEmail.HTMLBody
End With
End Function
我還在構建一個用于事件處理的類模塊,它能夠檢測新檢查器何時打開并且它是一個郵件專案。(不幸的是,它似乎還無法檢測它是一封新電子郵件還是已檢索并打開的電子郵件。就像您雙擊收件箱中的電子郵件一樣,它會在檢查器視窗中打開。我這樣做是通過有時會發生事故)。
Explorer 和objMailReply變數都在那里,因為我也希望它能處理這個行內回復。我有事件處理程式newExplorer,ActiveInlineResponse我在這里遺漏了,因為我現在只關注檢查員中的新電子郵件。
類模塊稱為“clsMailHandler”
' Class for event handling of created emails
' re-start Outlook after compiling and saving changes to re-initialize class
' or run Application_Quit and Application_Startup from ThisOutlookSession cls
Option Explicit
Public WithEvents olApp As Outlook.Application
Public WithEvents objInspectors As Outlook.Inspectors
Public WithEvents objActInspector As Outlook.Inspector
Public WithEvents objExplorers As Outlook.Explorers
Public WithEvents objActExplorer As Outlook.Explorer
Public WithEvents objCurrentItem As Outlook.mailItem
Public WithEvents objMailNew As Outlook.mailItem
Public WithEvents objMailReply As Outlook.mailItem
' Called under Application_Startup in ThisOutlookSession as Handler class is created
Public Sub Class_Initialize()
Set olApp = Outlook.Application
' so far, all that's needed here is to initialize the explorers and inspectors w/ the app itself
Set objInspectors = olApp.Inspectors
Set objExplorers = olApp.Explorers
Set objActExplorer = olApp.ActiveExplorer
End Sub
' Called in Application_Quit as handler class is cleared
Public Sub Class_Terminate()
'when the application is closed, the class is terminated
'un-set variables
Set olApp = Nothing
Set objInspectors = Nothing
Set objActInspector = Nothing
Set objExplorers = Nothing
Set objActExplorer = Nothing
Set objMailNew = Nothing
Set objMailReply = Nothing
Set objCurrentItem = Nothing
End Sub
' Event handler for a new inspector window opening (i.e. new email is created)
' ISSUE - or when a received email is opened in a new window (double-click)
Public Sub objInspectors_NewInspector(ByVal Inspector As Outlook.Inspector)
Dim insType As String
Set objActInspector = Inspector
' this is just to keep names of object variables short and easy to remember
Set objCurrentItem = objActInspector.CurrentItem
' grab & test type name of current inspector item
insType = TypeName(objCurrentItem)
If insType = "MailItem" Then
' if its a mailItem - set variable that's more specific
Set objMailNew = objCurrentItem
' MsgBox is for debugging to make sure this fires
MsgBox ("New email has been created")
' Function from other module that is called to add the greeting
' Again, this works to add the greeting, but it doesn't keep the auto-signature
Call AddGreeting(objMailNew)
End If
End Sub
' This also fires if a received email that was opened in a new window is closed.
Public Sub objActInspector_Close()
' if the inspector window (created email) is closed, clear the variables
Set objMailNew = Nothing
Set objCurrentItem = Nothing
MsgBox ("Inspector has closed")
End Sub
這是從 ThisOutlookSession 中初始化類的方式
Option Explicit
'Instantiate the class on global application level
Dim EventHandler As clsMailHandler
Sub Application_Startup()
'Set custom variable as new instance of class
'to initialize the class (run Class_Initialize() sub)
Set EventHandler = New clsMailHandler
End Sub
Sub Application_Quit()
'Set handler to nothing to clear instance of class
Set EventHandler = Nothing
End Sub
我遇到的問題是,事件處理程式newInspector可以呼叫添加問候語的函式(并編輯主題以進行測驗/除錯),但是我的自動簽名沒有被添加。我認為因為newInspector事件在電子郵件實際存在之前觸發,所以自動簽名不會觸發。我的簽名需要特定格式并包含影像,這也很復雜,因此僅將其添加為純文本是行不通的。
我看到的大多數解決方案都涉及以編程方式創建電子郵件CreateItem(olMailItem),但我不想那樣做。我想將這些應用于默認創建電子郵件的方式。
我見過的一些東西聽起來像是可以滿足我的需要,但我不知道如何實作它們,因為我找不到我理解的示例。也就是說,inspector_activate就像這篇文章中的Event that fires after signature is added一樣。
如何獲得我的自動問候語并保留我的自動簽名?
編輯:我自己修好了,下面添加了解決方案作為答案。
uj5u.com熱心網友回復:
首先,新專案沒有EntryID設定屬性,在將專案保存到 Outlook 中的商店之前它是空的。因此,檢查屬性值有助于區分新專案和已保存專案。
Activate其次,如果要將簽名添加到訊息正文中,則需要等待 Inspector 類的第一個事件。否則,默認簽名將被您修改的郵件正文覆寫。
第三,要在訊息正文中保留默認簽名,您必須在屬性值中的開始<body>標記之后立即插入您的問候語。HTMLBody
uj5u.com熱心網友回復:
我自己的回答:我使用Inspector_Activate事件作為單獨的子來修復它。默認的“新電子郵件”功能區按鈕會創建一個新的檢查器視窗,該_NewInspector事件會捕獲該視窗并將其設定為活動檢查器。Inspector_Activate當電子郵件本身完成其自己的創建/初始化程序時,事件就會被觸發。然后AddGreeting()呼叫該函式來編輯.HTMLbody和添加自動問候語。
Public Sub objInspectors_NewInspector(ByVal Inspector As Outlook.Inspector)
If TypeName(Inspector.CurrentItem) = "MailItem" Then
Set objActInspector = Inspector
End If
End Sub
Public Sub objActInspector_Activate()
MsgBox ("Inspector Activate")
Set objMailNew = objActInspector.CurrentItem
If Len(objMailNew.EntryID) = 0 Then
Call AddGreeting(objMailNew)
End If
Set objActInspector = Nothing
End Sub
在這個隨機網站上找到它:https ://www.vboffice.net/en/developers/newinspector-and-inspector-activate/
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/535870.html
下一篇:收到錯誤“statement.executeupdate()無法發出生成結果集的陳述句。”嘗試使用JDBC插入mysql時
