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

时间:2017-03-20 09:26:05

标签: excel vba excel-vba excel-2007 outlook-2007

我使用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>"
但由于我是在文本框中而不是在代码中编写正文,我更愿意找到解决方案。

提前谢谢。

2 个答案:

答案 0 :(得分:2)

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

您必须修改代码的其余部分以使用编写器而不是.HTMLbody或.body进行插入。

另请注意,对于检查员/写作工作,您似乎无法隐藏窗口,但我没有完全测试。

args

答案 1 :(得分:1)

首先,我不知道Excel中的任何默认/内置方式来执行此操作。您需要的是HTML转换器的 Excel单元格格式

您只能自己构建或在线查找合适的脚本;与此相似: Macro to Convert Excel Cell to HTML and preserve font formatting