簡體   English   中英

將簽名插入 Outlook email 從 Excel Z6E3EC7E6A9F6007B09838FC0EEZ93A

[英]Inserting Signature into Outlook email from Excel VBA

我正在嘗試使用 VBA 為 Excel 自動化一些電子郵件。 到目前為止一切都很好,除了試圖將我的簽名保留在 email 中。

一旦你告訴 VBA 創建一個新的 email,它就會包含你的默認簽名。 如果您嘗試Outmail.Display ,則可以看到這一點。 但是,如果您覆蓋.HTMLBody屬性,它將被刪除。

我的簡單嘗試是將.HTMLBody的內容保存到signature (字符串),然后將其重新分配給.HTMLBody以進行測試。 但是,生成的 email 將沒有任何簽名。

復制並重新插入簽名的代碼:

Dim myOlApp As Outlook.Application
Dim MyItem As Outlook.MailItem

Dim signature As String


Set myOlApp = CreateObject("Outlook.Application")
Set Outmail = myOlApp.CreateItem(0)

signature = Outmail.HTMLBody
Outmail.HTMLBody = signature

Outmail.Display

顯示自動插入簽名的代碼:

Dim myOlApp As Outlook.Application
Dim MyItem As Outlook.MailItem 
Set myOlApp = CreateObject("Outlook.Application")
Set Outmail = myOlApp.CreateItem(0)
Outmail.Display

編輯:我嘗試用.Body .HTMLBody 這行得通,但顯然去掉了簽名的 HTML 格式

編輯2:代碼在我朋友的電腦上有效,但在我的電腦上無效

解決了。 只需重新啟動Outlook解決了它。

另一個可能的解決方案是直接從Windows存儲它的目錄中獲取簽名並將其附加到正文。 Microsoft MVP解釋了這里(有點冗長)的過程: https//www.rondebruin.nl/win/s1/outlook/signature.htm

我用這個技巧解決了這個問題:

Set myOutlook = CreateObject("Outlook.Application")
Set tempMail = myOutlook.CreateItem(olMailItem)

With tempMail
  ' Trick to preserve Outlook default signature
  ' MailItem need to be displayed to be "fully created" as object (maybe VBA bug)

  .Display
  HTMLBody = .HTMLBody
  .Close olDiscard

  ' --- Trick to split HTMLBody into Head and Signature ---
  ' Search for the position of the tag before signature
  bodyTag = "<body"
  PosBody = InStr(HTMLBody, bodyTag)
  pTag = "<o:p>"
  PosSignature = InStr(PosBody, HTMLBody, pTag)

  Head = Left(HTMLBody, PosSignature - 1)
  Signature = Right(HTMLBody, Len(HTMLBody) - PosSignature + 1)

End With

然后,您可以簡單地將HTML文本放在Head和Signature之間:

Set myOutlook = CreateObject("Outlook.Application")
Set myMail = myOutlook.CreateItem(olMailItem)

With myMail
  .To = Recipients
  .Subject = Subject
  .CC = CC
  .HTMLBody = Head & "Here the HTML text of your mail" & Signature
End With

我認為這是一種VBA錯誤:如果您不使用.Display方法,則不會“完全”創建MailItem對象。

嘗試放置一個制動點並在.Display方法行之前和之后查看立即窗口(Ctrl + G)上的?mymail.HTMLbody值....

您也可以在Locals窗口中獲取相同的簡單擴展mymail對象!

您可以從Signatures文件夾中讀取簽名文件(請記住文件夾名稱已本地化)並將其與郵件正文合並(您不能簡單地連接兩個格式良好的HTML文檔並獲取有效的HTML文檔)。 您還需要注意簽名樣式和圖像 - 它們必須單獨處理。

您還可以顯示消息(Outlook在顯示消息時使用簽名填充未修改的消息正文),然后讀取HTMLBody屬性並關閉檢查器(閃爍是不可避免的)。 如果要顯示該消息,請先顯示該消息,以便Outlook插入簽名,然后再添加您的數據(需要插入,而不是連接)。

如果使用Redemption是一個選項,它會公開RDOSignature對象。 您可以使用其ApplyTo方法將任何簽名插入任何消息中。

set Session = CreateObject("Redemption.RDOSession")
Session.MAPIOBJECT = Application.Session.MAPIOBJECT
set Drafts = Session.GetDefaultFolder(olFolderDrafts)
set Msg = Drafts.Items.Add
Msg.To = "user@domain.demo"
Msg.Subject = "testing signatures"
Msg.HTMLBody = "<html><body>some <b>bold</b> message text<br></body></html>"
set Account = Session.Accounts.GetOrder(2).Item(1) 'first mail account
if Not (Account Is Nothing) Then
  set Signature = Account.NewMessageSignature
  if Not (Signature Is Nothing) Then
    Signature.ApplyTo Msg, false 'apply at the bottom
  End If
End If
Msg.Display

我正在編寫 Word 文檔上的提交按鈕以將其發送給支持人員,並發現下面的解決方案提供了簽名。 這個 VBA 在 Excel 電子表格上的工作方式相同,方法是交換對 Word 的引用,將其替換為 Excel2668CFDE6319BD54968 基本上,在要求簽名之前,您必須先調用 .Display 命令。 隨着正文的添加,email 消息上有一點“閃光”,但我發現我可以忍受:-)。

Private Sub btnSubmit_Click() On Error Resume Next Dim objOutlook As Object Dim objEmail As Object Dim objDoc As Document Dim emailMsg As String

'let's turn off the screen for a bit...
Application.ScreenUpdating = False

Set objOutlook = CreateObject("Outlook.Application")
Set objEmail = objOutlook.CreateItem(0)     '0=olMailItem    

'the 'Submit' button is on this Word document, so we'll need
'to save the file before we can attach it...
Set objDoc = ActiveDocument
objDoc.Save

emailMsg = "Here's the body of the email... edit as needed."
      
'send the email
objEmail.Subject = "Testing Outlook Signatures"
objEmail.bodyFormat = 2                         '2=olFormatHTML
 
objEmail.To = "whateveremailaddressyouhave@somewhere.com"
objEmail.Importance = 1                         '1=olImportantceNormal
objEmail.Attachments.Add objDoc.FullName

'if we don't call this, the email message isn't 'real' yet...
objEmail.Display

'NOW, we can add the message and the HTML Body, 
'which is where the signature is...
objEmail.HTMLBody = emailMsg & objEmail.HTMLBody

'we'll do some cleanup here
Set objDoc = Nothing
Set objEmail = Nothing
Set objOutlook = Nothing
Application.ScreenUpdating = True

結束子

剪切簽名的html源(例如在編輯器或mozilla中 - 源視圖),然后將其復制到單元格中。 然后你可以將單元格的值添加到.htmlbody中,這將是完美的。 所有其他解決方案都很糟糕,我嘗試了所有:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM