Assign filename to pdf from cell value

南笙酒味 提交于 2020-02-08 02:55:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!