VBA SaveAS PDF

Jason Barnes

New Member
Joined
Jan 15, 2015
Messages
46
I need to save sheet as pdf.
Have tried numerous codes from examples and continue to fail.

Getting Runtime Error 1004;
Document not saved. The document may be open, or an error may have been encountered when saving.

Code:
Sub SaveAsPDFExcel()
 
  Dim fName As String
  Dim sLoc As String  'location
 
  sLoc = "C:\Users\Jason Barnes\Desktop\Denbury\"
 
  ' Select sheet to export into PDF file
  Sheets("MAIN").Select
 
  With ActiveSheet
   
    fName = .Range("BC3").Value
 
    'Save as xlsm
    ActiveWorkbook.SaveAs Filename:=sLoc & fName, FileFormat:=xlOpenXMLWorkbookMacroEnabled
   
    ' Export to the PDF
    .ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
                         sLoc & fName, Quality:=xlQualityStandard, _
                         IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
                         
  End With
End Sub
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
I copied and pasted this and it worked for me, obviously changed file path. Unless there is an issue with Cell BC3, idk.
 
Upvote 0
.
Code:
Option Explicit


Sub SavePDF()
Dim Path, FileName1 As String
Path = "C:\Users\My\Desktop\"  '<--- edit path as desired
FileName1 = "Test PDF"   '<--- change file name as desired
ActiveWorkbook.ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=Path & FileName1 & ".pdf", OpenAfterPublish:=False
End Sub




Sub SavePDF_2()
Dim Path, FileName1 As String
Path = "C:\Users\My\Desktop\"  '<--- edit path as desired
FileName1 = "Test PDF" '<--- change file name as desired


ThisWorkbook.Sheets(Array("Sheet1", "Sheet2")).Select  '<--- add the sheet names as desired


ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    Path & FileName1 & ".pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, _
     IgnorePrintAreas:=False, OpenAfterPublish:=True
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,328
Messages
6,124,299
Members
449,149
Latest member
mwdbActuary

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