chriswhincup
New Member
- Joined
- Apr 19, 2023
- Messages
- 10
- Office Version
- 365
- Platform
- Windows
Hi,
I'm trying to create a script that will pdf a worksheet based on the date and customer name in a cell (C4). I need it to save onto the desktop of whichever user is using the spreadsheet. I then want it to send it by email using outlook to an email address in cell I2 and CC to an email in I9.
This is how far I got which did work at first but now seems to error when adding the attachment but I cant work out where I've gone wrong. Happy to use an entirely different script if there's an easier way.
Sub SaveRangeAsPDF()
'Create and assign variables
Dim saveLocation As String
Dim ID As String
Dim ws As Worksheet
Dim rng As Range
Dim Eapp As Object
Set Eapp = CreateObject("Outlook.Application")
Dim EItem As Object
Set EItem = Eapp.createitem(0)
ID = Range("C4").Text
saveLocation = today + ID + ".pdf"
Set ws = Sheets("ORDER FORM")
Set rng = ws.Range("A1:L37")
'Save a range as PDF
rng.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=saveLocation
user = Environ("Username")
desktop = "C:\users\" & Environ("Username") & "\Desktop"
today = Format(Now(), "DD-Mmm-YYYY")
With EItem
.To = Range("I2")
.CC = Range("I9")
.Subject = "Sales Order for " & ID
.Body = "Hi, Please find attached a new sales order. Thanks"
.Attachments.Add (today + ID + ".pdf")
.Display
End With
End Sub
I'm trying to create a script that will pdf a worksheet based on the date and customer name in a cell (C4). I need it to save onto the desktop of whichever user is using the spreadsheet. I then want it to send it by email using outlook to an email address in cell I2 and CC to an email in I9.
This is how far I got which did work at first but now seems to error when adding the attachment but I cant work out where I've gone wrong. Happy to use an entirely different script if there's an easier way.
Sub SaveRangeAsPDF()
'Create and assign variables
Dim saveLocation As String
Dim ID As String
Dim ws As Worksheet
Dim rng As Range
Dim Eapp As Object
Set Eapp = CreateObject("Outlook.Application")
Dim EItem As Object
Set EItem = Eapp.createitem(0)
ID = Range("C4").Text
saveLocation = today + ID + ".pdf"
Set ws = Sheets("ORDER FORM")
Set rng = ws.Range("A1:L37")
'Save a range as PDF
rng.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=saveLocation
user = Environ("Username")
desktop = "C:\users\" & Environ("Username") & "\Desktop"
today = Format(Now(), "DD-Mmm-YYYY")
With EItem
.To = Range("I2")
.CC = Range("I9")
.Subject = "Sales Order for " & ID
.Body = "Hi, Please find attached a new sales order. Thanks"
.Attachments.Add (today + ID + ".pdf")
.Display
End With
End Sub