Whatever is in Cell B6

RLPeloquin

Board Regular
Joined
Jul 4, 2020
Messages
61
Office Version
  1. 2019
Platform
  1. Windows
This code works great but there is one exception that would make it perfect. Is there a way to Send whatever Email address is in B6 simply by clicking on Cell "B6". Which in this code is, With OutMail .To = "somebody@somewhere" This email address will always change but it will always be in Cell "B6". Thanks in advance!
Sub Send_Email()
Dim OutApp As Object, OutMail As Object, rng As Range
Set rng = Range("A1:N38")
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail
.To = "somebody@somewhere"
.Subject = "Electricity Usage"
.HTMLBody = RangetoHTML(rng)
.Display
End With

End Sub

Function RangetoHTML(rng As Range)
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"
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
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 savechanges:=False
Kill TempFile

Set ts = Nothing
Set fso = Nothing
Set TempWB = Nothing
End Function
 
Last edited by a moderator:

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
21,185
Office Version
  1. 2016
  2. 2013
  3. 2007
Platform
  1. Windows
Do you mean
Rich (BB code):
With OutMail
.To = cells(6,2).value
.Subject = "Electricity Usage"
.HTMLBody = RangetoHTML(rng)
.Display
End With
 

RLPeloquin

Board Regular
Joined
Jul 4, 2020
Messages
61
Office Version
  1. 2019
Platform
  1. Windows
Do you mean
Rich (BB code):
With OutMail
.To = cells(6,2).value
.Subject = "Electricity Usage"
.HTMLBody = RangetoHTML(rng)
.Display
End With
Works like a charm. Thank you so much
 

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
21,185
Office Version
  1. 2016
  2. 2013
  3. 2007
Platform
  1. Windows
Glad to help... :cool: (y)
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,152,579
Messages
5,770,967
Members
425,653
Latest member
UNSING

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
Top