【问题标题】:Mail range with formatting through vba in excel在excel中通过vba格式化的邮件范围
【发布时间】:2012-06-21 10:29:57
【问题描述】:

网上有很多文章,如何在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

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

感谢您的帮助!

【问题讨论】:

    标签: excel outlook range mailing vba


    【解决方案1】:

    我相信你正在使用 Ron 的RangetoHTML

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

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

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

    这是修改后的代码。

    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
    

    跟进

    快照

    【讨论】:

    • 感谢您的回答。首先,您的代码中有一个小错误(Next Hlink)。我修复它,但结果是特别相同的。超链接不可点击。你的代码对你有用吗?也许 Outlook 应用程序中有一些选项?
    • 是的,它对我有用。尽管您必须按 CTL + Click 才能跟随 hyperling。更新快照
    • 对我来说 CTRL + CLick 不起作用(只有当我切换 bodyformat 以达到文本时(
    • 我已经更新了上面的帖子。我可以确认它在我的电脑上运行。
    • 现在我有了 Siddharth 的电子邮件地址!哇哈哈哈哈哈!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多