Programming MS Words in VBA

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Have an Access application need to open a MS Words doc with hundreds of pages.
Application has to know every single detail of this doc (e.g. specific text
is there, page count, line count for each page, ...). Upon knowing this info,
TEXT/Images be added to each page depending on the above characteristics of
each page. This is all done via VBA. There must be some lib/ref from Words
obj with those Methods/Properties available.
How can this be done?
 
Hi,
in general - this can be done, but lot of work. You can start with a
following - try to make several steps in Word, with macro recorder switched
on - and then see what kind of VBA code word produce. This code you can also
use in Access, if you add reference to word object library, create
word.application object and open this document
 
Hi!

Did you try Ref. "Microsoft Word 10.0 Object Library"? Maybe that's the one
you need...

A. Pach
 
Thanks Alex,

I am NEW to this VBA, in particular dealing with this OOP, class, methods and
events stuff.
But I have been writing tradition (structural) programming for a while.
I had actually include this MS Word reference and play around with the Macro
recording in Word. There are yet some properties NOT exposed in VBA, I
believe.
Let say I start the MS doc from the beginning of doc and I just want to know
how many lines are there on EACH page. There seems to have NO easy answer
to that (but when are interacting with Words you obviously see the line
count at
the status bar).
This is how I plan to achieve:
1) open the Word doc
2) locate the insertion point at the top of page
3) call a function to get the line count of this current page, somehow
4) repeat step 2) until end of doc
The above may involve the cursor movement (some method I guess) within
a loop. How can it be done?
Of cousre I can trap/snapshot some other stuff on a page but may not
seem straight forward.

Awaiting youtr advice.
QK
 
PMFJI,

You're right. There's no easy answer to the question "how many lines on
each page", because Word doesn't have a concept of a page as a unit (in
the way that old-fashioned word processors had, or DTP programmes have).
Instead, Word lays out pages entirely dynamically.

This means that you can only get at the line number displayed on the
status bar via the Selection object, using
Selection.Information(wdFirstCharacterLineNumber)

It's years since I had to use it, but as far as I can remember
Selection.Information (and anything involving where pages break) is only
reliable if the document is open in page view, in a visible window, and
if the document has been repaginated since the last edit. In these
circumstances the predefined bookmark "\Page" will also be useful: you
might be able to use something like

ActiveDocument.Bookmarks("\Page").Select
Selection.Collapse wdCollapseEnd
NumLinesOnPage = Selection.Information(wdFirstCharacterLineNumber)

If you haven't already done so, go to http://word.mvps.org, which is the
best single source of Word information. It's also worth googling the
microsoft.public.word.* newsgroups, especially
microsoft.public.word.vba.general. This last is a good place to post
specific questions that you can't answer for yourself.
 
Many thanks John,

Today I find something very strange.
In Access (VBA) a Word Doc is created and what I want is to
set the Top and Left margins at the Document level (Whole Doc).
So, the code looks like this..
With ActiveDocument.PageSetup
.LeftMargin = CentimeterToPoints(1.3)
.TopMargin = CentimeterToPoints(2.35)
bra bra bra
ENd With

At run time VBA complains with a run-time error of Data Out Range.
The debug window reveals that VBA appears to have values "initialise"
to 9999999 (a very large value) and it doesn't "accept" any value!

I noticed that the doc is NOT read-only and the same code works OK with
some other docs!
It might have been too much for a novice!

Cheers
QK
 
Back
Top