VBA:发送邮件时更改文本样式 [英] VBA : Change the style of text when sending a mail

查看:113
本文介绍了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 = "...@...com"
    .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 = "...@...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:发送邮件时更改文本样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆