问题
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
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?
回答1:
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
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.
来源:https://stackoverflow.com/questions/26098120/assign-filename-to-pdf-from-cell-value