Printing Sheets in PDF

Rajesh D

New Member
Joined
Apr 26, 2010
Messages
26
Hi, I've tried the below code to print my file in pdf, unfortunately code not works at file name. Pls. guide me if anyone of you know how to rectify this.

Sub PRINT_PDF()
Application.ScreenUpdating = False
ActiveWindow.SelectedSheets.PrintOut Copies:=1, ActivePrinter:="CutePDF Writer on CPW2:", Collate:=True
newHour = Hour(Now())
newMinute = Minute(Now())
newSecond = Second(Now()) + 2
WaitTime = TimeSerial(newHour, newMinute, newSecond)
Application.Wait WaitTime
Filename="D:\MIS Reports\" & Date & ".pdf"
SendKeys Filename & "{Enter}", False
Application.ScreenUpdating = True
End Sub
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
CutePDF does not accept a filename in code - you have to enter it from the keyboard.
Consequently, when I want to specify the filename programmatically I use PDF Creator (a free version is available).
Have a look at my reply to the following post - it contains links to a PDF Creator solution for you:
http://www.mrexcel.com/forum/showthread.php?t=402856
(however, you will find an example of the PDF Creator code in the pos)
 
Upvote 0
After my code as below, still I'm getting a dialog box to save manually, will you Pls. clarify on this

Sub PRINT_PDF()
Application.ScreenUpdating = False
ActiveWindow.SelectedSheets.PrintOut Copies:=1, ActivePrinter:="PDFCreator on PDFCreator:", Collate:=True
newHour = Hour(Now())
newMinute = Minute(Now())
newSecond = Second(Now()) + 2
waitTime = TimeSerial(newHour, newMinute, newSecond)
Application.Wait waitTime
Filename = "G:\Report Copy\" & "Daily Report as on" & Format(Now, "-dd-mm-yyyy hh-mm-ss") & ".pdf"
SendKeys Filename & "{ENTER}", False
MsgBox "Report Copy has been Saved !!!"
Application.ScreenUpdating = True
End Sub
 
Upvote 0
This is because you have only changed one line in your code.
There are several more lines that you need including those (as shown below) that allow you to specify the filename:
Code:
    With pdfjob
        .cOption("UseAutosave") = 1
        .cOption("UseAutosaveDirectory") = 1
        .cOption("AutosaveDirectory") = sPDFPath
        .cOption("AutosaveFilename") = sPDFName
        .cOption("AutosaveFormat") = 0    ' 0 = PDF
        .cClearCache
    End With
See the 'AutosaveDirectory' and 'AutosaveFilename'.
 
Upvote 0
Re: Printing Sheets in PDF; Solved

Hi, Derek,
I've added your code and working fine, Thanks.
For viewer reference I've attached the code as below,

Sub PRINTTOPDF()
Dim pdfjob As Object
Dim sPDFName As String
Dim sPDFPath As String
'/// Change the output file name here! ///
sPDFName = "Daily Report as on " & Date & ".pdf"
'///I've changed the below code to my requirement///
'sPDFPath = ActiveWorkbook.Path & Application.PathSeparator
sPDFPath = "G:\Report Copy\"
'Check if worksheet is empty and exit if so
If IsEmpty(ActiveSheet.UsedRange) Then Exit Sub
Set pdfjob = CreateObject("PDFCreator.clsPDFCreator")
With pdfjob
If .cStart("/NoProcessingAtStartup") = False Then
MsgBox "Can't initialize PDFCreator.", vbCritical + _
vbOKOnly, "PrtPDFCreator"
Exit Sub
End If
.cOption("UseAutosave") = 1
.cOption("UseAutosaveDirectory") = 1
.cOption("AutosaveDirectory") = sPDFPath
.cOption("AutosaveFilename") = sPDFName
.cOption("AutosaveFormat") = 0 ' 0 = PDF
.cClearCache
End With
'Print the document to PDF
ActiveSheet.PrintOut Copies:=1, ActivePrinter:="PDFCreator"
'Wait until the print job has entered the print queue
Do Until pdfjob.cCountOfPrintjobs = 1
DoEvents
Loop
pdfjob.cPrinterStop = False
'Wait until PDF creator is finished then release the objects
Do Until pdfjob.cCountOfPrintjobs = 0
DoEvents
Loop
MsgBox ("The PDF has been successfully created as " & sPDFName)
pdfjob.cClose
Set pdfjob = Nothing
Set pdfjob = Nothing
End Sub

Thanks for your support.
 
Upvote 0

Forum statistics

Threads
1,225,149
Messages
6,183,194
Members
453,151
Latest member
Lizamaison

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top