在VBA中发送Outlook电子邮件,任何带冒号“:”的主题会导致电子邮件空白

时间:2017-03-10 16:31:51

标签: excel vba outlook

我有一个复制word文档内容的宏,将其粘贴到电子邮件中。然后从Excel中提取电子邮件地址和名称,并向每个人发送带有附件的电子邮件。 (基本上是一个mailmerge)

问题是,只要主题有冒号“:”,电子邮件就会发送为空白。如果我保存电子邮件,也不会显示它,就不会发生这种情况。只有在立即发送时才会发生。

以下是代码:

Option Explicit
Sub SendInitialEmail()
    'directory of email body
    Dim dirEmailBody As String

    ' Directory of email template
    dirEmailBody = _
        "C:\Users\me\Documents\Email Body.docx"

    Dim wordApp As Word.Application
    Dim docEmail As Document

    ' Opens email template and copies it
    Set wordApp = New Word.Application
    Set docEmail = wordApp.Documents.Open(dirEmailBody)
    docEmail.Content.Copy

    Dim outEdit As Document
    Dim outApp As Outlook.Application
    Set outApp = New Outlook.Application
    Dim outMail As MailItem

    ' The names/emails to send too
    Dim sendName As String, sendEmail As String, _
        ccEmail As String, siteName As String

    Dim row As Integer
    ' Was only testing on one row, but generally this pulls from 
    'a sheet of names and email addresses to send an email with attachments too.
    For row = 1 to 1

        sendName = actSheet.Cells(row, 1)
        sendEmail = actSheet.Cells(row, 2)
        ccEmail = actSheet.Cells(row, 3)
        siteName = actSheet.Cells(row, 4)

        Set outMail = outApp.CreateItem(olMailItem)
        With outMail
            .SendUsingAccount = outApp.Session.Accounts.Item(1)
            .To = "myemailaddress to test@gmail.com"
            .BodyFormat = olFormatHTML
            .subject = _
                "Is the error cause of a colon: Email test to me" 
                ' it was

            Set outEdit = .GetInspector.WordEditor
            outEdit.Content.Paste
            outEdit.Range(0).InsertBefore ("Dear " & sendName & "," & vbNewLine)
            ' If I do display, it shows up correctly. 
            ' If I display then send it is fine (workaround)
            .Send
        End With
    Next row

        docEmail.Close
        wordApp.Quit
End Sub

2 个答案:

答案 0 :(得分:1)

删除以下内容:

`.Body = "Dear " & sendName & "," & vbNewLine & docEmail.Content.Text`

并添加:

{{1}}

冒号不是问题。

答案 1 :(得分:0)

尝试使用Chr()命令,在本例中为":"是Chr(58)