Strange issue Excel >> Word automation

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
 
A

aidan.heritage

You have referred to

wdPrintFromTo

which is a Word constant - replace it with the numeric equivalent and
you should be OK (which is why it works when you add the reference to
Word)
 
M

mxs

But where do I find what is the numerical equivalent? I tried 1,2 or 3
but no luck. VBA clearly doesn't like that. No luck via web search
either.

Thanks

Marek
 
J

Jeff Standen

By opening the Word VBE and putting this

?wdprintfromto

in the immediate pane, it gives the answer 3.

Jeff
 
M

mxs

Hi Jeff,

only after reading your suggestion I realized that object browser
reveals all the numeric constant equivalents as well.

When I tried yesterday 3 it didn't work, but that was only because I
had the From and To as integers instead of strings. I was desperate
trying everything and that's how I messed it up.

Again, thanks for your help, it's working now.

Marek
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top