简体   繁体   中英

Send email from Excel, copy all values, formats and pictures into body text of email

Hi I have a worksheet that I need to send as an email, including the image (our company logo), it is being sent externally so has to have the image. I have tried a few different things but nothing seems to work. The best I have come up with is the below (my function works fine for the cell values but adding the image is just not working) but this has 2 issues, the first is that it only opens the dialogue box to choose the image file, the second is that the picture is inserted as a link so it can't be displayed on the email.

Dim fNameAndPath As Variant
Dim img As Picture
fNameAndPath = Application.GetOpenFilename(Title:="filepath\name.extension")
If fNameAndPath = False Then Exit Sub
    Set img = ActiveSheet.Pictures.Insert(fNameAndPath)
    With img
    .Left = ActiveSheet.Range("D2").Left
    .Top = ActiveSheet.Range("D2").Top
    .Width = ActiveSheet.Range("D2:H2").Width
    .Height = ActiveSheet.Range("D2:D6").Height
    .Placement = 1
    .PrintObject = True
End With

I have pasted the whole sub and function below in case there is something else I can change elsewhere to make this happen, I use this code in another workbook to create an email from a workbook with no image and it works great so I'm pretty sure the problem is the picture. I have changed the 'TempWB.Close savechanges' code to True and removed the 'Kill TempFile' instructin so I can check that the new file has the image which it does, so the issue lays in the Excel to Outlook part of the function.

Private Sub CommandButton1_Click()
Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object
Set rng = Nothing
Set rng = ActiveSheet.Range("B2:L67").SpecialCells(xlCellTypeVisible)
If rng Is Nothing Then
    MsgBox "The selection is not a range or the sheet is protected. " & _
           vbNewLine & "Please correct and try again.", vbOKOnly
    Exit Sub
End If
With Application
    .EnableEvents = False
    .ScreenUpdating = False
End With
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail
    .Subject = ActiveSheet.Range("R3")
    .HTMLBody = RangetoHTML(rng)
    .Display
End With
On Error GoTo 0
With Application
    .EnableEvents = True
    .ScreenUpdating = True
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
Function RangetoHTML(rng As Range)
    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"
    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 GoTo 0
    End With
    Dim fNameAndPath As Variant
    Dim img As Picture
    fNameAndPath = Application.GetOpenFilename(Title:="filenameandpath")
    If fNameAndPath = False Then Exit Sub
        Set img = ActiveSheet.Pictures.Insert(fNameAndPath)
        With img
        .Left = ActiveSheet.Range("D2").Left
        .Top = ActiveSheet.Range("D2").Top
        .Width = ActiveSheet.Range("D2:H2").Width
        .Height = ActiveSheet.Range("D2:D6").Height
        .Placement = 1
        .PrintObject = True
    End With
     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
    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=")

    TempWB.Close savechanges:=False
    Kill TempFile
    Set ts = Nothing
    Set fso = Nothing
    Set TempWB = Nothing
End Function

I tried to add LinkToFile:=msoFalse in the hope that would insert the image rather than a link to the file but I get a syntax error no matter where I put it! I've also tried all the suggestions in How to insert a picture into Excel at a specified cell position with VBA but most of those given me an error too (run-time error 424, object required - the debugger always highlights the line with the file path).

I think I found the answer here: Embed picture in outlook mail body excel vba

I amended your code as follows:

With OutMail
    .Subject = ActiveSheet.Range("R3")
    .HTMLBody = RangetoHTML(rng) & "<img src=""cid:LAN.png""height=520 width=750>"
    .attachments.Add "C:\Users\Pictures\LAN.png", 1, 0
    .Display
End With

The function broke for me because

If fNameAndPath = False Then Exit Sub

Should be Then End and not Then Exit

Using this should remedy needing to select the file:

fNameAndPath = "C:\Users\Pictures\LAN.png"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM