我試圖創建一個 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
下面是我想保留的文本框格式

uj5u.com熱心網友回復:
你需要使用.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
uj5u.com熱心網友回復:
您可以使用以下代碼復制帶格式的文本。當然,您需要對此代碼進行更改以滿足您的要求。
但是xlSheet.Range("C1:C2").copy是將單元格文本以富文本格式復制到剪貼板的重要代碼。
你可以擁有
- 單元格
C1中的稱呼(交易電子郵件標題)作為公式,以便可以使用公式進行更新。 - 正文的其余部分在單元格中具有富文本格式
C2
參考:使用 VBA 將剪貼板內容復制到 Outlook 郵件項中
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!"
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/321481.html
上一篇:具有各種標準的中位數
