简体   繁体   English

如何通过excel宏使用选项“.ExportAsFixedFormat”将excel文件打印为pdf

[英]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.我已经编写了一个代码来将 excel 文件打印为带有页面设置参数的 .PDF 文件。而且它也消除了对提示对话框的需要。

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.但我需要知道我是否需要将 .PDF 文件命名为与以下代码的 excel 文件名相同但目标路径不同。例如:= 如果 excel 文件名是“质量报告 1411185623689 ” 生成此文件因此,它的名称每天都在更改。 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):未经测试,但假设您想将 PDF 命名为与 Excel 文件相同的名称(忽略文件扩展名),但位于不同的文件夹中(例如某个名为"C\\:Desktop\\Reports\\"文件夹/目录):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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