【问题标题】:Keep the format of a text box in excel and insert it in an email body在excel中保留文本框的格式并将其插入到电子邮件正文中
【发布时间】:2021-12-03 09:33:04
【问题描述】:

我尝试创建一个 vba 代码来帮助我向不同的客户发送电子邮件。

我将所有电子邮件正文放在具有特定格式的 Excel 文本框中。我现在遇到的问题是,当电子邮件正文插入电子邮件时,正文格式丢失了。

问题:如何保持文本框的格式?

Sub test_email_template()
    Dim name, email, body, subject, copy, place, business As String
    
    
    name = Range("B4").Value
    email = Range("C4").Value
    body = Format(ActiveSheet.TextBoxes("TextBox 1").Text)
    subject = " Payment Summary Reports"
    copy = Range("D2").Value
   
'replace name'
body = Replace(body, "Email Title", name)


Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    With OutMail
    
         .to = email
         .cc = copy
         .subject = subject
         .body = body
         
         .display
        
    End With
    Set OutMail = Nothing
    Set OutApp = Nothing
    MsgBox "Email(s) Sent!"
End Sub

下面是我要保留的文本框格式

【问题讨论】:

  • I put all email body in a text box in excel with specific formatting.具体是什么格式?

标签: excel vba email textbox


【解决方案1】:

您需要使用.htmlbody。这个想法是用 html 在 excel 单元格中写你的消息,例如:

<p><font font face="Arial">Dear Email Title</p>
<p><font font face="Arial">Attached please find your up to date Payment Summary Reports.</p>
<p><font font face="Arial">Kind regards</p>
<p><font font face="Arial">John Johns</p>

只需将其作为复制/粘贴正文放入 excel 单元格中,然后进行替换并将此文本放入 .htmlbody = body

【讨论】:

    【解决方案2】:

    您可以使用以下代码复制带有格式的文本。当然,您需要更改此代码以满足您的要求。

    xlSheet.Range("C1:C2").copy 是将单元格文本复制到富文本格式的剪贴板的重要代码。

    你可以拥有

    • 单元格C1 中的称呼(交易电子邮件标题)作为公式,以便可以使用公式进行更新。
    • 正文的其余部分在单元格C2 中采用富文本格式

    参考:Copying clipboard content into outlook mail item using VBA

    Dim OutApp As Object
    Dim OutMail As Object
    Dim olInsp As Object
    Dim xlSheet As Worksheet
    Dim wdDoc As Object
    Dim oRng As Object
        Set xlSheet = ActiveWorkbook.Sheets("Sheet1")
        xlSheet.Range("C1:C2").copy
    
        On Error Resume Next
        Set OutApp = GetObject(, "Outlook.Application")
        If Err <> 0 Then Set OutApp = CreateObject("Outlook.Application")
        On Error GoTo 0
        
        Set OutMail = OutApp.CreateItem(0)
        With OutMail
            .BodyFormat = 3
            .To = ""
            .CC = ""
            .BCC = ""
            .subject = "Your Subject here"
            Set olInsp = .GetInspector
            Set wdDoc = olInsp.WordEditor
            Set oRng = wdDoc.Range
            oRng.collapse 1
            oRng.Paste
            .Display
        End With
       
     
        Set OutMail = Nothing
        Set OutApp = Nothing
        Set olInsp = Nothing
        Set wdDoc = Nothing
        Set oRng = Nothing
        Set OutMail = Nothing
        Set OutApp = Nothing
        'MsgBox "Email(s) Sent!"
    

    【讨论】:

    • 非常感谢甘古拉!效果很好!!!
    猜你喜欢
    • 2020-11-09
    • 2017-12-11
    • 1970-01-01
    • 1970-01-01
    • 2015-10-02
    • 2021-09-28
    • 1970-01-01
    • 1970-01-01
    • 2011-06-26
    相关资源
    最近更新 更多