我使用 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>"
但由于我是在文本框中而不是在代码中编写正文,所以我更愿意找到解决方案。先感谢您。
最佳答案
PasteExcelTable 可能是您要查找的内容,但从 Outlook 实际使用 Word 文档编写器的意义上说,它有点隐藏。您需要添加 Word 对象引用。
您必须修改其余代码以使用 writer 而不是 .HTMLbody 或 .body 插入。
另请注意,对于检查员/写入工作,您似乎无法隐藏窗口,但我没有完全测试。
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
关于VBA : Change the style of text when sending a mail,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42899865/