Macro to save range as PDF issue

jtmjtm

New Member
Joined
Oct 16, 2018
Messages
3
Hi All,

I've found a macro online that I am using to save a fixed range to PDF, with the name generated by cells within the sheet. However when I try to run the macro it returns this:

<style type="text/css">p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px 'Helvetica Neue'}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px 'Helvetica Neue'; min-height: 12.0px}</style>
Run-time error '1004':


Application-defined or object-defined error

This is the macro that I am using, when selecting debug it highlights the line containing the ExportAsFixedFormat command, I was also having this issue when testing with just the ExportAsFixedFormat command.

Code:
Public Sub CreateInvoicePdf()


Dim outputFile As String


With Sheets("Invoice Creation")
    outputFile = Environ("UserProfile") & "\Desktop\Quotes"
    outputFile = outputFile & .Range("A1").Value & " " & .Range("E1").Value & " Invoice" & ".pdf"
    .Range("A1:G45").ExportAsFixedFormat Type:=xlTypePDF, Filename:=outputFile
End With


End Sub

I am using Excel on a mac if that helps.

Thank you in advance for any help.
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
It is probably a path error. My guess is missing backslash
- is Quotes a subfolder? If so...
Code:
   outputFile = Environ("UserProfile") & "\Desktop\Quotes[SIZE=3][B][COLOR=#000080]\[/COLOR][/B][/SIZE]"

I presume this simply generates the name of the file
Code:
    .Range("A1").Value & " " & .Range("E1").Value & " Invoice"

See what the message box returns. correct the error and remove the apostrophe on next line
Code:
Public Sub CreateInvoicePdf()
Dim outputFile As String
With Sheets("Invoice Creation")
    outputFile = Environ("UserProfile") & "\Desktop\Quotes"
    outputFile = outputFile & .Range("A1").Value & " " & .Range("E1").Value & " Invoice" & ".pdf"
    
    MsgBox outputFile & .Range("A1").Value & " " & .Range("E1").Value & " Invoice" & ".pdf"
[COLOR=#ff0000] [SIZE=3][B]'[/B][/SIZE][/COLOR]  .Range("A1:G45").ExportAsFixedFormat Type:=xlTypePDF, Filename:=outputFile
End With

End Sub
 
Last edited:
Upvote 0
Try
Code:
Public Sub CreateInvoicePdf()


Dim outputFile As String
Dim Sep As String
Sep = Application.PathSeparator

With Sheets("Invoice Creation")
    outputFile = Environ("UserProfile") & Sep & "Desktop" & Sep & "Quotes" & Sep
    outputFile = outputFile & .Range("A1").Value & " " & .Range("E1").Value & " Invoice" & ".pdf"
    .Range("A1:G45").ExportAsFixedFormat Type:=xlTypePDF, fileName:=outputFile
End With
 
Upvote 0

Forum statistics

Threads
1,223,895
Messages
6,175,257
Members
452,625
Latest member
saadat28

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