Hi there, Swastik,
Sorry to hear you had issues with the PDFCreator download. I've never heard of them having viruses in their downloads.
With regards to the issues with the "Can't initialize PDFCreator" message, I've seen this happen before. The most frequent cause that I've seen on PC's where it has been working fine is that after some software gets installed PDFCreator starts failing. I have no idea what sofware though... could be a windows update, a new program that runs in memory, could be anything. My suspicion is that it occurs when a new service is registered in Windows that takes just a little bit too many resources that PDFCreator was needing. Typically it hangs the PDFCreator process, and that prevents it from loading again.
Regardless, we can deal with it. At worst case we force a pause into the code, but I've put a fair effort into trying to work around that as I just don't like that concept at all.
The code below is a revised version of the Using PDFCreator with security options set article. It's got a revised method that checks if PDFCreator is hung up and kills it off if so.
Code:
Sub PrintToPDF_WithSecurity()
'Author : Ken Puls (www.excelguru.ca)
'Macro Purpose: Print to PDF file using PDFCreator WITH SECURITY
' (Download from http://sourceforge.net/projects/pdfcreator/)
' Designed for early bind, set reference to PDFCreator
Dim pdfjob As PDFCreator.clsPDFCreator
Dim bRestart As Boolean
Dim sPDFName As String
Dim sPDFPath As String
Dim sMasterPass As String
Dim sUserPass As String
'/// Change the output file names and passwords (if security req'd)! ///
sPDFName = "testPDF.pdf"
sPDFPath = ActiveWorkbook.Path & Application.PathSeparator
sMasterPass = "master"
sUserPass = "letmein"
'Check if worksheet is empty and exit if so
If IsEmpty(ActiveSheet.UsedRange) Then Exit Sub
'Check if PDFCreator is already running and attempt to kill the process if so
Do
bRestart = False
Set pdfjob = New PDFCreator.clsPDFCreator
If pdfjob.cStart("/NoProcessingAtStartup") = False Then
'PDF Creator is already running. Kill the existing process
Shell "taskkill /f /im PDFCreator.exe", vbHide
DoEvents
Set pdfjob = Nothing
bRestart = True
End If
Loop Until bRestart = False
With pdfjob
'Set details on where to save file to, and flag it automatic
.cOption("UseAutosave") = 1
.cOption("UseAutosaveDirectory") = 1
.cOption("AutosaveDirectory") = sPDFPath
.cOption("AutosaveFilename") = sPDFName
.cOption("AutosaveFormat") = 0 ' 0 = PDF
'The following are required to set security of any kind
.cOption("PDFUseSecurity") = 1
.cOption("PDFOwnerPass") = 1
.cOption("PDFOwnerPasswordString") = sMasterPass
'To set individual security options
.cOption("PDFDisallowCopy") = 1
.cOption("PDFDisallowModifyContents") = 1
.cOption("PDFDisallowPrinting") = 1
'To force a user to enter a password before opening
.cOption("PDFUserPass") = 1
.cOption("PDFUserPasswordString") = sUserPass
'To change to High encryption
.cOption("PDFHighEncryption") = 1
'Get ready for the print job
.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 the file shows up before closing PDF Creator
Do
DoEvents
Loop Until Dir(sPDFPath & sPDFName) = sPDFName
Cleanup:
'Release objects and terminate PDFCreator
Set pdfjob = Nothing
Shell "taskkill /f /im PDFCreator.exe", vbHide
On Error GoTo 0
Application.ScreenUpdating = True
Exit Sub
EarlyExit:
'Inform user of error, and go to cleanup section
MsgBox "There was an error encountered. PDFCreator has" & vbCrLf & _
"has been terminated. Please try again.", _
vbCritical + vbOKOnly, "Error"
Resume Cleanup
End Sub
If that doesn't solve it, let me know and we'll look at putting some pauses in.
Bookmarks