触发Outlook事件:更改签名 [英] Trigger Outlook Event: Change Signature
问题描述
有人知道在Outlook 2016中是否可能触发签名更改事件? IE.按照下面的屏幕快照模拟单击签名:
Does anyone know if it possible to trigger a signature change event in Outlook 2016? I.e. Simulate clicking a signature as per the screenshot below:
我正在尝试根据已选择的发件人"地址自动更改签名.我可以捕获发件人"地址的更改(在此处回答).
I am trying to change the signature automatically based on which 'From' address has been selected. I can capture the 'From' address change (as answered here).
但是,我找不到以编程方式更改签名的方法.我的研究得出的结论是,在Office 2016中不推荐使用CommandBar
对象,并且需要与IRibbonUI
对象进行交互.还是其他功能区对象?也许除了伪造按钮单击之外,还有其他更好的选择签名的方法?
However I cannot find a way to programmatically change the signature. My research has gotten as far as concluding that the CommandBar
object is deprecated in Office 2016 and that I need to interact with the IRibbonUI
object. Or perhaps some other Ribbon object? Or perhaps there is better way of selecting the signature other than faking a button click?
我无法在Outlook
命名空间中找到Signature
对象,并导致我得出这样的结论:MailItem
类不了解签名-它仅了解正文.但是奇怪的是,我可以右键单击签名正文并打开一个上下文菜单:
I cannot find a Signature
object in the Outlook
namespace with leads me to conclude that the MailItem
class does not know about signatures - it only knows about body text. However what is odd is that I can right-click on the signature body and bring up a context menu:
因此,某些地方 的某些对象必须了解签名-也许是电子邮件编辑器?
So some object somewhere must know about signatures - perhaps the email editor?
非常感谢
推荐答案
哇,好了,可以解决这个问题,但这是获得结果的一种round回的方式.感谢@niton的此方便的注释,为我指出了正确的方向.
Wow OK so figured this out but it is quite a roundabout way of achieving the result. Thanks to @niton for this handy comment to point me in the right direction.
总而言之,它执行以下操作:
In summary it performs the following:
- 更改
MailItem
中的SentOnBehalfOfName
属性时引发事件 - 基于名为
_MailAutoSig
的书签删除当前签名
- 根据选择的发件人选择html签名
- 插入html签名文件的内容并添加名为
_MailAutoSig
的书签
- Raise event when
SentOnBehalfOfName
property inMailItem
is changed - Delete the current signature based on the presence of bookmark named
_MailAutoSig
- Select the html signature based upon which sender was selected
- Insert the html signature file contents and add bookmark named
_MailAutoSig
这是我到目前为止已实现的代码:
Here is the code I have implemented so far:
Dim WithEvents myInspector As Outlook.Inspectors
Dim WithEvents myMailItem As Outlook.MailItem
Private Sub Application_Startup()
Set myInspector = Application.Inspectors
End Sub
Private Sub myInspector_NewInspector(ByVal Inspector As Outlook.Inspector)
If TypeOf Inspector.CurrentItem Is MailItem Then
Set myMailItem = Inspector.CurrentItem
End If
End Sub
Private Sub myMailItem_PropertyChange(ByVal Name As String)
On Error GoTo ErrorCatcher
Dim signatureName As String
Dim signatureFilePath As String
' Properties we are interested in: "SendUsingAccount" / "SentOnBehalfOfName"
' Both get fired when the 'From' field is changed/re-selected
' So we are only going to trigger on one event or we will call the code twice
If Name = "SentOnBehalfOfName" Then
' Delete the current signature
Call DeleteSignature(myMailItem)
' Insert the new signature at the current cursor point
' The cursor will be at the point where the old signature was deleted
signatureName = GetSignatureName(myMailItem.SentOnBehalfOfName)
signatureFilePath = GetSignatureFilePath(signatureName)
Call InsertSignature(myMailItem, signatureFilePath)
End If
Exit Sub
ErrorCatcher:
MsgBox Err.Description
End Sub
Private Function DeleteSignature(objMail As MailItem)
Dim objDoc As Word.Document
Dim objBkm As Word.Bookmark
Set objDoc = objMail.GetInspector.WordEditor
If objDoc.Bookmarks.Exists("_MailAutoSig") Then
Set objBkm = objDoc.Bookmarks("_MailAutoSig")
objBkm.Select
objDoc.Windows(1).Selection.Delete
End If
End Function
Private Function GetSignatureName(sender As String)
Select Case sender
Case "Sender Name 1"
GetSignatureName = "Signature 1"
Case "Sender Name 2"
GetSignatureName = "Signature 2"
Case Else
GetSignatureName = "Default"
End Select
End Function
Private Function GetSignatureFilePath(signatureName As String) As String
GetSignatureFilePath = Environ("AppData") & "\Microsoft\Signatures\" & signatureName & ".htm"
End Function
Private Function InsertSignature(objMail As MailItem, signatureFilePath As String)
Dim objDoc As Word.Document
Dim rngStart As Range
Dim rngEnd As Range
Set objDoc = objMail.GetInspector.WordEditor
Set rngStart = objDoc.Application.Selection.Range
rngStart.Collapse wdCollapseStart
Set rngEnd = rngStart.Duplicate
rngEnd.InsertParagraph
rngStart.InsertFile signatureFilePath, , , , False
rngEnd.Characters.Last.Delete
objDoc.Bookmarks.Add "_MailAutoSig", rngEnd
End Function
这篇关于触发Outlook事件:更改签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!