I'm trying to create a PDF file without any user interface, other than clicking a button within the Excel file.
Using the below code, when the file is to be saved as with an automatically generated name, the code prompts for the filename, instead of grabbing it from the code.
I have a feeling that sendkeys
is not working out.
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
It would be simpler to use .ExportAsFixedFormat rather than .PrintOut, and just stack your requests (no delay code needed):
Sub ExporttoPDF()
Sheets("Sheet1").ExportAsFixedFormat xlTypePDF, "C:\Folder\Filename1.pdf"
Sheets("Sheet2").ExportAsFixedFormat xlTypePDF, "C:\Folder\Filename2.pdf"
End Sub
Just replace the destinations and filenames, and this code should work fine. If you want to use a value in the sheet as part of a filename, then you can concatenate (&) like this:
Sheets("Sheet1").ExportAsFixedFormat xlTypePDF, "C:\Folder\" & Range("P10").Value & ".pdf"
Or store the filename in a variable and do it this way:
dim filename as string
filename = Range("P10").Value
Sheets("Sheet1").ExportAsFixedFormat xlTypePDF, filename
And it goes on and on...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.