【问题标题】:RangeToHTML & Image in CellRangeToHTML 和单元格中的图像
【发布时间】:2019-09-12 14:47:49
【问题描述】:

我正在尝试使用 Ron de Bruin 的 RangeToHTML 生成一封电子邮件,到目前为止它运行良好,但是我的一个单元格(“B26”)包含一张图片,这不会复制到电子邮件中。

我已尝试并成功在范围之前或之后添加图像,但我需要此图像出现在此特定单元格中。如果可能的话,我有什么想法可以让它发挥作用吗?

Sub SendEmail()

    Dim OutlookApp As Outlook.Application
    Dim MItem As Outlook.MailItem
    Dim cell As Range
    Dim Subj As String
    Dim EmailAddr As String
    Dim Recipient As String
    Dim rng As Range
    Dim rng2 As Range
    Dim StrBody As String

    Set rng = Sheets("Email Templates").Range("A1:D29")
    'Set rng2 = Sheets("Email Templates").Range("A6:D32").SpecialCells(xlCellTypeVisible)

    'Create Outlook object
    Set OutlookApp = New Outlook.Application

    'Operations Contacts
    For Each cell In Sheets("Contacts").Columns("A").Cells.SpecialCells(xlCellTypeVisible)
        If cell.Value Like "*@*" Then
            EmailAddr = EmailAddr & ";" & cell.Value
        End If
    Next

    'Systems Contacts
    For Each cell In Sheets("Contacts").Columns("B").Cells.SpecialCells(xlCellTypeVisible)
        If cell.Value Like "*@*" Then
            EmailAddr = EmailAddr & ";" & cell.Value
        End If
    Next

    Subj = "Systems Notification | System Outage | " & Sheets("Email Templates").Range("C6") & " " & Sheets("Email Templates").Range("C4") & " " & Sheets("Email Templates").Range("C6")

    'Create Mail Item and view before sending
    Set MItem = OutlookApp.CreateItem(olMailItem)
    With MItem
        .To = EmailAddr
        .Subject = Subj
        .HTMLBody = RangetoHTML(rng)
        .Display
    End With

End Sub



Function RangetoHTML(rng As Range)
' By Ron de Bruin.
    Dim fso As Object
    Dim ts As Object
    Dim TempFile As String
    Dim TempWB As Workbook
    Dim r As Long

    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).PasteSpecial xlPasteAllUsingSourceTheme, , False, False
        .Cells(1).Select
        Application.CutCopyMode = False
        On Error Resume Next
        .DrawingObjects.Visible = True
        .DrawingObjects.Delete
        On Error GoTo 0
        For r = 1 To rng.Rows.Count
            .Rows(r).RowHeight = rng.Rows(r).RowHeight
        Next r
    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=")
    RangetoHTML = Replace(RangetoHTML, "<!--[if !excel]>&nbsp;&nbsp;<![endif]-->", "")

    '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

【问题讨论】:

    标签: excel vba outlook


    【解决方案1】:

    可以通过截取相关范围的屏幕截图(使用 VBA 代码)来实现(必须在屏幕上可见 ) 然后在 Outlook 中保存并导入该图像..

    这将使您入门。我已经添加了 cmets,所以您理解它应该没有问题。如果您仍然这样做,那么只需询问。

    Option Explicit
    
    Sub SaveRngAsImage()
        Dim flName As String
        Dim ws As Worksheet
        Dim shp As Shape
        Dim objChart As ChartObject
        Dim chrt As Chart
    
        Set ws = ActiveSheet
    
        '~~> Change as applicable
        flName = "C:\Users\routs\Desktop\MyRng.jpg"
    
        '~~> Delete the above image
        If Dir(flName) <> "" Then Kill flName
    
        '~~> Check if what the user selected is a valid range
        If TypeName(Selection) <> "Range" Then
            MsgBox "Select a range first."
            Exit Sub
        End If
    
        '~~> Take a screenshot of the range
        Selection.CopyPicture xlScreen, xlBitmap
        DoEvents
    
        '~~> Paste the screenshot in the worksheet and assign it to
        '~~> a shape object so that we can use it's approx width and
        '~~> Height to create the chart object
        With ws
            .Paste
            DoEvents
            Set shp = .Shapes(.Shapes.Count)
            Set objChart = ActiveSheet.ChartObjects.Add(0, 0, shp.Width, shp.Height)
            Set chrt = objChart.Chart
    
            With chrt
                shp.Copy '~~> Copy the shape (in case the clipboard is cleared)
                .ChartArea.Select
                .Paste
                '~~> Save the image
                .Export ("C:\Users\routs\Desktop\MyRng.jpg")
            End With
            shp.Delete
            objChart.Delete
        End With
    
        '~~> Attaching the above image to outlook email body
        'https://stackoverflow.com/questions/44869790/embed-picture-in-outlook-mail-body-excel-vba
        Dim OutApp As Object
        Dim OutMail As Object
    
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
    
        With OutMail
            .To = "SomeEmail@SomeServer.com"
            .Subject = "Attaching an image"
            .Attachments.Add flName, 1, 0
            .HtmlBody = "<html><p>Dear XYZ</p>" & _
            "<img src=""cid:MyRng.jpg"">"
            .Display
        End With
    End Sub
    

    截图

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-28
      • 2011-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多