VBA Error: "Compile error: Expected End Sub" - excel

Trying to pass "GetFullNamePDF()" to the Filename attribute, but getting the following error: "Compile error: Expected End Sub"
Sub PrintPDF()
Function GetFullNamePDF() As String
GetFullNameCSV = Replace(ThisWorkbook.FullName, ".xlsm", ".pdf")
End Function
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"GetFullNamePDF()", Quality:=xlQualityStandard, IncludeDocProperties _
:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
End Sub
I know nothing about VBA, and got the above code from a question I asked yesterday, but was unable to test at the time. Guessing the error has to do with the function, since the code works without the function added and the filepath/name hard coded.
Idea of the code is to dynamically use the filename of itself to name the path and file for the PDF. If you have any questions, just comment -- thanks!

You can't nest a function inside a procedure. You need to move it above:
Function GetFullNamePDF() As String
GetFullNameCSV = Replace(ThisWorkbook.FullName, ".xlsm", ".pdf")
'This should be
GetFullNamePDF = Replace(ThisWorkbook.FullName, ".xlsm", ".pdf")
End Function
Sub PrintPDF()
'Remove the quotes from GetFullNamePDF
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
GetFullNamePDF(), Quality:=xlQualityStandard, IncludeDocProperties _
:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
End Sub

It is illegal to declare a function within a sub.
It should look like this:
Function GetFullNamePDF() As String
GetFullNamePDF = Replace(ThisWorkbook.FullName, ".xlsm", ".pdf")
End Function
Sub PrintPDF()
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"GetFullNamePDF()", Quality:=xlQualityStandard, IncludeDocProperties _
:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
End Sub

Like this:
Function GetFullNamePDF() As String
GetFullNamePDF = Replace(ThisWorkbook.FullName, ".xlsm", ".pdf")
End Function
Sub PrintPDF()
Dim sFileName As Variable
sFileName=GetFullNamePDF()
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
sFilename, Quality:=xlQualityStandard, IncludeDocProperties _
:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
End Sub

Related

Run-time error '-2147024773 (8007007b) Document not saved

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

Excel VBA: Save active worksheet as PDF

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

Saving document as a PDF with modified filename

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

Save range of cells to PDF at ThisWorkbook.Path

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

autosave keeps saving as "true" or "false" for filename

something is wrong with the macro, it keeps saving the way i want it to, but names the file "true" or "false". Note: the cell value itself is "=today()"
Sub Macro1()
'
' Macro1 Macro
ActiveWorkbook.SaveAs Filename = Range("C6").Value
FileFormat = xlOpenXMLWorkbookMacroEnabled
CreateBackup = False
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF
Filename = Range("c6").Value
FileFormat = pdf
Quality = xlQualityStandard
IncludeDocProperties = True
IgnorePrintAreas = False
OpenAfterPublish = False
End Sub
Try Filename:=Range("c6").Text to get the date as it appears in the cell.
The operator syntax is := between argument name and argument value.
Sub Macro1()
ActiveWorkbook.SaveAs _
Filename:=Range("C6").Text, _
FileFormat:=xlOpenXMLWorkbookMacroEnabled, _
CreateBackup:=False
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=Range("c6").Text, _
FileFormat:=xlpdf, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
End Sub

Resources