Selecting Multiple Pages in Word from Excel

Selecting Multiple Pages in Word from Excel

Post by TWFyeV » Wed, 15 Jul 2009 06:16:01


I have an Excel script that opens Word, selects some number of lines of text,
cuts the selection, then pastes the information into Excel where the data is
further manipulated.

Instead of selecting lines of text, I would like to change the code to
select pages of text (i.e. the first 100 pages). I found the following code
in another posting in this forum.

Dim rgePages As Range

Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=2
Set rgePages = Selection.Range
Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=4
rgePages.End = Selection.Bookmarks("\Page").Range.End
rgePages.Select

It works great when I run it as a Word script, but I run into trouble when I
bring it into the Excel script. How can I select pages 1-100 in a Word
document from my Excel script?

I'm using VB 6.5. Any suggestions would be appreciated.
 
 
 

Selecting Multiple Pages in Word from Excel

Post by Jay Freedm » Wed, 15 Jul 2009 10:34:52

When you move macro code from one Office application to another, you'll run into
things that have the same name in both applications. You must qualify each
reference to any of these things; otherwise VBA will try to resolve the
ambiguous reference by choosing the one from the current application -- which
will probably be the wrong choice.

Other things will be defined only in the application where the macro was
originally written, but will be undefined in the other application unless you
set a "reference" to the proper place.

In your case, first open the Tools menu in the VBA editor, choose References,
and check the box for "Microsoft Word Object Library". That will make sure that
Word-only items such as wdGoToAbsolute will be defined.

Then you have to declare

Dim rgePages As Word.Range

because there is also an Excel.Range object that has different properties. Also
it wouldn't hurt to write Word.Selection instead of just Selection, although it
isn't strictly necessary.

On Mon, 13 Jul 2009 14:16:01 -0700, MaryS < XXXX@XXXXX.COM >

 
 
 

Selecting Multiple Pages in Word from Excel

Post by TWFyeV » Wed, 15 Jul 2009 22:59:01

Thanks. I knew I needed to reference Word but I just couldn't put all the
pieces together correctly. Now that I look at the corrected code, it all
makes sense. Thanks again.