Send e-mails from excel VBA - problem with signature, insert excel table in sent e-mail

Paulika

New Member
Joined
Feb 16, 2016
Messages
2
Hi all,

I have a little code that I am trying to make it work but I am now stuck. I am obviously very new with VBA so I grabbed a few pieces of code from various places. Currently my code can create an e-mail (.Display or .Send) it can put the subject that I need, send from a specific Outlook Account and add the e-mail addresses that I want but I need it to do the following:

a. Add a signature (default signature) - it has some logos with hyperlinks in it - The problem here is after i add "Signature = ObjMail.HTMLBody" the text from body dissapears. I can only assume that when I add the HTMLbody for the signature it changes everything. If i do not add the signature this way then the signature doesn't display the logo but it shows HYPERLINK "link...." and I can't have that happening.
b. Add a table from excel (even if it's copy / paste the cells from an excel sheet and then I manually set the background colours etc). - This is didn't yet try but I didn't find anything about it...

Here is the code:

Sub Mail_To_SP()
'Working in Excel 2000-2016
Dim OutApp As Object
Dim OutMail As Outlook.MailItem, Signature As String
Dim strbody As String

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

With OutMail
.Display
End With
Signature = OutMail.body

strbody = "Hello," & vbNewLine & vbNewLine & _
"text text" & vbNewLine & _
" " & vbNewLine & _
"text text text" & vbNewLine & _
" " & vbNewLine & _
"__" & vbNewLine & _
"__" & vbNewLine & _
" " & vbNewLine & _
"Thank you."
On Error Resume Next
With OutMail
.To = Range("AA1").Value
.CC = ""
.BCC = ""
.Subject = "text text " & Range("C26") & " " & "-text text " & Range("C36").Value & " " & Range("D36") & " " & Range("F36") & " " & "-" & " " & Range("S4").Value & " text " & "text " & Range("J26") & " at " & Range("H26") & ":" & Range("I26")
.body = strbody & vbNewLine & Signature = ObjMail.HTMLBody
'You can add a file like this
'.Attachments.Add ("C:\test.txt")
.SendUsingAccount = OutApp.Session.Accounts.Item(3)
.Display 'or use .Send
End With
On Error GoTo 0


Set OutMail = Nothing
Set OutApp = Nothing
End Sub
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Hi everyone,

I found a better code from Ron. It adds the table that I wanted but I still can't add the body and the signature doesn't display properly. I kinda know what is wrong but this doesn't mean I can fix it. I believe I have a code for plain text but then I am trying to add the signature and table in html format so maybe everything has to be written for html? Here is what I have now:

Sub Mail_Selection_Range_Outlook_Body()
' You need to use this module with the RangetoHTML subroutine.
' Works in Excel 2000, Excel 2002, Excel 2003, Excel 2007, Excel 2010, Outlook 2000, Outlook 2002, Outlook 2003, Outlook 2007, and Outlook 2010.
Dim rng As Range
Dim OutApp As Object
Dim OutMail As Outlook.MailItem, Signature As String
Dim strbody As String

Set rng = Nothing
On Error Resume Next
' Only send the visible cells in the selection.
Set rng = Sheets("Load").Range("A2:P3").SpecialCells(xlCellTypeVisible)
' You can also use a range with the following statement.
' Set rng = Sheets("YourSheet").Range("D4:D12").SpecialCells(xlCellTypeVisible)
On Error GoTo 0


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
.Display
End With
Signature = OutMail.body

strbody = "Hello Team," & vbNewLine & vbNewLine & _
"I hope my e-mail finds you well." & vbNewLine & _
" " & vbNewLine & _
"Kindly confirm the below new booking:" & vbNewLine & _
" " & vbNewLine & _
"__" & vbNewLine & _
"__" & vbNewLine & _
" " & vbNewLine & _
"Thank you."



With OutMail
.To = Range("AA1").Value
.CC = ""
.BCC = ""
.Subject = "New Booking Request in " & Range("C26") & " " & "- Meet and Assist with VAT Reclaim Assistance for " & Range("C36").Value & " " & Range("D36") & " " & Range("F36") & " " & "-" & " " & Range("S4").Value & " Service " & "on " & Range("J26") & " at " & Range("H26") & ":" & Range("I26")
.HTMLBody = strbody & vbNewLine & RangetoHTML(rng) & Signature = ObjMail.HTMLBody
'You can add a file like this
'.Attachments.Add ("C:\test.txt")
.SendUsingAccount = OutApp.Session.Accounts.Item(3)
.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)
' Works in Excel 2000, Excel 2002, Excel 2003, Excel 2007, Excel 2010, Outlook 2000, Outlook 2002, Outlook 2003, Outlook 2007, and Outlook 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 workbook to receive the data.
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 an .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 the RangetoHTML subroutine.
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.
Kill TempFile

Set ts = Nothing
Set fso = Nothing
Set TempWB = Nothing
End Function

Thank you.
 
Upvote 0

Forum statistics

Threads
1,223,956
Messages
6,175,612
Members
452,660
Latest member
Zatman

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