从单元格值将文件名分配给pdf [英] Assign filename to pdf from cell value
问题描述
我有一个宏可以将 excel 文件保存为 PDF 文件:
I have a macro that saves the excel file into a PDF one:
Sub PDF()
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:="C:\Report.pdf", _
OpenAfterPublish:=False
End Sub
如何将 pdf 名称分配给 excel 中特定单元格中的值?以及如何将保存目录指定为与 excel 文件所在的位置相同?
How do I assign the pdf name to a value in a specific cell in excel? And how I do specify the save directory as the same where the excel file is located?
推荐答案
如果您像下面这样修改您的代码,您的 ActiveSheet
将作为 .PDF
导出到与 ActiveWorkbook
相同的文件夹,并在 ActiveSheet
的单元格 A1
中定义名称.
If you modify your code like below, your ActiveSheet
will get exported as a .PDF
to the same folder as the ActiveWorkbook
, and with the name defined in cell A1
of the ActiveSheet
.
Sub PDF()
Dim SaveAsStr As String
SaveAsStr = ActiveWorkbook.Path & "\" & ActiveSheet.Range("A1").Value
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=SaveAsStr & ".pdf", _
OpenAfterPublish:=False
End Sub
请注意,您可能需要稍微改进此代码,因为如果您在尚未保存的工作簿中运行它(即没有保存 PDF 的有效路径),或者如果单元格带有具有文件名的单元格为空.
Note that you may want to refine this code a little since this will return an error if you run it in a workbook that is not yet saved (i.e. there is no valid path to save the PDF at) or if the cell with the cell with the filename is empty.
这篇关于从单元格值将文件名分配给pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!