将PDF自动保存到预定的文件夹

时间:2016-07-21 20:59:45

标签: excel vba excel-vba

我正在编写一个宏,可以自动发送我每天发送的每日报告。我最后一项是编写一个脚本,将格式化的Excel工作表保存为PDF(通过print to pdf选项),并将其保存在特定文件夹中。我写了以下内容,但它仍然提示用户保存它的位置。

将它自动保存到桌面某个位置的文件夹会有什么更好的方式或一般方法?

Sub printToPDF()
    Worksheets("general_report").PageSetup.CenterVertically = False
    ActiveWindow.SelectedSheets.PrintOut Copies:=1, ActivePrinter:="Foxit Reader PDF Printer"
End Sub

1 个答案:

答案 0 :(得分:1)

这应该做的工作,

Sub printToPDF()

    Dim FilePath As String
    Dim FileName As String

    FilePath = "C:\Users\userName\Desktop\" 'Change as per your username

    ActiveSheet.Copy 'Copy a worksheet to a new workbook

    'It saves .PDF file at your Descrop with the name of the worksheet
    ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, FileName:=FilePath & ActiveSheet.Name, _
        Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
        OpenAfterPublish:=False

    'Closing a newly created workbook without saving it
    Application.DisplayAlerts = False
    ActiveWorkbook.Close

End Sub