Save Excel worksheet as PDF

Akshay_divecha

Board Regular
Joined
Mar 11, 2014
Messages
70
Hi Exports,

I am trying to save excel worksheet to PDF however encountering runtime error 1004 with the msg with reads as "Document not saved. The document may be open, or an error may have been encountered while saving.

when clicked on deburg, below in bold is highlited in yellow.

Kindly help to fix this.

Code:
Sub DesktopPDF()

'Set path to Desktop
    fPath = "C:\Users\divecak\Desktop"

'Build File Name from Sheet1 I8, A11 & H11
    fName = Sheets(1).Range("I8") & " - " & _
            Sheets(1).Range("A11") & " - " & _
            Sheets(1).Range("H11")

'Export as PDF to Desktop
[B]    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=fPath & fName, _
        Quality:=xlQualityStandard, IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, OpenAfterPublish:=True[/B]
        
End Sub
 
Hi John,

i just changed value in cell I8 to 123-45679-000 and file got saved.

but i would not like to use "-" in my invoice no.

so is it possible that this invoice no with from cell I8 gets copied in cell M1, remove the special characters and then M1 is used for file saving.
 
Upvote 0

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
"/" isn't valid in file names because it separates folder names. This code replaces "/" with "_":
Code:
fname = Replace(Sheets(1).Range("I8"), "/", "_") & " - " & _
            Sheets(1).Range("A11") & " - " & _
            Sheets(1).Range("H11") & ".pdf"

Edit - or if you want every "/" omitted then:

Code:
fname = Replace(Sheets(1).Range("I8"), "/", "") & " - " & _
            Sheets(1).Range("A11") & " - " & _
            Sheets(1).Range("H11") & ".pdf"
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,647
Messages
6,120,722
Members
448,987
Latest member
marion_davis

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