'VBA Publishing temporary HTML file using RangetoHTML, run time error 1004

I am running into a run time error 1004 when I'm using the following code. My Code used to work in the beginning. Now I don't know why it stopped working. When the debug the error it shows that the following lines cause the error:

    '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

This is my complete Code. It's supposed the automatically create an msg-file in outlook:

Sub EmailSenden()

    Dim rng As Range
    Dim OutApp As Object
    Dim OutMail As Object
    Dim sTemplate As String
    Dim Folder As String
    Dim Filename As String

    Set rng = Sheets("Tabelle1").Range("A1:D5").SpecialCells(xlCellTypeVisible)
    
    'sTemplate = Sheets("E-Mail Text").Shapes(1).TextFrame2.TextRange.Text
    
    sTemplate = Worksheets("E-Mail Text").Cells(2, 1).Value
    
    Folder = Worksheets("ARF File Path").Cells(1, 2).Value
    If Left$(Folder, 1) <> "\" Then Folder = Folder & "\"

    With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    sHTML2 = Replace(sTemplate, "[@Time]", Format(Now, "hh:mm") & " Uhr")
    sHTML2 = Replace(sHTML2, "[@SamplesToWhichTargetLab]", Worksheets("E-Mail Text").Cells(3, 1).Value)
    sHTML2 = Replace(sHTML2, Chr(10), "<br>")
    sHTML2 = Replace(sHTML2, "[@SampleTable]", RangetoHTML(rng))

        
    '------------------------------------------------------------------------------
    
    With OutMail
        .To = Worksheets("To").Cells(1, 2).Value
        .CC = Worksheets("CC").Cells(1, 2).Value
        .Subject = "Daily Carrier " & Format(Date, "dd.mm.yyyy")
        .HTMLBody = sHTML2
        .Display

        Filename = Dir$(Folder & "*")

        Do Until Filename = vbNullString

            Call .Attachments.Add(Folder & Filename)

            Filename = Dir$

        Loop
    End With

    '------------------------------------------------------------------------------

    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With

    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub


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

Thanks for your help :-)



Solution 1:[1]

There is no need to save a temporarily file on the disk. You can copy the required range in Excel and using the Word object model (see the WordEditor property) you can paste the copied data into the message body preserving the markup.


The Outlook object model supports three main ways of customizing the message body:

  1. The Body property returns or sets a string representing the clear-text body of the Outlook item.
  2. The HTMLBody property of the MailItem class returns or sets a string representing the HTML body of the specified item. Setting the HTMLBody property will always update the Body property immediately. For example:
     Sub CreateHTMLMail() 
       'Creates a new e-mail item and modifies its properties. 
       Dim objMail As Outlook.MailItem 
       'Create e-mail item 
       Set objMail = Application.CreateItem(olMailItem) 
       With objMail 
        'Set body format to HTML 
        .BodyFormat = olFormatHTML 
        .HTMLBody = "<HTML><BODY>Enter the message <a href="http://google.com">text</a> here. </BODY></HTML>" 
        .Display 
       End With 
     End Sub
  1. The Word object model can be used for dealing with message bodies. See Chapter 17: Working with Item Bodies for more information.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Eugene Astafiev