简体   繁体   English

将签名插入 Outlook email 从 Excel Z6E3EC7E6A9F6007B09838FC0EEZ93A

[英]Inserting Signature into Outlook email from Excel VBA

I am trying to automate some emails using VBA for Excel.我正在尝试使用 VBA 为 Excel 自动化一些电子邮件。 Everything so far is fine, except trying to keep my signature in the email.到目前为止一切都很好,除了试图将我的签名保留在 email 中。

Once you tell VBA to create a new email, it will already contain your default signature.一旦你告诉 VBA 创建一个新的 email,它就会包含你的默认签名。 This can be seen if you try Outmail.Display .如果您尝试Outmail.Display ,则可以看到这一点。 However if you overwrite the .HTMLBody property, it will be deleted.但是,如果您覆盖.HTMLBody属性,它将被删除。

My simple attempt was to save the contents of .HTMLBody to a signature (string), and then reassign it to .HTMLBody just to test it out.我的简单尝试是将.HTMLBody的内容保存到signature (字符串),然后将其重新分配给.HTMLBody以进行测试。 However the resulting email will be void of any signature.但是,生成的 email 将没有任何签名。

Code to copy and re-insert the signature:复制并重新插入签名的代码:

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

Code to show that signature is automatically inserted:显示自动插入签名的代码:

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

Edit: I tried replacing .HTMLBody with .Body .编辑:我尝试用.Body .HTMLBody That works, but obviously takes out the HTML formatting of the signature这行得通,但显然去掉了签名的 HTML 格式

Edit 2: The code works on my friend's computer but not on mine编辑2:代码在我朋友的电脑上有效,但在我的电脑上无效

Solved. 解决了。 Simply restarting Outlook solved it. 只需重新启动Outlook解决了它。

Another potential solution is to grab the signature directly from the directory where Windows stores it and append it to the body. 另一个可能的解决方案是直接从Windows存储它的目录中获取签名并将其附加到正文。 A Microsoft MVP explains the (somewhat lengthy) process here: https://www.rondebruin.nl/win/s1/outlook/signature.htm Microsoft MVP解释了这里(有点冗长)的过程: https//www.rondebruin.nl/win/s1/outlook/signature.htm

I solved that issue with this trick: 我用这个技巧解决了这个问题:

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

Then you can simply put your HTML text between Head and Signature: 然后,您可以简单地将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

I think it's a sort of VBA bug: if you don't use the .Display method, the MailItem object is not "fully" created. 我认为这是一种VBA错误:如果您不使用.Display方法,则不会“完全”创建MailItem对象。

Try to place a brakepoint and look at ?mymail.HTMLbody values on Immediate Window (Ctrl + G) before and after the .Display method line.... 尝试放置一个制动点并在.Display方法行之前和之后查看立即窗口(Ctrl + G)上的?mymail.HTMLbody值....

You can also obtain the same simply expanding mymail object in the Locals Window! 您也可以在Locals窗口中获取相同的简单扩展mymail对象!

You can read the signature file from the Signatures folder (keep in mind that the folder name is localized) and merge it with the message body (you cannot simply concatenate two well formed HTML documents and get back a valid HTML document). 您可以从Signatures文件夹中读取签名文件(请记住文件夹名称已本地化)并将其与邮件正文合并(您不能简单地连接两个格式良好的HTML文档并获取有效的HTML文档)。 You would also need to be careful with the signature styles and images - they must be processed separately. 您还需要注意签名样式和图像 - 它们必须单独处理。

You can also display the message (Outlook populates the unmodified message body with the signature when the message is displayed) and then either read the HTMLBody property and close the inspector (the flicker is unavoidable). 您还可以显示消息(Outlook在显示消息时使用签名填充未修改的消息正文),然后读取HTMLBody属性并关闭检查器(闪烁是不可避免的)。 If you want to display the message anyway, display it first so that the signature is inserted by Outlook, and only then add your data (it needs to be inserted, not concatenated). 如果要显示该消息,请先显示该消息,以便Outlook插入签名,然后再添加您的数据(需要插入,而不是连接)。

If using Redemption is an option, it exposes the RDOSignature object. 如果使用Redemption是一个选项,它会公开RDOSignature对象。 You can use its ApplyTo method to insert any signature into any message. 您可以使用其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

I'm programming a Submit button on a Word document to send it to support, and found the solution below delivers the signature.我正在编写 Word 文档上的提交按钮以将其发送给支持人员,并发现下面的解决方案提供了签名。 This VBA would work the same on an Excel spreadsheet by swapping out the reference to Word, replacing it with an Excel object.这个 VBA 在 Excel 电子表格上的工作方式相同,方法是交换对 Word 的引用,将其替换为 Excel2668CFDE6319BD54968 Basically, you have to call the.Display command FIRST before asking for the signature.基本上,在要求签名之前,您必须先调用 .Display 命令。 There's a little 'flash' on the email message as the body gets added, but found I can live with that:-).随着正文的添加,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 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

End Sub结束子

cut the html source of your signature (for example in an editor or mozilla - source view) and then copy it into a cell. 剪切签名的html源(例如在编辑器或mozilla中 - 源视图),然后将其复制到单元格中。 then you can add the value of the cell to your .htmlbody and will be perfect. 然后你可以将单元格的值添加到.htmlbody中,这将是完美的。 all other solutions are awful, i tried them all :) 所有其他解决方案都很糟糕,我尝试了所有:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM