Excel Emailer Macro Buttons

samcorp

New Member
Joined
Jul 25, 2011
Messages
5
I'm looking to create buttons that will essetnially modify my current excel macro code so that it will have options such as:

Click Button A to email Sheet 1
Click Button B to email Sheet 2

I think the best method is to make it automatically change the universal emailer code, which is already made.

Can someone help me do this?
 

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
Code:
Sub Auto_Open()
Run "Mail_Sheet_Outlook_Body"
End Sub
 
Sub Mail_Sheet_Outlook_Body()
' Don't forget to copy the function RangetoHTML in the module.
' Working in Office 2000-2010
Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
 
Set rng = Nothing
Set rng = Sheets("Sheet3").UsedRange
'You can also use a sheet name
'Set rng = Sheets("YourSheet").UsedRange
 
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
 
On Error Resume Next
With OutMail
.To = Range("a2") & ";" & Range("a3") & ";" & Range("a4") & ";" & Range("a5") & ";" & Range("a6") & ";" & Range("a7") & ";" & Range("a8") & ";" & Range("a9") & ";" & Range("a10") & ";" & Range("a11") & ";" & Range("a12") & ";" & Range("a13") & ";" & Range("a14") & ";" & Range("a15") & ";" & Range("a16") & ";" & Range("a17") & ";" & Range("a18") & ";" & Range("a19") & ";" & Range("a20") & ";" & Range("a21") & ";" & Range("a22") & ";" & Range("a23") & ";" & Range("a24") & ";" & Range("a25") & ";" & Range("a26") & ";" & Range("a27") & ";" & Range("a28") & ";" & Range("a29") & ";" & Range("a30") & ";"
.CC = Range("d2") & ";" & Range("d3") & ";" & Range("d4") & ";" & Range("d5") & ";" & Range("d6") & ";" & Range("d7") & ";" & Range("d8") & ";" & Range("d9") & ";" & Range("d10") & ";" & Range("d11") & ";" & Range("d12") & ";" & Range("d13") & ";" & Range("d14") & ";" & Range("d15") & ";" & Range("d16") & ";" & Range("d17") & ";" & Range("d18") & ";" & Range("d19") & ";" & Range("d20") & ";" & Range("d21") & ";" & Range("d22") & ";" & Range("d23") & ";" & Range("d24") & ";" & Range("d25") & ";" & Range("d26") & ";" & Range("d27") & ";" & Range("d28") & ";" & Range("d29") & ";" & Range("d30") & ";"
.BCC = Range("e2") & ";" & Range("e3") & ";" & Range("e4") & ";" & Range("e5") & ";" & Range("e6") & ";" & Range("e7") & ";" & Range("e8") & ";" & Range("e9") & ";" & Range("e10") & ";" & Range("e11") & ";" & Range("e12") & ";" & Range("e13") & ";" & Range("e14") & ";" & Range("e15") & ";" & Range("e16") & ";" & Range("e17") & ";" & Range("e18") & ";" & Range("e19") & ";" & Range("e20") & ";" & Range("e21") & ";" & Range("e22") & ";" & Range("e23") & ";" & Range("e24") & ";" & Range("e25") & ";" & Range("e26") & ";" & Range("e27") & ";" & Range("e28") & ";" & Range("e29") & ";" & Range("e30") & ";"
.Subject = Range("b2")
.HTMLBody = Range("c2")
.Send 'or use .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)
' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-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 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

I'm new to VBA.. I kinda mixed and matched random sources and put something together :laugh: .. its clear I need some help. :(
 
Last edited by a moderator:
Upvote 0
Heres a pic if it helps.
2v36f5c.png



There are some more features I'd to add, but i'll start out with these basic buttons.. and see where we go from there! Thanks for the help again :)
 
Upvote 0

Forum statistics

Threads
1,224,551
Messages
6,179,476
Members
452,915
Latest member
hannnahheileen

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