N
navin
Hi All,
I am trying to convert a word document to pdf in Excel. It converts
the word document into PDF if i manually change the default printer to
PDF printer, but when i try to assign the printer name using
ActivePrinter, it gives error: "Named Argument not found". Can anybody
please tell me, after opening the word document using excel VBA, how
can change the activeprinter for it.
Below is the code which i am using to convert the word to PDF with PDF
creator:
Function OpenPOWordDocument _
(FullNameOfDoc As String, _
Optional AuthorName As String) As Boolean
Dim wdApp As Object
Dim wdDoc As Object
Dim pdfjob As Object
Dim sPDFName As String
Dim sPDFPath As String
'On Error GoTo ErrorHandler
OpenPOWordDocument = False
'Open a new instance of Word and make it
'visible.
Set wdApp = CreateObject("Word.Application")
sPDFName = "testPDF.pdf"
sPDFPath = "D:\PO Copies"
wdApp.Visible = True
Set wdDoc = wdApp.Documents.Open(FileName:=FullNameOfDoc)
If AuthorName = "" Then
wdDoc.BuiltinDocumentProperties("Author") _
= Application.UserName
Else
wdDoc.BuiltinDocumentProperties("Author") _
= AuthorName
End If
wdDoc.Activate
Set pdfjob = CreateObject("PDFCreator.clsPDFCreator")
With pdfjob
If .cStart("/NoProcessingAtStartup") = False Then
MsgBox "Can't initialize PDFCreator.", vbCritical + _
vbOKOnly, "PrtPDFCreator"
Exit Function
End If
.cOption("UseAutosave") = 1
.cOption("UseAutosaveDirectory") = 1
.cOption("AutosaveDirectory") = sPDFPath
.cOption("AutosaveFilename") = checklength
.cOption("AutosaveFormat") = 0 ' 0 = PDF
.cClearCache
End With
wdDoc.ActiveWindow.PrintOut copies:=1, ActivePrinter:="PDFCreator"
Do Until pdfjob.cCountOfPrintjobs = 1
DoEvents
Loop
pdfjob.cPrinterStop = False
Do Until pdfjob.cCountOfPrintjobs = 0
DoEvents
Loop
pdfjob.cClose
Set pdfjob = Nothing
'Return value of the function; signifies successful completion.
OpenSelectedWordDocument = True
'ExitPoint:
' Set wdDoc = Nothing
' Set wdApp = Nothing
'
' Exit Function
'ErrorHandler:
' MsgBox "Sorry, unable to open the Word file. The following error
occurred:" _
' & vbCrLf & Err.Number & vbCrLf & Err.Description
' Resume ExitPoint
End Function
Thanks for the help.
Navin
I am trying to convert a word document to pdf in Excel. It converts
the word document into PDF if i manually change the default printer to
PDF printer, but when i try to assign the printer name using
ActivePrinter, it gives error: "Named Argument not found". Can anybody
please tell me, after opening the word document using excel VBA, how
can change the activeprinter for it.
Below is the code which i am using to convert the word to PDF with PDF
creator:
Function OpenPOWordDocument _
(FullNameOfDoc As String, _
Optional AuthorName As String) As Boolean
Dim wdApp As Object
Dim wdDoc As Object
Dim pdfjob As Object
Dim sPDFName As String
Dim sPDFPath As String
'On Error GoTo ErrorHandler
OpenPOWordDocument = False
'Open a new instance of Word and make it
'visible.
Set wdApp = CreateObject("Word.Application")
sPDFName = "testPDF.pdf"
sPDFPath = "D:\PO Copies"
wdApp.Visible = True
Set wdDoc = wdApp.Documents.Open(FileName:=FullNameOfDoc)
If AuthorName = "" Then
wdDoc.BuiltinDocumentProperties("Author") _
= Application.UserName
Else
wdDoc.BuiltinDocumentProperties("Author") _
= AuthorName
End If
wdDoc.Activate
Set pdfjob = CreateObject("PDFCreator.clsPDFCreator")
With pdfjob
If .cStart("/NoProcessingAtStartup") = False Then
MsgBox "Can't initialize PDFCreator.", vbCritical + _
vbOKOnly, "PrtPDFCreator"
Exit Function
End If
.cOption("UseAutosave") = 1
.cOption("UseAutosaveDirectory") = 1
.cOption("AutosaveDirectory") = sPDFPath
.cOption("AutosaveFilename") = checklength
.cOption("AutosaveFormat") = 0 ' 0 = PDF
.cClearCache
End With
wdDoc.ActiveWindow.PrintOut copies:=1, ActivePrinter:="PDFCreator"
Do Until pdfjob.cCountOfPrintjobs = 1
DoEvents
Loop
pdfjob.cPrinterStop = False
Do Until pdfjob.cCountOfPrintjobs = 0
DoEvents
Loop
pdfjob.cClose
Set pdfjob = Nothing
'Return value of the function; signifies successful completion.
OpenSelectedWordDocument = True
'ExitPoint:
' Set wdDoc = Nothing
' Set wdApp = Nothing
'
' Exit Function
'ErrorHandler:
' MsgBox "Sorry, unable to open the Word file. The following error
occurred:" _
' & vbCrLf & Err.Number & vbCrLf & Err.Description
' Resume ExitPoint
End Function
Thanks for the help.
Navin