在VBA中,我怎样才能发送带有签名和图像的电子邮件而没有直接path?

我正在寻找发送邮件,其中包含与发件人的默认签名(其中包含一个图像)的Excel文档的范围。 我正在使用似乎是去的方法,如下所示。 本来,我不能让电子邮件出现,而不改变.htm文件的图像src直接链接到相关的图像。 这工作得很好,但问题是我需要一个不需要直接path的解决scheme。 在工作中,我们的计算机将在启动时更新签名文件夹,并覆盖我对其做出的任何更改。 我必须使这个程序适应任何工作电脑,所以我不能具体更改每个img srcpath。 如果有人知道允许访问完整签名而没有直接path或任何其他解决方法,我将不胜感激。 谢谢。

.htm文件表示签名并引用其源中的图像。 它看起来像<img border=0 width=240 height=148 src="MYCOMPANY%20Signature_files/image001.png" v:shapes="Picture_x0020_1"></span><![endif]></span></a><span style='font-size:8.0pt;mso-bidi-font-size:11.0pt;font-family:"Arial",sans-serif; color:#A1A0A4'> <img border=0 width=240 height=148 src="MYCOMPANY%20Signature_files/image001.png" v:shapes="Picture_x0020_1"></span><![endif]></span></a><span style='font-size:8.0pt;mso-bidi-font-size:11.0pt;font-family:"Arial",sans-serif; color:#A1A0A4'> 。 我能够将src =直接更改为计算机上图像的path,但是由于被覆盖,我无法将其用作解决scheme。

  Dim OutApp As Object Dim OutMail As Object With Application .EnableEvents = False .ScreenUpdating = False End With Set OutApp = CreateObject("Outlook.Application") Set OutMail = OutApp.CreateItem(0) a = customerContact b = salesExec Dim Ebody As String Ebody = "placeholder" Ebody = Cells(3, 2) & "<br>" _ & "<br>" _ & "Dear, " & customerFirstName & "<br>" _ & "<br>" _ & Cells(7, 2) & "<br>" _ & "<br>" _ & Cells(9, 2) & "<br>" _ & "<br>" _ & Cells(11, 2) & "<br>" _ & "<br>" _ & Cells(13, 2) Signature = Environ("appdata") & "\Microsoft\Signatures\" If Dir(Signature, vbDirectory) <> vbNullString Then Signature = Signature & Dir$(Signature & "*.htm") Else: Signature = "" End If Signature = CreateObject("Scripting.FileSystemObject").GetFile(Signature).OpenAsTextStream(1, -2).ReadAll On Error Resume Next With OutMail .To = customerContact .CC = "" .BCC = salesExec .Subject = "Welcome" ' In place of the following statement, you can use ".Display" to ' display the e-mail message. 'or if you dont want it to auto send.....change .send to .display .HTMLBody = "<body style='font-family:calibri;font-size:11pt'>" _ & Ebody _ & "<br>" _ & "<br>" & Signature .display End With On Error GoTo 0 With Application .EnableEvents = True .ScreenUpdating = True End With Set OutMail = Nothing Set OutApp = Nothing 

好,所以我认为这应该是可能的parsingHTML并在运行时操作它,但是这给了我比预期更多的麻烦,但如果我理解正确,你只需要插入签名默认情况下,我认为你在做什么是造成一个问题,因为你在Outlook之前操纵.HTMLBody能够做到这一点。 如果编辑HTMLBody 之前调用MailItem.DisplayMailItem.GetInspector Outlook将添加用户的默认签名。

这是一个简单的例子:

 Sub foo() Dim Signature$ Dim olApp As Object Dim olMail As Object Set olApp = GetObject(, "Outlook.Application") Set olMail = olApp.CreateItem(0) olMail.GetInspector Signature = olMail.HTMLBody olMail.HTMLBody = "<body style='font-family:calibri;font-size:11pt'>blah blah blah" _ & "<br>" _ & "<br>" & Signature olMail.display '## Verify you can see the signature End Sub 

尝试这个。 想法是调用.GetInspector (它应该正确插入签名),然后捕获Signature = .HTMLBody (稍后添加到电子邮件的末尾),添加自定义HTML,然后附加Signature

 With OutMail .GetInspector ' ## This inserts default signature Signature = .HTMLBody ' ## Capture the signature HTML .To = customerContact .CC = "" .BCC = salesExec .Subject = "Welcome" ' In place of the following statement, you can use ".Display" to ' display the e-mail message. 'or if you dont want it to auto send.....change .send to .display .HTMLBody = "<body style='font-family:calibri;font-size:11pt'>" _ & Ebody _ & "<br>" _ & Signature .display End With 

怎么运行的:

当您调用MailItem.Display (这会导致邮件显示在屏幕上)时,或者当您访问MailItem.GetInspector属性时,Outlook将签名添加到新的未修改的邮件(您不应该修改主体)

完整的代码可以省略之前与Signature和FSO等的交易。

 Dim OutApp As Object Dim OutMail As Object With Application .EnableEvents = False .ScreenUpdating = False End With Set OutApp = CreateObject("Outlook.Application") Set OutMail = OutApp.CreateItem(0) a = customerContact b = salesExec Dim Ebody As String Ebody = "placeholder" Ebody = Cells(3, 2) & "<br>" _ & "<br>" _ & "Dear, " & customerFirstName & "<br>" _ & "<br>" _ & Cells(7, 2) & "<br>" _ & "<br>" _ & Cells(9, 2) & "<br>" _ & "<br>" _ & Cells(11, 2) & "<br>" _ & "<br>" _ & Cells(13, 2) '!---- NO LONGER NEEDED ----!> 'Signature = Environ("appdata") & "\Microsoft\Signatures\" 'If Dir(Signature, vbDirectory) <> vbNullString Then ' Signature = Signature & Dir$(Signature & "*.htm") 'Else: ' Signature = "" 'End If ' Signature = CreateObject("Scripting.FileSystemObject").GetFile(Signature).OpenAsTextStream(1, -2).ReadAll '<!-------------------------!> With OutMail .GetInspector ' ## This inserts default signature Signature = .HTMLBody ' ## Capture the signature HTML .To = customerContact .CC = "" .BCC = salesExec .Subject = "Welcome" ' In place of the following statement, you can use ".Display" to ' display the e-mail message. 'or if you dont want it to auto send.....change .send to .display .HTMLBody = "<body style='font-family:calibri;font-size:11pt'>" _ & Ebody _ & "<br>" _ & Signature .display End With On Error GoTo 0 With Application .EnableEvents = True .ScreenUpdating = True End With Set OutMail = Nothing Set OutApp = Nothing 

我已经找出了一个似乎没问题的解决scheme。 我没有编辑.htm文件,而是从.htmpath中提取图像并将其粘贴到.HTML主体上。

你可以将签名和图像移动到你的需求,我只是想确保它的工作。

实质上,修改只适用于代码的With OutMail部分,它看起来像这样(但我删除了公司相关的东西):

 With OutMail .To = customerContact .CC = "" .BCC = salesExec .Subject = "Welcome" ' In place of the following statement, you can use ".Display" to ' display the e-mail message. 'or if you dont want it to auto send.....change .send to .display .HTMLBody = "<body style='font-family:calibri;font-size:11pt'>" _ & Ebody _ & "<br>" _ & "<img src='c:\Users\<YOUR USERNAME>\AppData\Roaming\Microsoft\Signatures\<SIGNATURE FILES FOLDER>\image001.png'>" _ & "<br>" & Signature .display End With