VBA Outlook E-mail w/ Text & Attachment

mst3kr

New Member
Joined
Apr 15, 2013
Messages
46
Office Version
  1. 2019
  2. 2010
  3. 2007
Platform
  1. Windows
Hello. I need some assistance in correcting, improving, simplifying, streamlining, etc., the Mail_Outlook macro found below. This entirety is two macros and one 'Function' macro that I’ve cobbled together from parts found online and some pieces of macros I already had. I think it can be slimmed down a bit, but I haven't been successful in doing it. In the end, the Excel file I'm working on is a multi-tab sales report that, once updated, is saved & then E-mailed out to a large list of people (45+) every day; I want to automate the distribution of the report. Thus, the VBA.

In its current [ugly] state, the macros works fine except for one part:

With Sheets("Email Summary")
Set rngBody = .Range("A1:G16")
rngBody.Copy
End With

This should pull from a particular tab within the file, copy the particular range, and then paste the contents into the body of the E-mail. I’ve used it in other macros and it works, but here it does not and I'm not sure why (based on my novice knowledge of VBA).

In essence, what the E-mail should look like is:

To: Populated by the ‘With Sheets’ portion (which works just fine).
Subject: Sales (which populates fine from the ‘With Mail’ portion).

E-mail Body:
Good Morning,

Attached is the today’s sales report. Please advise with any questions.

Then the content of:

With Sheets("Email Summary")
Set rngBody = .Range("A1:G16")
rngBody.Copy
End With

Outlook E-mail Signature (which populates just fine with the ‘SigString’ portion)



Any help to shore this up and make all the components work would be greatly appreciated!! :)


Sub Save()
ActiveWorkbook.Worksheets("Sheet1").Select
Range("D9").Select

Dim IntialName As String
Dim sFileSaveName As Variant

IntialName = "Sample Output"
sFileSaveName = Application.GetSaveAsFilename(InitialFileName:=InitialName, fileFilter:="Excel Files (*.xlsm), *.xlsm")

If sFileSaveName <> False Then
ActiveWorkbook.SaveAs sFileSaveName
End If

Sub Mail_Outlook_()
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Dim SigString As String
Dim Signature As String
Dim rngTo As Range
Dim rngSubject As Range
Dim rngBody As Range

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

strbody = "Good Morning," & _
"Attached is the today’s sales report. Please advise with any questions."

With Sheets("E-mail")
Set rngTo = .Range("D3")
Set rngSubject = .Range("D4")

End With

With Sheets("Email Summary")
Set rngBody = .Range("A1:G16")
rngBody.Copy
End With

SigString = Environ("appdata") & _
"\Microsoft\Signatures\Greg.htm"

If Dir(SigString) <> "" Then
Signature = GetBoiler(SigString)
Else
Signature = ""
End If

On Error Resume Next

With OutMail
.To = rngTo.Value
.Subject = "Sales"
.Attachments.Add ActiveWorkbook.FullName
.HTMLBody = strbody & "
" & Signature
.Display
End With

On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub

Function GetBoiler(ByVal sFile As String) As String
Dim fso As Object
Dim ts As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.GetFile(sFile).OpenAsTextStream(1, -2)
GetBoiler = ts.readall
ts.Close
End Function
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK

Forum statistics

Threads
1,224,521
Messages
6,179,280
Members
452,902
Latest member
Knuddeluff

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