I've been using some code to save an active worksheet to PDF, using the current date/time as the filename, and saving into a SharePoint folder.
Sub Save()
Dim strFilename As String
strFilename = Format(Now(), "yyyy-mm-dd hhmm")
Sheets("Weather Warning Action Sheet").Activate
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"https://xxx.sharepoint.com/sites/xxx/xxx/Paperwork and Admin/WX Warnings/2020/" & strFilename & ".pdf" _
, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=False
End Sub
This code has been working fine for a while, but has suddenly come up with the "Run-time error '1004': Document not saved" error message.
Debugging shows this as the error:
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"https://xxx.sharepoint.com/sites/xxx/xxx/Paperwork and Admin/WX Warnings/2020/" & strFilename & ".pdf" _
, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=False
Any assistance would be greatly appreciate.
Many thanks,
James
Related
Could anyone pls help regarding below, i am unable to save as pdf
AF26 (=TEXT("1st half"&AE36,)
sub hihihi()
Dim fName As String
fName = Range("AF26").Value
Range("A1:Q22").ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"C:\Users\user\Google 雲端硬碟 (staff01.phc#gmail.com)\xxx\fax\" & fName, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
End Sub
The following code runs perfectly from my desktop or other folder paths. Except in a shared drive that keeps giving me a run-time error 5
The error appears on the following line
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
ThisWorkbook.Path & "\" & Cell & ".pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
The full code
Private Sub CommandButton1_Click()
Dim ReportSheet As Worksheet
If ThisWorkbook.Worksheets("Cover Page").Range("F11").Value <> vbNullString Then
FormatDate = Format(Sheets("Cover Page").Range("F11"), "MMMM DD, YYYY")
End If
Cell = "Statements - " & FormatDate
ThisWorkbook.Sheets(Array("Sheet1", " Sheet2", " Sheet3”).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
ThisWorkbook.Path & "\" & Cell & ".pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
Application.Goto Reference:=Sheets("X").Range("A1")
End Sub
I am trying to save my Excel file as a PDF but with custom file name.
I would only add a piece to the prompted filename from the Excel file.
According to the queries here:
Save excel as PDF in current folder using current workbook name
Save excel as PDF in current folder using current workbook name
My code looks as follows:
Sub DPPtoPDF()
ThisWorkbook.Sheets.Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
ThisWorkbook.Path & "\" & ThisWorkbook.Name, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
Sheets("Frontsheet").Select
End Sub
From this code we know the PDF filename will be the Excel filename.
I tried something like this:
ThisWorkbook.Sheets.Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= &fName $ "-Route-Aprooval.pdf" _
ThisWorkbook.Path & "\" & ThisWorkbook.Name, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
I get an error as per in the picture below.
I can see something is up, as my code is turning red.
The code:
ThisWorkbook.Sheets.Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
" &fName $ "-Route-Aprooval.pdf""
ThisWorkbook.Path & "\" & ThisWorkbook.Name, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
also doesn't work.
Any code behind the Filename:= destroys whole code.
I want to keep the name as in Excel, but add another part of the name after dash (per the image above).
Where should I place my new output filename?
Try the code below.
Sub DPPtoPDF()
Dim Custom_Name as string
ThisWorkbook.Sheets.Select
Custom_Name= ThisWorkbook.Name & "-route approval" & ".pdf"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
ThisWorkbook.Path & "\" & Custom_Name, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
Sheets("Frontsheet").Select
End Sub
I'm trying to export a sheet to PDF, but when I run the code I keep getting a 1004 error.
I believe that it is linked to the folder path to the save destination. I've tried defining the file path in different ways but still get the error.
This code is supposed to pull a part of the file name from cell B1 and the file path from the location of the worksheet. The PDF is supposed to be saved to the location of the worksheet. I'm fairly new to VBA. Any help is appreciated!
Here is the code
Sub ExportAsPDFTest()
Dim Name As String
Dim Preface As String
Name = Cells(1, "B").Value
Preface = "PreR Summer 2019 - "
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
FileName:=ActiveWorkbook.Path & Preface & Name & ".pdf", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=False, _
IgnorePrintAreas:=False, _
From:=1, _
To:=1, _
OpenAfterPublish:=False
End Sub
You're missing a backslash \ (or / if you're on Mac) after ActiveWorkbook.Path. You can use Application.PathSeparator so it will work on both:
Sub ExportAsPDFTest()
Dim Name As String
Dim Preface As String
Name = Cells(1, "B").Value
Preface = "PreR Summer 2019 - "
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=ActiveWorkbook.Path & Application.PathSeparator & Preface & Name & ".pdf", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=False, _
IgnorePrintAreas:=False, _
From:=1, _
To:=1, _
OpenAfterPublish:=False
End Sub
Hope this helps.
Please add msgbox Activeworkbook.path to check your save path, you need add one more "\"
I have this macro that saves to a defined path which is not useful since multiple people will be using the file.
Sub SavetoPdf()
'
' SavetoPdf Macro
' To Save to PDF Details
'
' Keyboard Shortcut: Ctrl+s
'
Range("E31:I54").Select
Application.CutCopyMode = False
ChDir "/Users/Me/Desktop/"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:= _
"/Users/Me/Desktop/" & Range("G41").Value & ".pdf", Quality:=xlQualityMinimum, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
False
End Sub
I thought to save to ThisWorkbook.Path and modified the above.
Sub SavetoPdf()
' Saves active sheet as PDF file.
Dim Name As String
Name = ThisWorkbook.Path & "/" & Range("G41").Value & ".pdf"
Range("E31:I54").Select
Application.CutCopyMode = False
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:=Name, _
Quality:=xlQualityMinimum, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
End Sub
I am on a Mac.
I get
Error on Printing
and the debugger highlights this code:
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:=Name, _
Quality:=xlQualityMinimum, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
You title reads that you want to PDF a "Range" of cells. Is that what is not working for you?
Sub SavetoPdf()
' Saves active sheet as PDF file.
Dim Name As String
Name = ThisWorkbook.Path & "/" & Range("G41").Value & ".pdf"
Range("E31:I54").ExportAsFixedFormat Type:=xlTypePDF, Filename:=Name, _
Quality:=xlQualityMinimum, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
End Sub
Modified #Davesexel code: Tested on mock data and saved no problem.
Dim rng As Range
Dim Name As String
Set rng = Worksheets("Sheet1").Range("G41") 'Change worksheet to your needs
Name = rng.Value
Range("E31:I54").ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=ThisWorkbook.Path & "/" & Name & ".pdf", _
Quality:=xlQualityMinimum, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=False