繁体   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