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

编程入门 行业动态 更新时间:2024-10-27 23:19:37
本文介绍了VBA:发送邮件时更改文本样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用Excel以文本框中的文本作为正文发送电子邮件.这很好用,除了发送邮件时,它仅复制文本的字体大小,而不复制颜色或样式.我做了很多研究,但没有找到任何解决方案.是否有代码允许Excel复制文本框中的文本样式及其内容?这是发送邮件的代码:

I use Excel to send emails using text in a textbox as body. This works fine, except that when sending a mail, it only copies the text's font size, but not its color or style. I did lots of research, but didn't find any solution. Is there a code that allows Excel to copy the style of the text in a textbox as well as its content? Here is the code of sending the mail :

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 = "...@.." .CC = "" .BCC = "" .Subject = Cells(3, 2) .Body = strbody .Send End With Set OutMail = Nothing Set OutApp = Nothing End Sub

我知道这在HTML中是可能的,例如: strbody =< BODY样式= font-size:11pt; font-family:Calibri>早安;< p>我们今天已经完成了我们的主要别名处理.所有指定的公司均已完成.请随时回答任何问题.< p>谢谢.</BODY>" 但是由于我是在文本框中而不是在代码中编写正文,所以我更愿意找到一种解决方案.

I know this is possible in HTML like : 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>" But since I'm writing the body in a textbox instead of in the code, I'd prefer to find a solution.

谢谢.

推荐答案

PasteExcelTable可能是您正在寻找的东西,但是从某种意义上说,Outlook实际上是在使用Word文档编写器,因此它更具隐藏性.您需要添加Word对象引用.

PasteExcelTable is probably what you're looking for, but it's a little more hidden in the sense that Outlook is actually using a Word Document writer. You need to add the Word Object reference.

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

You'll have to modify the rest of your code to insert using the writer instead of .HTMLbody or .body.

还请注意,为了使检查器/写入正常工作,您似乎无法隐藏窗口,但我并未对此进行完全测试.

Also note that for the inspector/write to work it seems that you cannot hide the window, but I did not fully test that.

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 = "...@.." .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:发送邮件时更改文本样式

本文发布于:2023-06-09 04:14:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/595032.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:发送邮件   样式   文本   VBA

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!