有谁知道是否可以在 Outlook 2016 中触发签名更改事件? IE。模拟点击签名,如下图所示:
我正在尝试根据选择的“发件人”地址自动更改签名。我可以捕获“发件人”地址更改(如所回答here https://stackoverflow.com/q/42654337/1596582).
但是我找不到以编程方式更改签名的方法。我的研究得出的结论是CommandBar
对象在 Office 2016 中已弃用,我需要与IRibbonUI
目的。或者也许是其他一些 Ribbon 对象?或者除了假装点击按钮之外,也许还有更好的选择签名的方法?
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:
所以某处有某个对象must了解签名 - 也许是电子邮件编辑器?
非常感谢
哇,好吧,所以想通了,但这是实现结果的相当迂回的方式。感谢@niton这个方便的评论 https://stackoverflow.com/questions/42723453/trigger-outlook-event-change-signature#comment72567917_42723453为我指明正确的方向。
总之,它执行以下操作:
- 引发事件时
SentOnBehalfOfName
财产在MailItem
被改变了
- 根据名为 的书签的存在删除当前签名
_MailAutoSig
- 根据选择的发件人选择 html 签名
- 插入html签名文件内容并添加名为的书签
_MailAutoSig
这是我迄今为止实现的代码:
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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)