VBA : 发送邮件时更改文本样式
VBA : Change the style of text when sending a mail
我使用 Excel 发送电子邮件,使用文本框中的文本作为正文。这工作正常,除了在发送邮件时,它只复制文本的字体大小,而不是它的颜色或样式。我做了很多研究,但没有找到任何解决方案。是否有代码允许 Excel 复制文本框中文本的样式及其内容?这是发送邮件的代码:
Sub SendMail()
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Dim strbody As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(olMailItem)
strbody = ThisWorkbook.Sheets("Mail").Shapes("txt").DrawingObject.Text
'I named the textbox "txt" in the worksheet
'On Error Resume Next
With OutMail
.To = "...@...com"
.CC = ""
.BCC = ""
.Subject = Cells(3, 2)
.Body = strbody
.Send
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
我知道这在 HTML 中是可能的,比如 :
strbody = "<BODY style=font-size:11pt;font-family:Calibri>Good Morning;<p>We have completed our main aliasing process for today. All assigned firms are complete. Please feel free to respond with any questions.<p>Thank you.</BODY>"
但是由于我是在文本框中而不是在代码中编写正文,所以我更愿意找到解决方案。
提前致谢。
首先,我不知道 Excel 中有任何 default/built-in 方法可以做到这一点。您需要的是 Excel 单元格格式到 HTML 转换器.
只能自己搭建或者网上找合适的脚本;类似于这个:
Macro to Convert Excel Cell to HTML and preserve font formatting.
PasteExcelTable 可能是您要查找的内容,但从 Outlook 实际上使用 Word 文档编写器的意义上来说,它更隐蔽一些。您需要添加 Word 对象引用。
您必须修改其余代码以使用编写器而不是 .HTMLbody 或 .body 进行插入。
另请注意,要使 inspector/write 正常工作,您似乎无法隐藏 window,但我没有对其进行全面测试。
Sub SendEmail()
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Dim strbody As String
Dim olInsp As Outlook.Inspector
Dim document As Word.document
Dim oRng As Excel.Range
Set OutApp = New Outlook.Application
Set OutMail = OutApp.CreateItem(olMailItem)
With OutMail
.To = "...@...com"
.CC = ""
.BCC = ""
.Subject = Cells(3, 2)
.Display
Set olInsp = .GetInspector
If olInsp.IsWordMail And olInsp.EditorType = olEditorWord Then
Set document = olInsp.WordEditor
Set oRng = Range("A1:B2") ' The range you wish to copy into the document
oRng.Copy ' Loads info to clipboard
' Write the range into the first paragragh of the word document.
document.Paragraphs(1).Range.PasteExcelTable False, False, True
' Example how to write to the end of the email.
Dim p As Word.Paragraph
Set p = document.Paragraphs.Add
p.Range.Text = "test"
End If
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
我使用 Excel 发送电子邮件,使用文本框中的文本作为正文。这工作正常,除了在发送邮件时,它只复制文本的字体大小,而不是它的颜色或样式。我做了很多研究,但没有找到任何解决方案。是否有代码允许 Excel 复制文本框中文本的样式及其内容?这是发送邮件的代码:
Sub SendMail()
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Dim strbody As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(olMailItem)
strbody = ThisWorkbook.Sheets("Mail").Shapes("txt").DrawingObject.Text
'I named the textbox "txt" in the worksheet
'On Error Resume Next
With OutMail
.To = "...@...com"
.CC = ""
.BCC = ""
.Subject = Cells(3, 2)
.Body = strbody
.Send
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
我知道这在 HTML 中是可能的,比如 :
strbody = "<BODY style=font-size:11pt;font-family:Calibri>Good Morning;<p>We have completed our main aliasing process for today. All assigned firms are complete. Please feel free to respond with any questions.<p>Thank you.</BODY>"
但是由于我是在文本框中而不是在代码中编写正文,所以我更愿意找到解决方案。
提前致谢。
首先,我不知道 Excel 中有任何 default/built-in 方法可以做到这一点。您需要的是 Excel 单元格格式到 HTML 转换器.
只能自己搭建或者网上找合适的脚本;类似于这个: Macro to Convert Excel Cell to HTML and preserve font formatting.
PasteExcelTable 可能是您要查找的内容,但从 Outlook 实际上使用 Word 文档编写器的意义上来说,它更隐蔽一些。您需要添加 Word 对象引用。
您必须修改其余代码以使用编写器而不是 .HTMLbody 或 .body 进行插入。
另请注意,要使 inspector/write 正常工作,您似乎无法隐藏 window,但我没有对其进行全面测试。
Sub SendEmail()
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Dim strbody As String
Dim olInsp As Outlook.Inspector
Dim document As Word.document
Dim oRng As Excel.Range
Set OutApp = New Outlook.Application
Set OutMail = OutApp.CreateItem(olMailItem)
With OutMail
.To = "...@...com"
.CC = ""
.BCC = ""
.Subject = Cells(3, 2)
.Display
Set olInsp = .GetInspector
If olInsp.IsWordMail And olInsp.EditorType = olEditorWord Then
Set document = olInsp.WordEditor
Set oRng = Range("A1:B2") ' The range you wish to copy into the document
oRng.Copy ' Loads info to clipboard
' Write the range into the first paragragh of the word document.
document.Paragraphs(1).Range.PasteExcelTable False, False, True
' Example how to write to the end of the email.
Dim p As Word.Paragraph
Set p = document.Paragraphs.Add
p.Range.Text = "test"
End If
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub