vba 使用当前工作簿名称在当前文件夹中将 excel 保存为 PDF
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43345109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Save excel as PDF in current folder using current workbook name
提问by Joaquim Agostinho
Trying to save a set of sheets from a workbook to PDF format to the same folder and using workbook name.
尝试将工作簿中的一组工作表以 PDF 格式保存到同一文件夹并使用工作簿名称。
After recording the macro with all the steps went back and tried to replace the pieces of the code by what I found in some of this forum's threads but now it is not working.
在用所有步骤录制宏之后,我返回并尝试用我在该论坛的一些线程中找到的内容替换代码片段,但现在它不起作用。
Current version is below. What did I break?
当前版本如下。我打破了什么?
SaveToPDF Macro
Sheets(Array("AUDIT Info", "REVIEW", "FILES", "WARNINGS", "PURGE", "NonBIM", _
"Clashes", "ViewsManagement")).Select
Sheets("AUDIT Info").Activate
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
ThisWorkbook.Path & "\" & ActiveWorkbook.Name _
, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=True
Sheets("AUDIT Info").Select
回答by Shai Rado
Try the code below:
试试下面的代码:
Option Explicit
Sub SaveSheetsasPDF()
ThisWorkbook.Sheets(Array("AUDIT Info", "REVIEW", "FILES", "WARNINGS", "PURGE", "NonBIM", "Clashes", "ViewsManagement")).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
ThisWorkbook.Path & "\" & ThisWorkbook.Name, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
End Sub
回答by Artem
You will get an error if before running the macro one of the needed sheets will be selected/activated.. that's why you need to add the first line sheets(1).select
如果在运行宏之前将选择/激活一个所需的工作表,您将收到错误..这就是您需要添加第一行工作表(1)的原因。
where the first sheet isn't in your array
第一张表不在您的数组中