简体   繁体   中英

How to print excel file to pdf with the option ".ExportAsFixedFormat" via excel macro

I have wrote a code to print excel file to .PDF file with the page setups parameters.And also it eliminates the need of having a prompt dialog box also.

But I need to know if I need to name the .PDF file as same as the excel file name with below code but not the same destination path .As an example:= if excel file name is " Quality Report 1411185623689 " This file is generated by a system therefore its name is changed everyday. How do I solve this?

 Sub Save_As_PDF()
With ActiveSheet.PageSetup
     .Orientation=xlLandscape
     .Zoom=16

End With
ActiveSheet.ExportAsFixedFormat _
 Type:=xlTypePDF, _
 FileName:="C\:Desktop\Reports\Same as excel file name", _
 Quality:=xlQualityStandard, _
 IncludeDocProperties:=False, _
 IgnorePrintAreas:=False, _
 OpenAfterPublish:=True

Exit Sub

Untested, but assuming you want to name the PDF the same as the Excel file (ignoring file extension), but in a different folder (say some folder/directory called "C\\:Desktop\\Reports\\" for example):

Option explicit

Sub SaveAsPDF()

Dim folderPath as string
folderPath = "C\:Desktop\Reports\" ' Change to whatever folder, but make sure it ends with a \

If len(dir$(folderPath, vbDirectory)) = 0 then
Msgbox("'" & folderPath & "' is not a valid/existing directory. Abandoning export. Code will stop running now.")
Exit sub
End if

Dim Filename as string
Filename = left$(Thisworkbook.name, instrrev(Thisworkbook.name, ".", -1, vbbinarycompare) -1) & ".pdf"

With ActiveSheet.PageSetup
     .Orientation=xlLandscape
     .Zoom=16
End With
ActiveSheet.ExportAsFixedFormat _
 Type:=xlTypePDF, _
 FileName:=folderPath & filename, _
 Quality:=xlQualityStandard, _
 IncludeDocProperties:=False, _
 IgnorePrintAreas:=False, _
 OpenAfterPublish:=True

Exit Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM