![](/img/trans.png)
[英]Macro for getting the file name from the cell and prompt the location to save the file
[英]Save as file without prompt for file name
我正在嘗試創建一個沒有任何用戶界面的PDF文件,只是單擊Excel文件中的一個按鈕。
使用以下代碼,當使用自動生成的名稱將文件另存為時,代碼提示輸入文件名,而不是從代碼中獲取文件名。
我感覺sendkeys
無法正常工作。
Sub PrinttoPDFTest()
ActiveSheet.PageSetup.PrintArea = "$A$1:$F$17"
With ActiveSheet.PageSetup
.PrintTitleRows = ""
.PrintTitleColumns = ""
End With
ActiveSheet.PageSetup.PrintArea = "$A$1:$F$17"
With ActiveSheet.PageSetup
.Orientation = xlLandscape
.FitToPagesWide = 1
.FitToPagesTall = 1
End With
ActiveWindow.SelectedSheets.PrintOut Copies:=1, ActivePrinter:="Adobe PDF on Ne04:", Collate:=True
newHour = Hour(Now())
newMinute = Minute(Now())
newSecond = Second(Now()) + 5
waitTime = TimeSerial(newHour, newMinute, newSecond)
Application.Wait waitTime
Filename = "C:\Temp\PDF\" & ActiveSheet.Range("DateSerial").Value & ".pdf"
SendKeys Filename & "{Enter}", False
End Sub
使用.ExportAsFixedFormat而不是.PrintOut會更簡單,只需堆疊您的請求即可(無需延遲代碼):
Sub ExporttoPDF()
Sheets("Sheet1").ExportAsFixedFormat xlTypePDF, "C:\Folder\Filename1.pdf"
Sheets("Sheet2").ExportAsFixedFormat xlTypePDF, "C:\Folder\Filename2.pdf"
End Sub
只需替換目標位置和文件名,此代碼就可以正常工作。 如果要在工作表中使用值作為文件名的一部分,則可以這樣連接(&):
Sheets("Sheet1").ExportAsFixedFormat xlTypePDF, "C:\Folder\" & Range("P10").Value & ".pdf"
或者將文件名存儲在變量中,並通過以下方式進行操作:
dim filename as string
filename = Range("P10").Value
Sheets("Sheet1").ExportAsFixedFormat xlTypePDF, filename
並持續下去...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.