Hi all,
I am trying to create a tool using VBA that can send forecasts to suppliers. The message consists of an intro in cells A2:J11. Columns A:J will have the forecast which will be copied from another sheet (the number of row varies depending on supplier). And finally there is a note at the end of the forecast.
Through various sources and help from others, I have managed to get it working and the code is as below:
Sub Mail_Selection_Forecast()
'Working in Excel 2000-2013
Dim Rng As Range
Dim OutApp As Object
Dim OutMail As Object
Set Rng = Nothing
On Error Resume Next
'Only the visible cells in the selection
Set Rng = Selection.SpecialCells(xlCellTypeVisible)
'You can also use a fixed range if you want
Set Rng = Sheets("Forecast").Range("A2:H150", "A1").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)
On Error Resume Next
With OutMail
.To = Sheets("Forecast").Range("Q1")
.CC = "example@example.com"
.BCC = ""
.Subject = "Forecast"
.HTMLBody = RangetoHTML(Rng)
.Display '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-2013
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 xublishsource=", _
"align=left xublishsource=")
'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
With the current code, only the chosen range can be sent (Range("A2:H150", "A1"). However, I would like to be able to change this depending on the number of rows the forecast has. For example, if a company has 40 rows, I would like to just send 40 and then be able to have the "Note" just below it (it is currently in a merged cell M118:V131).
Would this be possible? If so, how would I be able to do it?
Please do let me know if anything is not clear.
Greatly appreciate any response.
Thank you
I am trying to create a tool using VBA that can send forecasts to suppliers. The message consists of an intro in cells A2:J11. Columns A:J will have the forecast which will be copied from another sheet (the number of row varies depending on supplier). And finally there is a note at the end of the forecast.
Through various sources and help from others, I have managed to get it working and the code is as below:
Sub Mail_Selection_Forecast()
'Working in Excel 2000-2013
Dim Rng As Range
Dim OutApp As Object
Dim OutMail As Object
Set Rng = Nothing
On Error Resume Next
'Only the visible cells in the selection
Set Rng = Selection.SpecialCells(xlCellTypeVisible)
'You can also use a fixed range if you want
Set Rng = Sheets("Forecast").Range("A2:H150", "A1").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)
On Error Resume Next
With OutMail
.To = Sheets("Forecast").Range("Q1")
.CC = "example@example.com"
.BCC = ""
.Subject = "Forecast"
.HTMLBody = RangetoHTML(Rng)
.Display '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-2013
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 xublishsource=", _
"align=left xublishsource=")
'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
With the current code, only the chosen range can be sent (Range("A2:H150", "A1"). However, I would like to be able to change this depending on the number of rows the forecast has. For example, if a company has 40 rows, I would like to just send 40 and then be able to have the "Note" just below it (it is currently in a merged cell M118:V131).
Would this be possible? If so, how would I be able to do it?
Please do let me know if anything is not clear.
Greatly appreciate any response.
Thank you