3

我想在 Excel 中构建\编辑邮件签名:

第一单元:|问候,|
第二单元(姓名):|Asaf Gilad |
第三单元(标题):|PMO |
第 4 格(邮件):|Asaf@mail.com |

这样当我单击发送时,消息的正文将如下所示:

尊敬的先生
...................................
....... 留言内容 ........
...................................
...................................

问候,
阿萨夫·吉拉德    
项目办           
asaf@mail.com

签名也包含图片。

我设法将范围保存为图片并将该图片作为附件发送,但图片在正文中是空的,尽管它已作为附件正确发送。

这是我使用的代码:

公共子导出电子邮件(收件人名称为字符串)
    在错误转到错误时:
    Dim olApp 作为 Outlook.Application
    将 olN 变暗为 Outlook.Namespace
    将 olMail 暗淡为 Outlook.MailItem
    暗淡 strEmailTo 作为字符串,strEmailCC 作为字符串,strEmailBCC 作为字符串
    将 FNAME 调暗为字符串
    将橙色调暗为范围
    Dim oChart 作为图表
    暗淡为图片
    strEmailTo = ""
    strEmailCC = ""
    strEmailBCC = ""
    strEmailTo = "a@a.com"
    strEmailCC = "b@b.com
    如果 strEmailTo "" 那么
        设置 olApp = 新 Outlook.Application
        设置 olNs = olApp.GetNamespace("MAPI")
        olNs.登录
        设置 olMail = olApp.CreateItem(olMailItem)
        olMail.To = strEmailTo
        olMail.CC = strEmailCC
        olMail.BCC = strEmailBCC
        olMail.Subject = "我的主题"
        设置 oRange = Sheets(1).Range("A1:Z100")
        设置oChart = Charts.Add
        oRange.CopyPicture xlScreen, xlPicture
        oChart.Paste
        FNAME = Environ$("temp") & "\testPic.gif"
        oChart.Export Filename:=FNAME, FilterName:="GIF"
        olMail.Attachments.Add FNAME
        olMail.HTMLBody = "" & _
            “”
        olMail.Attachments.Add FNAME
        olMail.Send
    万一
    Application.StatusBar = False
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    杀死 FNAME
    设置 olApp = 无
    设置 olNs = 无
    设置 oRange = 无
    设置 oChart = 无
    设置 oImg = 无
    退出子
呃:
    MsgBox err.Description
结束子
4

2 回答 2

2

这是个好问题,阿萨夫。当我建立自动化电子邮件解决方案时,我发现很难输入签名行。这是可能的,但并不容易。也许它在 2010 年更新,但我还没有检查。

我所做的是将整个正文放入驱动器上的文本文件中,并附上我想要格式化的任何 html 标签。这给了我很大的灵活性,可以制作格式精美的电子邮件,我也可以在其中分配变量。

然后我通过Microsoft Scripting Runtime库访问这些文件。

请参见下面的代码片段:

Option Explicit

Const strEmailBoiler As String = "\\server\path\folder\subfolder\email_text\"

Sub PrepMessage()

Dim strBody As String, strMon As String

strMon = range("Mon").Value
strFY = range("FY").Value
strBody = FileToString(strEmailBoiler, "reports_email_body.txt")

strBody = Replace(strBody, "[MONTH]", strMon)
strBody = Replace(strBody, "[YEAR]", Right(strFY, 2))
strBody = Replace(strBody, "[FILE PATH]", strFileName)

SendMail "firstname.lastname@xyz.com", "Subject Goes Here " & strMon & " YTD", strBody

End Sub

Function FileToString(ByVal strPath As String, ByVal strFile As String) As String
'requires reference to Microsoft Scripting Runtime Object Library (or late binding)

    Dim ts As TextStream

    Set fso = New FileSystemObject
    Set ts = fso.OpenTextFile(strPath & strFile, ForReading, False, TristateUseDefault)

    FileToString = ts.ReadAll
    ts.Close

    Set ts = Nothing
    Set fso = Nothing

End Function

Sub SendMail(strTo As String, strSubject As String, strHTMLBody As String, Optional strAttach As String, Optional strCC As String)
'requires reference to Microsoft Outlook X.X Object Library (or late binding)

Dim olApp As Outlook.Application
Dim olMI As Outlook.MailItem

Set olApp = CreateObject("Outlook.Application")
Set olMI = olApp.CreateItem(olMailItem)

With olMI

    .To = strTo
    .Subject = strSubject
    .HTMLBody = strHTMLBody
    If strAttach <> vbNullString Then .Attachments.Add strAttach
    .Display 'using this because of security access to Outlook
    '.Send

End With

End Sub

然后我的reports_email_body.txt文件将如下所示:

<p>Hello Person,</p> 
<p>The Reports file for [MONTH] FY[YEAR] has been saved in the following location:</p>
<p><a href="[FILE PATH]">[FILE PATH]</a></p>
<p>Best,</p>
<br>
Scott Holtzman
<br>My Address
<br>my title
<br>whatever else...
于 2012-12-17T17:07:41.207 回答
1

在 Excel 2010(可能还有 2007)中,您可以将 .HTMLBody 添加到正文字符串的末尾。例如,使用这样的东西:

    .HTMLBody = "<br>" & strbody & .HTMLBody
                ' <br> is an HTML tag to turn the text into HTML
                ' strbody is your text from cell C9 on the mail tab
                ' .HTMLBody inserts your email signature

这至少可以解决您的签名行问题。

我正在寻找相同问题的解决方案:将范围作为图片插入。

于 2014-11-13T20:58:46.143 回答