VBA Excel到Outlook字体格式 [英] VBA Excel to Outlook font formatting

查看:119
本文介绍了VBA Excel到Outlook字体格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在为我的工作进行过程自动化.基本上,宏会分析数据并创建包含结果的电子邮件.

I am currently working on a process automation for my work. Basically, the macro analyses the data and creates an email with results.

除了我完全不知道如何将字体格式实现到Outlook的电子邮件正文中之外,一切工作都很好.我无法将字体格式设置为恒定值,因为每行的格式各不相同,并且取决于工作表中的excel公式和条件格式.

Everything works perfectly fine, besides the fact I have absolutely no clue how to implement my font formatting into Outlook's email body. I cannot set font formatting to constant value as each row's formatting varies and is depended on excel formulas and conditional formattings in my worksheet.

创建HTML正文的函数如下:

The function which creates the HTML body looks as below:

            Private Function BodyBuilder(wsA As Worksheet) As String
            Dim html As String
            Dim sTTo As String
            Dim rngSN As Range
            Dim cl As Range
            Dim sName As String


            Set OutSh = ThisWorkbook.Sheets("Output")

               sTTo = Comm.Cells(5, 4)

               sName = OutSh.Cells(3, 2)

               html = "Dear " & sTTo & ","

            Set rngSN = OutSh.Range("a4:a11")

            html = html & "<br><br><table><tr><td><strong>" & "Detailed risk report for selected company - " & sName & "</strong></td></tr>"

            **For Each cl In rngSN
               html = html & "<tr><td>" & Format(cl.Value, cl.NumberFormat) & "</td><td>" & cl.Offset(, 1).Value & "</td></tr>"
            Next cl**


            html = html & "</table>" & "<br>" & "Kind regards"

            BodyBuilder = html
            End Function

基本上,这封电子邮件看起来非常不错,除了我想查看其字体格式(如果需要和颜色加粗)的值.

Basically, the email looks perfectly fine besides that I would like to see my values with it's font formats (bolded if needed and colors).

有人可以帮忙吗?

谢谢

推荐答案

如果您的目标是在功能上能够将Excel中的某些数据复制-粘贴"到Outlook电子邮件中,同时保留所有格式,则可能最简单的方法就是使用Ron de Bruin的将Excel范围转换为HTML的函数.我检查了一下,看来它甚至保留了条件格式中的格式.

If your goal is to functionally be able to "copy-paste" some data from Excel into an Outlook e-mail while retaining all the formatting, it would probably be easiest to just use Ron de Bruin's function that converts an Excel range into HTML. I checked and this seems like it even retains formatting from conditional formatting.

源网站.

然后,您只需将数据转换为HTML即可放入body标签,例如

Then you can just get your data converted to HTML to put in the body tag like

html = html & RangeToHTML(OutSh.Range("a4:a11"))

(有关功能的重新发布)

(Repost of the function in question)

Function RangetoHTML(rng As Range)
' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-2016
    Dim fso As Object
    Dim ts As Object
    Dim TempFile As String
    Dim TempWB As Workbook

    TempFile = Environ$("temp") & "\" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"

    'Copy the range and create a new workbook to past the data in
    rng.Copy
    Set TempWB = Workbooks.Add(1)
    With TempWB.Sheets(1)
        .Cells(1).PasteSpecial Paste:=8
        .Cells(1).PasteSpecial xlPasteValues, , False, False
        .Cells(1).PasteSpecial xlPasteFormats, , False, False
        .Cells(1).Select
        Application.CutCopyMode = False
        On Error Resume Next
        .DrawingObjects.Visible = True
        .DrawingObjects.Delete
        On Error GoTo 0
    End With

    'Publish the sheet to a htm file
    With TempWB.PublishObjects.Add( _
         SourceType:=xlSourceRange, _
         Filename:=TempFile, _
         Sheet:=TempWB.Sheets(1).Name, _
         Source:=TempWB.Sheets(1).UsedRange.Address, _
         HtmlType:=xlHtmlStatic)
        .Publish (True)
    End With

    'Read all data from the htm file into RangetoHTML
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
    RangetoHTML = ts.readall
    ts.Close
    RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
                          "align=left x:publishsource=")

    'Close TempWB
    TempWB.Close savechanges:=False

    'Delete the htm file we used in this function
    Kill TempFile

    Set ts = Nothing
    Set fso = Nothing
    Set TempWB = Nothing
End Function

这篇关于VBA Excel到Outlook字体格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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