How to export from Excel to PDF to a determined location? - excel

I have this code to export to PDF, but it goes to the documents folder. I'd like to export it to a different location. How do I do that?
This is the code:
Private Sub CommandButton1_Click()
Range("a1:k44").Select
Selection.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=ru & "REMITO " & Range("C7") & ".pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=False
End Sub

Export excel as PDF to a specific path, you can use the code below. You will need to adjust it to your needs, this is the base.
Private Sub CommandButton1_Click()
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:="path_here+filename.pdf"
End sub
Example:
Filename:="C:\Documents\newfolder2\archive.pdf"

Related

Excel VBA SaveAs "Read-only" PDF file

The following code successfully saves an excel sheet as PDF. I would like to save it as a file, that can be opened by everyone, but not edited with the "fill and sign" feature, that is provided by Adobe Acrobat Reader DC. I have tried to simply add the following, but that does not work:
Attributes:=vbReadOnly
Here is the rest of the code:
Dim sPath As String
sPath = "O:\"
With Worksheets("Sheet 1")
.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=sPath & UserForm.TextBox1.Value & ".pdf", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
End With
You should be able to use SetAttr after saving.
SetAttr "c:\path\yourfilename.pdf", vbReadOnly

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

Saving Range Selection as PDF on Mac

I have the following code to try and export a specific selection as a one-page PDF. I was able to grant access but I can't seem to be able to export the selection as a PDF. I get a 1004 error. Any advice?
Sub PrintTest()
ChDir "/Users/Mr_Madoff/Desktop/"
Sheets("Sheet1").Range("A1:F53").Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:= _
"/Users/Mr_Madoff/Desktop/Book1.pdf", Quality:=xlQualityMinimum, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
False
End Sub

PDF print to file path VBA

I have the below code I have researched and got to work in one of my workbooks. The idea of the code is to automatically print a sheet to PDF and save down in a directory of my choosing naming it the contents of a specific cell.
All works fine until I try to copy into a new workbook (I designed in a test workbook as not to corrupt the original) then I get a
run time error 1004
and the ThisWorkbook.Sheets(Array("sheet 2")).Select is highlighted when I debug. Am I missing something simple? as the code works in the original workbook but not in if I paste into a new module in a new workbook? Sorry i'm quite new to this!
sub PrintPDFRT()
Sheets("test").Activate
ActiveSheet.UsedRange.Select
Sheets("malbru1").Activate
ActiveSheet.UsedRange.Select
Sheets("sheet 2").Activate
Range("A1:j137").Select
ThisWorkbook.Sheets(Array("sheet 2")).Select
Selection.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:="FIle path\" & Range("L7").Value, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
End Sub
Try it like this: (without Array)
sub PrintPDFRT()
WorkSheets("test").Activate
ActiveSheet.UsedRange.Select
WorkSheets("malbru1").Activate
ActiveSheet.UsedRange.Select
WorkSheets("sheet 2").Activate
Range("A1:j137").Select
ThisWorkbook.WorkSheets("sheet 2").ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:="FIle path\" & Range("L7").Value, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
End Sub

VBA Error: "Compile error: Expected End Sub"

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

Resources