M
mxs
Hi folks,
I am new here and hoping you can help me out with a very strange
problem I am facing when trying to print Word document out of excel vba
code.
Because my application will be installed on many PCs with different
versions of Word or Excel I need to rely on late binding of Word
objects.
Here's the code which gives me an error "The number must be between
-32765 and 32767." on .printout line. Of course when I saw that I
immediately tried to use integer instead of string for the page range,
but that to my surprise didn't help.
********************
Private Sub PrintRangeTest()
Dim wdApp As Object
Dim wdDoc As Object
On Error GoTo ErrHand
Set wdApp = CreateObject("Word.Application")
Set wdDoc = wdApp.Documents.Add("C:\Documents and
Settings\MSTranik\Desktop\test.doc")
wdDoc.PrintOut Background:=False, Range:=wdPrintFromTo, From:="1",
To:="2"
wdDoc.Close
wdApp.Quit
Set wdApp = Nothing
Set wdDoc = Nothing
Exit Sub
ErrHand:
MsgBox "It didn't work again!!!!!" & Chr(13) & Err.Description
wdDoc.Close
wdApp.Quit
Set wdApp = Nothing
Set wdDoc = Nothing
End Sub
**************************
What actually did help to my surprise was to add reference to
"Microsoft Word Library". I mean it wouldn't surprise me if every other
method or property I used didn't work. This would be clear indication
that there's some binding problem. But that's not the case as every
other method or property of wdApp or wdDoc works without a problem. So
I am really puzzled why .printout just doesn't want to work. If I call
..printout without any parameter (prints all pages at default setting)
it works!!
I am starting to wonder this is one of many MS bugs or I am just going
crazy not seeing an obvious flaw in my code.
Thanks for helping out.
Regards
Marek
I am new here and hoping you can help me out with a very strange
problem I am facing when trying to print Word document out of excel vba
code.
Because my application will be installed on many PCs with different
versions of Word or Excel I need to rely on late binding of Word
objects.
Here's the code which gives me an error "The number must be between
-32765 and 32767." on .printout line. Of course when I saw that I
immediately tried to use integer instead of string for the page range,
but that to my surprise didn't help.
********************
Private Sub PrintRangeTest()
Dim wdApp As Object
Dim wdDoc As Object
On Error GoTo ErrHand
Set wdApp = CreateObject("Word.Application")
Set wdDoc = wdApp.Documents.Add("C:\Documents and
Settings\MSTranik\Desktop\test.doc")
wdDoc.PrintOut Background:=False, Range:=wdPrintFromTo, From:="1",
To:="2"
wdDoc.Close
wdApp.Quit
Set wdApp = Nothing
Set wdDoc = Nothing
Exit Sub
ErrHand:
MsgBox "It didn't work again!!!!!" & Chr(13) & Err.Description
wdDoc.Close
wdApp.Quit
Set wdApp = Nothing
Set wdDoc = Nothing
End Sub
**************************
What actually did help to my surprise was to add reference to
"Microsoft Word Library". I mean it wouldn't surprise me if every other
method or property I used didn't work. This would be clear indication
that there's some binding problem. But that's not the case as every
other method or property of wdApp or wdDoc works without a problem. So
I am really puzzled why .printout just doesn't want to work. If I call
..printout without any parameter (prints all pages at default setting)
it works!!
I am starting to wonder this is one of many MS bugs or I am just going
crazy not seeing an obvious flaw in my code.
Thanks for helping out.
Regards
Marek