vba 在excel中通过vba格式化的邮件范围

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11136214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 16:33:20  来源:igfitidea点击:

Mail range with formatting through vba in excel

excel-vbaoutlookrangemailingvba

提问by AlexSolovyov

there are lot`s of articles in the web, how to pass excel range in the outlook mail. So i am using the following code:

网上有很多文章,如何在outlook邮件中传递excel范围。所以我使用以下代码:

Set OutApp = GetObject(, "Outlook.Application")
If OutApp Is Nothing Then Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
Set rng = Sheets("Report").Range("A3:F" & ThisWorkbook.Worksheets("Report").UsedRange.Rows.Count)

With OutMail
    .HTMLBody = RangetoHTML(rng)   
    .Display
End With

In my range i have table of hyperlinks. And i want them to be clickable in the email body. But they are not. I understand this, because the BodyFormat propherty is olFormatHTML. And if i change the BodyFormat to Plain or RichText, hyperlinks become clickable but all the formatting crashes. How can i save all the formatting and make hyperlinks clickable in the same time?

在我的范围内,我有超链接表。我希望它们可以在电子邮件正文中点击。但他们不是。我理解这一点,因为 BodyFormat 属性是 olFormatHTML。如果我将 BodyFormat 更改为纯文本或富文本,超链接变得可点击,但所有格式都会崩溃。如何保存所有格式并同时使超链接可点击?

Thanks for your help!

谢谢你的帮助!

采纳答案by Siddharth Rout

I believe you are using Ron's RangetoHTML

我相信你正在使用 Ron 的RangetoHTML

If yes then you have to add some extra code to it so that it retains the hyperlinks.

如果是,那么您必须向其添加一些额外的代码,以便它保留超链接。

I have added the code and you can see the same between

我已经添加了代码,你可以看到它们之间的相同

'~~~~~~~~~~~~~~~~~~~~  ADDED THIS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Here is the amended Code.

这是修改后的代码。

Function RangetoHTML(rng As Range)
    ' Changed by Ron de Bruin 28-Oct-2006
    ' Working in Office 2000-2010
    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

    '~~~~~~~~~~~~~~~~~~~~  ADDED THIS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Dim HyperL As Hyperlink
    For Each HyperL In rng.Hyperlinks
        TempWB.Sheets(1).Hyperlinks.Add _
        Anchor:=TempWB.Sheets(1).Range(HyperL.Range.Address), _
        Address:=HyperL.Address, _
        TextToDisplay:=HyperL.TextToDisplay
    Next HyperL
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    '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

FOLLOWUP

跟进

SNAPSHOT

快照

enter image description here

在此处输入图片说明