VBA:发送邮件时更改文本样式

我使用Excel来发送电子邮件作为正文使用文本框中的文本。 这工作正常,除了发送邮件时,它只复制文本的字体大小,而不是它的颜色或样式。 我做了大量的研究,但没有find任何解决scheme。 有没有一个代码,允许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>"
但是由于我在文本框中而不是在代码中编写正文,所以我更愿意find一个解决scheme。

先谢谢你。

PasteExcelTable可能是你正在寻找的东西,但是它实际上使用了一个Word文档编写器,在这个意义上它更隐藏一些。 您需要添加Word对象引用。

您将不得不修改代码的其余部分以插入使用写入器而不是.HTMLbody或.body。

另外请注意,对于检查员/编写工作似乎你不能隐藏窗口,但我没有完全testing。

 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格式的HTML转换器

你只能自己build立这个或在网上find合适的脚本; 与此类似: 将Excel单元格转换为HTML并保留字体格式的macros 。