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

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
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,224,820
Messages
6,181,154
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