Discussion:
Selecting Multiple Pages in Word from Excel
(too old to reply)
MaryS
2009-07-13 21:16:01 UTC
Permalink
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.
Jay Freedman
2009-07-14 01:34:52 UTC
Permalink
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.

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all
may benefit.
Post by MaryS
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.
MaryS
2009-07-14 13:59:01 UTC
Permalink
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.
Post by Jay Freedman
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.
--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all
may benefit.
Post by MaryS
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.
Continue reading on narkive:
Loading...