从 Excel VBA 将签名插入 Outlook 电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22233264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:56:08  来源:igfitidea点击:

Inserting Signature into Outlook email from Excel VBA

excelvbaoutlooksignature

提问by msmf14

I am trying to automate some emails using VBA for Excel. Everything so far is fine, except trying to keep my signature in the email.

我正在尝试使用 VBA for Excel 自动化一些电子邮件。到目前为止一切都很好,除了试图在电子邮件中保留我的签名。

Once you tell VBA to create a new email, it will already contain your default signature. This can be seen if you try Outmail.Display. However if you overwrite the .HTMLBodyproperty, it will be deleted.

一旦您告诉 VBA 创建新电子邮件,它就已经包含您的默认签名。如果您尝试,可以看到这一点Outmail.Display。但是,如果您覆盖该.HTMLBody属性,它将被删除。

My simple attempt was to save the contents of .HTMLBodyto a signature(string), and then reassign it to .HTMLBodyjust to test it out. However the resulting email will be void of any signature.

我的简单尝试是将 的内容保存.HTMLBodysignature(字符串)中,然后将其重新分配给以.HTMLBody进行测试。然而,由此产生的电子邮件将没有任何签名。

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 .HTMLBodywith .Body. That works, but obviously takes out the HTML formatting of the signature

编辑:我试着更换.HTMLBody.Body。这有效,但显然去掉了签名的 HTML 格式

Edit 2: The code works on my friend's computer but not on mine

编辑 2:代码在我朋友的电脑上有效,但在我的电脑上无效

采纳答案by msmf14

Solved. Simply restarting Outlook solved it.

解决了。只需重新启动 Outlook 即可解决。

回答by takanuva15

Another potential solution is to grab the signature directly from the directory where Windows stores it and append it to the body. A Microsoft MVP explains the (somewhat lengthy) process here: https://www.rondebruin.nl/win/s1/outlook/signature.htm

另一个可能的解决方案是直接从 Windows 存储签名的目录中获取签名并将其附加到正文中。微软 MVP 在这里解释了(有点冗长)过程:https: //www.rondebruin.nl/win/s1/outlook/signature.htm

回答by Federico F.

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.HTMLbodyvalues on Immediate Window (Ctrl + G) before and after the .Display method line....

尝试?mymail.HTMLbody在 .Display 方法行之前和之后放置一个刹车点并查看立即窗口(Ctrl + G)上的值....

You can also obtain the same simply expanding mymailobject in the Locals Window!

您还可以mymail在本地窗口中获得相同的简单扩展对象!

回答by Dmitry Streblechenko

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). You would also need to be careful with the signature styles and images - they must be processed separately.

您可以从 Signatures 文件夹中读取签名文件(请记住,文件夹名称是本地化的)并将其与邮件正文合并(您不能简单地连接两个格式正确的 HTML 文档并返回一个有效的 HTML 文档)。您还需要小心签名样式和图像 - 它们必须单独处理。

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 HTMLBodyproperty and close the inspector (the flicker is unavoidable). 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 使用签名填充未修改的消息正文),然后读取HTMLBody属性并关闭检查器(闪烁是不可避免的)。如果您无论如何都想显示该消息,请先显示它,以便 Outlook 插入签名,然后才添加您的数据(需要插入,而不是连接)。

If using Redemptionis an option, it exposes the RDOSignatureobject. You can use its ApplyTomethod to insert any signature into any message.

如果使用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 = "[email protected]"
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

回答by Viktor

cut the html source of your signature (for example in an editor or mozilla - source view) and then copy it into a cell. then you can add the value of the cell to your .htmlbody and will be perfect. all other solutions are awful, i tried them all :)

剪切签名的 html 源代码(例如在编辑器或 mozilla - 源代码视图中),然后将其复制到单元格中。然后您可以将单元格的值添加到您的 .htmlbody 中,这将是完美的。所有其他解决方案都很糟糕,我都试过了:)