Kill Temp File

jeffreyCarlino

New Member
Joined
Nov 2, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Good afternoon everyone, I modified the code below to send multiple PDF documents in an email, the code works but it gives me a compile error regarding the fill temp file line, and I cannot figure out the life of me why the code works but gives me the error. Anyone's help is much appreciated. Thank you.

Option Explicit

' Note: The macro below use also the function below it and in the function module

Sub Mail_ActiveSheet_3()
'Working in Excel 2000-2016
Dim FileExtStr As String
Dim FileFormatNum As Long
Dim TempFilePath As String
Dim TempPDFFileName As String
Dim TempPDFFileName2 As String
Dim TempPDFFileName3 As String
Dim OutApp As Object
Dim OutMail As Object
Dim rng As Range
Dim FileName As String
Dim FileName2 As String
Dim FileName3 As String

Dim ExcelBodySheet As Worksheet
Dim ExcelPDFSheet As Worksheet
Dim ExcelPDFSheet2 As Worksheet
Dim ExcelPDFSheet3 As Worksheet

Set ExcelBodySheet = ThisWorkbook.Sheets("Cut & Paste")
Set ExcelPDFSheet = ThisWorkbook.Sheets("Area 1 Photos")
Set ExcelPDFSheet2 = ThisWorkbook.Sheets("Area 2 Photos")
Set ExcelPDFSheet3 = ThisWorkbook.Sheets("Area 3 Photos")

TempFilePath = Environ$("temp") & "\"
TempPDFFileName = Sheets("Area 1 Photos").Range("A19").Value
TempPDFFileName2 = Sheets("Area 2 Photos").Range("A19").Value
TempPDFFileName3 = Sheets("Area 3 Photos").Range("A19").Value

With Application
.ScreenUpdating = False
.EnableEvents = False
End With

DoEvents

FileName = RDB_Create_PDF(Source:=ExcelPDFSheet, _
FixedFilePathName:=TempFilePath & TempPDFFileName & ".pdf", _
OverwriteIfFileExist:=True, _
OpenPDFAfterPublish:=False)
FileName2 = RDB_Create_PDF(Source:=ExcelPDFSheet2, _
FixedFilePathName:=TempFilePath & TempPDFFileName2 & ".pdf", _
OverwriteIfFileExist:=True, _
OpenPDFAfterPublish:=False)
FileName3 = RDB_Create_PDF(Source:=ExcelPDFSheet3, _
FixedFilePathName:=TempFilePath & TempPDFFileName3 & ".pdf", _
OverwriteIfFileExist:=True, _
OpenPDFAfterPublish:=False)


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

Set rng = Nothing
Set rng = ExcelBodySheet.Range("A6:AF42").SpecialCells(xlCellTypeVisible)

With OutMail
.to = ThisWorkbook.Sheets("Start Here").Range("J22").Value
.CC = ThisWorkbook.Sheets("Start Here").Range("J24").Value
.BCC = ""
.Subject = ThisWorkbook.Sheets("Start Here").Range("J8").Value
If FileName <> "" Then .Attachments.Add FileName
If FileName2 <> "" Then .Attachments.Add FileName2
If FileName3 <> "" Then .Attachments.Add FileName3
.HTMLBody = RangetoHTML(rng)
.Display 'or use .Send

End With

'Delete the files you have send
Kill TempFilePath & TempPDFFileName & FileExtStr
If FileName <> "" Then Kill TempFilePath & TempPDFFileName & ".pdf"
If FileName2 <> "" Then Kill TempFilePath & TempPDFFileName2 & ".pdf"
If FileName3 <> "" Then Kill TempFilePath & TempPDFFileName3 & ".pdf"

Set OutMail = Nothing
Set OutApp = Nothing

With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub


Function RangetoHTML(rng As Range)
' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-2016
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
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
It doesn't seem like you ever set the variable FileExtStr to anything. So is just a blank string variable.
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,165
Members
453,021
Latest member
Justyna P

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