Trending September 2023 # Copying Active Sheet In Email Body / Rondebruin Rangetohtml Function # Suggested October 2023 # Top 13 Popular |

Trending September 2023 # Copying Active Sheet In Email Body / Rondebruin Rangetohtml Function # Suggested October 2023 # Top 13 Popular

You are reading the article Copying Active Sheet In Email Body / Rondebruin Rangetohtml Function updated in September 2023 on the website We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 Copying Active Sheet In Email Body / Rondebruin Rangetohtml Function

Hi Everyone, 

Thanks a lot for this forum and all those helpful posts – a truly great bunch of talented people !!

I’ve been trying to get a code to 

1) Save my Active Worksheet into a Pdf file with a pre-determined name

2) Attach it to an email with pre-determined subject, Email-To and Email- CC 

3) Copy part of the worksheet into the Email Body 

I’ve managed to get the first two running but I just can’t get my Email Body to work. 

I’ve used this great post by Philip Treacy as well as Ron de Bruin’s RangetoHTML function and tried to combine the two but when I try to run it I get a 91 type Error – Undefined Variable. 

The Debugor points to the line : rng.Copy, which to me is not a variable but a command action, as rng is defined at least twice in the code before. 

Here is the complete code : 

Application.ReferenceStyle = xlA1

Dim EmailSubject As String, EmailSignature As String

Dim Email_Body As String

Dim CurrentMonth As String, DestFolder As String, PDFFile As String

Dim Email_To As String, Email_CC As String, Email_BCC As String

Dim OpenPDFAfterCreating As Boolean, AlwaysOverwritePDF As Boolean, DisplayEmail As Boolean

Dim OverwritePDF As VbMsgBoxResult

Dim rng As Range

Dim OutlookApp As Object, OutlookMail As Object

CurrentMonth = “”

‘ *****************************************************

‘ ***** You Can Change These Variables *********

EmailSubject = “Emergence Actions (FR0011742683) – Amortissement en capital – ACMN – ” ‘Change this to change the subject of the email. The current month is added to end of subj line

OpenPDFAfterCreating = False ‘Change this if you want to open the PDF after creating it : TRUE or FALSE

AlwaysOverwritePDF = False ‘Change this if you always want to overwrite a PDF that already exists :TRUE or FALSE

DisplayEmail = True ‘Change this if you don’t want to display the email before sending. Note, you must have a TO email address specified for this to work

Email_To = ActiveSheet.Range(“J10”) ‘Change this if you want to specify To email e.g. ActiveSheet.Range(“H1”) to get email from cell H1

Email_CC = “”

Email_BCC = “”

Email_Body = RangetoHTML(rng)

‘ ******************************************************

‘Prompt for file destination

With Application.FileDialog(msoFileDialogFolderPicker)

If .Show = True Then

DestFolder = .SelectedItems(1)


MsgBox “You must specify a folder to save the PDF into.” & vbCrLf & vbCrLf & “Press OK to exit this macro.”, vbCritical, “Must Specify Destination Folder”

Exit Sub

End If

End With

‘Current month/year stored in H6 (this is a merged cell)

CurrentMonth = Mid(ActiveSheet.Range(“H6”).Value, InStr(1, ActiveSheet.Range(“H6″).Value, ” “) + 1)

‘Create new PDF file name including path and file extension

PDFFile = DestFolder & Application.PathSeparator & “Emergence Actions – Amortissement en Capital – ACMN ” _

& “_” & CurrentMonth & “.pdf”

‘If the PDF already exists

If AlwaysOverwritePDF = False Then

OverwritePDF = MsgBox(PDFFile & ” already exists.” & vbCrLf & vbCrLf & “Do you want to overwrite it?”, vbYesNo + vbQuestion, “File Exists”)

On Error Resume Next

‘If you want to overwrite the file then delete the current one

If OverwritePDF = vbYes Then

Kill PDFFile


MsgBox “OK then, if you don’t overwrite the existing PDF, I can’t continue.” _

& vbCrLf & vbCrLf & “Press OK to exit this macro.”, vbCritical, “Exiting Macro”

Exit Sub

End If


On Error Resume Next

Kill PDFFile

End If

If Err.Number 0 Then

MsgBox “Unable to delete existing file. Please make sure the file is not open or write protected.” _

& vbCrLf & vbCrLf & “Press OK to exit this macro.”, vbCritical, “Unable to Delete File”

Exit Sub

End If

End If

Debug.Print PDFFile

‘Create the PDF

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDFFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _

:=False, OpenAfterPublish:=OpenPDFAfterCreating

‘Create an Outlook object and new mail message

Set OutlookApp = CreateObject(“Outlook.Application”)

Set OutlookMail = OutlookApp.CreateItem(0)

‘Create the email body

Set rng = Nothing

On Error Resume Next

Set rng = Sheets(“Amort 1”).Range(“A23:E43”).SpecialCells(xlCellTypeVisible)

‘Display email and specify To, Subject, etc

With OutlookMail


.To = Email_To

.CC = Email_CC

.BCC = Email_BCC

.Subject = EmailSubject & CurrentMonth

.Attachments.Add PDFFile

If DisplayEmail = False Then


End If

End With

End Sub

Function RangetoHTML(rng As Range)

‘ Changed by Ron de Bruin 28-Oct-2006

‘ Working in Office 2000-2023

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


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


Application.CutCopyMode = False

On Error Resume Next

.DrawingObjects.Visible = True


On Error GoTo 0

End With

‘Publish the sheet to a htm file

With chúng tôi _

SourceType:=xlSourceRange, _

Filename:=TempFile, _

Sheet:=TempWB.Sheets(1).Name, _

Source:=TempWB.Sheets(1).UsedRange.Address, _


.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


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 am a complete beginner in VBA and find myself at my wits ends. 

Best !

You're reading Copying Active Sheet In Email Body / Rondebruin Rangetohtml Function

Update the detailed information about Copying Active Sheet In Email Body / Rondebruin Rangetohtml Function on the website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!