[英]Excel pdf export
我是Excel宏的新手。 我嘗試制作一個“另存為pdf”按鈕。 我寫了這樣的代碼:
Sub save_as_pdf()
'
' save_as_pdf Macro
' Saves sheet as PDF
'
Dim Path As String
Dim filename As String
Path = "/Users/Adrian/Desktop/"
filename = ThisWorkbook.Sheets("Controller").Range("B20")
PathAndFilename = Path & filename & ".pdf"
MsgBox "Saved file as: " & PathAndFilename
Sheets("View").Select
Application.DisplayAlerts = False
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, filename:= _
PathAndFilename, Quality:=xlQualityMinimum, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
False
Application.DisplayAlerts = True
End Sub
我需要Range("B20")
因為我根據一些in-excel邏輯保留了一個文件名。 MsgBox產生有效的路徑和文件名。
但是,當我運行此程序時,會出現“打印時出錯”和突出顯示ActiveSheet.ExportAsFixedFormat ...
的“運行時錯誤1004” ActiveSheet.ExportAsFixedFormat ...
在工作表中設置要導出的打印區域。
還要驗證路徑是否符合我的預期\\和驅動器號,例如C:\\
以下對我有用
Option Explicit
Sub save_as_pdf()
Dim Path As String
Dim filename As String
Dim PathAndFileName As String
Path = "C:\Users\User\Desktop\" ' "C:\Users\Adrian\Desktop\"
filename = ThisWorkbook.Sheets("Controller").Range("B20")
PathAndFileName = Path & filename & ".pdf"
MsgBox "Saved file as: " & PathAndFileName
Sheets("View").Select
Application.DisplayAlerts = False
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, filename:= _
PathAndFileName, Quality:=xlQualityMinimum, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
False
Application.DisplayAlerts = True
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.