Auto Outlook Signature gets deleted after inserting Excel Range template in Email body

PremPrakash

New Member
Joined
Sep 28, 2022
Messages
2
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
Hi Experts,

Below is the line of code, I'm using to filter blank cells from a specific column and pasting the same to new tab.

Everything works fine. When an Outlook email opens, I can see my Outlook signature in the email body. There are some HTML lines been added, later once the excel filter table gets added to the email, my signature gets deleted

VBA Code:
Option Explicit
Sub test()
    Dim r As Range
    Dim Mail As Object, Dict As Object, Rng As Range, Email As String, Recipient As String, i As Long
    Sheets("Output").Cells(1).CurrentRegion.Clear
    With Sheets("Data").Cells(1).CurrentRegion
        .Range("b1:c1,e1,h1:i1").Copy Sheets("Output").[a1]
        Set r = .Offset(, .Columns.Count + 1).Range("a1:a2")
        r(2).Formula = "=h2="""""
        .AdvancedFilter 2, r, Sheets("Output").Cells(1).CurrentRegion
        Worksheets("Output").UsedRange.EntireColumn.AutoFit
        Worksheets("Output").UsedRange.EntireRow.AutoFit
        r.Clear
    End With
    
Set Mail = CreateObject("Outlook.Application")
Set Dict = CreateObject("Scripting.Dictionary")
For i = 2 To Cells(Rows.Count, 1).End(xlUp).Row
    Email = Range("E" & i).Value
    Recipient = Split(Email, "@")(0)
    If Not Dict.exists(Email) Then
        Dict.Add Email, ""
        With Cells(1).CurrentRegion
            .AutoFilter 5, Email
            Set Rng = .SpecialCells(12)
            With Mail.CreateItem(0)
                .Display
                .To = Email
                .Subject = "Monthly Audits" & " - " & Format(Date - Day(Date), "mmmm yyyy")
                .HTMLBody = "<p style='font-family:arial;font-size:15'>" & "Hello " & Recipient & "," & "<br>" & "<br>" & "Please find attached the above invoices and backup" & "<br>" & "</p>" _
                & "<p style='font-family:arial;font-size:15'>Any queries please let me know </p>"
                .HTMLBody = .HTMLBody & RangetoHTML(Rng) '& "<p style='font-family:arial;font-size:15'>" & "<br>" & "Regards" & "<br>" & "Prem"
                '.Display
            End With
            .AutoFilter
        End With
    End If
Next i
End Sub
Function RangetoHTML(Rng As Range)
    Dim fso As Object, ts As Object, TempWB As Workbook, TempFile As String
    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 xlPasteAll, , False, False
        Application.CutCopyMode = False
    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 0
    Kill TempFile
    Set ts = Nothing: Set fso = Nothing: Set TempWB = Nothing
End Function

Can you please advise, as where I'm going wrong.

See attached excel for your reference
Copy Blank Rows.xlsb
I
8
Output
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
How about changing your HTMLBody to this?

VBA Code:
.HTMLBody = "<p style='font-family:arial;font-size:15'>" & "Hello " & Recipient & "," & "<br>" & "<br>" & "Please find attached the above invoices and backup" & "<br>" & "</p>" _
               & "<p style='font-family:arial;font-size:15'>Any queries please let me know </p>" & RangetoHTML(Rng) & .HTMLBody

When the email gets created, HTMLBody contains your signature. However, you overwrite HTMLBody completely without including the signature.
 
Upvote 0
Solution
Perfect! I small piece of code adjustment, and this worked.

Thank you! for a help
 
Upvote 0
@PremPrakash: Welcome to the MrExcel Message Board!

Perfect! I small piece of code adjustment, and this worked.

Thank you! for a help

I changed the marked solution post accordingly.

To help future readers, it is better to mark the post as the solution that answered your question. That would be great if you could do the same in your future threads.
No further action is required for this thread.
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top