Access with Word Automation

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 17:36
Joined
Feb 28, 2001
Messages
30,558
This is kind of long, but I've been working on it for a while and getting headaches. Here is what has been going on...

I am building a database to help me analyze some large Word documents that contain references to certain key words. I am developing in the AC97/Word97 environment. The documents are designed with a master-document/sub-document structure. More than one such document exists at the moment, each with between 16 and 22 chapters. The shortest document has a word count in excess of 120,000. The longest is over 160,000.

The goal of this exercise is a table that will contain a keyword index, document index, and chapter index for each case where the keyword appears in a chapter of the document. My report will show me chapters where the keyword is referenced. In other words, a cross-reference of keyword to chapter. I'm storing index numbers because I have lookup tables for each item and only need to store the indexes in order to build the final report.

The process is being controlled by a form in Access. Code in the class module of the form drives the relevant file scans, Word activation, etc. off a command button. Breakpoints confirm that the button kicks off the scanning procedure.

I open the Word document using the CreateObject method for "Application.Word" followed by the .Open method on the separate chapter document. This becomes my ActiveDocument in Word context. The local debug window confirms that there are times when the Word app object is open and has some number of paragraphs. The paragraph count is reasonable for the given document, though I haven't actually sat down and counted paragraphs. If I open Word through another window, I find that my document is visible from the Menu Bar's Window drop-down. Therefore I am quite sure that the file is being opened and closed through Word.

Where I come into grief is when I try to implement the cross-reference scanner that searches text. In general, I have tried several different approaches. The idea is that for

objWordDoc.Paragraphs(loPghNum).Range.Text

I want to find if a particular word exists in that paragraph. The words I seek come from a table. I have verified that the proper names are readable through the recordsets I use to feed the scanner module. (Again, using a breakpoint to look at variables at various parts of the scan.) So I know I'm looking for a non-null bit of text.

I have tried the Word Find object using .Paragraphs(index).Find with the proper name or keyword as an input to the find. The .Find parameters were set so the search was limited to the range of that paragraph. I.e. it is either in that paragraph or not. Since I am visiting each paragraph in turn, I don't want the search going outside the paragraph's range until I tell it to do so.

Anyway, after the .Find.Execute, I check the Found property - but it doesn't seem to find anything. I.e. .Found is always false even if I open the document directly and read the paragraph. There is the name I am seeking, big as life! And the .Find input was valid, too. Well, I got frustrated when that didn't work worth a poop.

So then I tried copying each paragraph in turn to a string so I could try an Instr function. But I opened the document read-only and it is telling me that

stRange = objWordDoc.Paragraph(index).Range.Text

won't work because the document is read-only. Well OF COURSE it is read-only. I don't want to edit it, I just want to read it. When I try to open the document with ReadOnly = false, I get another error because (get set to barf...) the REAL documents are in WordPerfect. I am opening the documents with automatic, silent conversion to Word but do not want to save anything!

So then I tried to do a .Copy of the paragraph and .Paste the text into a (invisible) text box. Well, ...Paragraph(index).Copy works great according to the Clipboard. But the Access text box doesn't seem to understand the .Paste or .PasteSpecial methods. (Yes, before you ask, Word and Office references are checked and not MISSING in my references list.) Is there something else I should have checked in Tools>>References to define .Paste or .PasteSpecial operations?

My Developer's Handbook Library (Access volume OR Office volume) isn't being very helpful here so far. Does anyone have any suggestions on where to go next?

All I really want is for Access to read the lousy document a paragraph at the time, search the text for keywords in my list, and make notes when it finds them. How hard can that be? (Obviously, given the above tale of woe, harder than it looks.)
 
Doc Man, I sympathize heavily. Since you're the one who's dived into this headfirst, I fear we will be the ones to benefit from your research, however.

If it's any consolation... I normally find that when I'm the one knee deep in something confusing, something simple jumps up and shows itself. Or, it turns out to be something I've overlooked.

Case in point:
My rambling on the other day about RecordSet Indexes Collection...those extra, strangely named 'Indexes' I was seeing were actually 'Relations'. Relations are part of the Indexes collection!

*smack*

Anyway, much luck, and I will read with interest. If I see a spot where I can lend a hand, you can count on seeing it.

If I were you, I would be nearly incensed that the Found property keeps coming up False and I would likely concentrate my stubborn efforts there, or look backwards just a bit into the workings of the Find object. Seems to me that if you can get Found to display properly, it would save a whole lot of effort (and overhead?) vs copying and searching each paragraph in turn.

Shep
 
Shep, I appreciate your sympathy.

I will be revisiting a lot of stuff to see if I didn't hold my mouth right when doing it before.

If I ever get it figured out, I'll post something.

In the meantime, any other takers?
 
Shep, as to being incensed...

This exercise has convinced me more than ever that Office is just another Bill Gates product, more to be pitied than censured.

Well, maybe equally censured.

All I can say is that if Bill Gates were walking by just at the time I was holding a lemon creme pie piled high with whipped cream, I would save myself quite a few calories and get a lot of personal satisfaction.

On the other hand, if it were a CHOCOLATE cream pie, I wouldn't waste it on him. But I wouldn't share, either.
 
When people talk about someone coming to the workplace carrying a lot of baggage... in my case they are talking about under my eyes.

But at least I got SOMETHING to work.

It turns out that the documentation under the Word97 help files doesn't make a distinction that it really ought to make, and the Object Browser and compiler don't help worth a poop, either. So if you don't read between the lines and remember that MS Help files often are double-dipped (i.e. one page referred to from two different but somewhat related topics), you can get BADLY confused about which parameters refer to which methods and objects.

OK, here is what went wrong.

I was using a construct something like

document-object.range-object.Find.Execute FindText:=string, Forward:=True, ...

and a bunch of other stuff that made some sense in the context of my limited search. Well, that was wrong. What worked is

With document-object.range-object.Find

.Text=variable
.Forward=True
.MatchCase=False
.Wrap=wdFindStop
.Execute
boResult = .Found

End With

Here is what I THINK happened. When I wrote out the long form on a line (with continuations etc), the compiler WOULD NOT recognize .Text as a property. The auto-fill feature for object properties didn't present .Text as an option. But it WOULD recognize FindText. So that is what I used in my first try.

Unfortunately, FindText is the keyword you use when you are doing a Find/Replace operation. (Matched with, you guessed it, .ReplaceText) A Find/Replace is what I wasn't doing. And darned if .FindText vs .Text doesn't seem to make a difference! As I said, the compiler and object browser didn't report .Text as a valid property for the .Find.Execute - but it IS a valid property for the Find object when using the With clause.

Talk about a mess! But at least I was able to get past the drop-dead point after loss of only a few hours of sleep. Hence the comments about bags under my eyes. The rest of my problem is just a bunch of DLookups and stuff. I'll take care of that later in the week if/when I catch up on some sleep.
 
Excellent work. We have benefited from your research. hah!

Remember, everyone:

A .FindText is used in a Find/Replace operation, in conjunction with a .ReplaceText. Don't confuse it with a plain old .Find.

(This Word automation stuff is not easy. Just try finding context-sensitive interfacing for the properties and methods of the Word Object library! Or, just ask Doc Man).

Shep
 
(This Word automation stuff is not easy. Shep[/QUOTE]

haha you can say that again. Thanks for reply, I will keep on trucking.
 

Users who are viewing this thread

Back
Top Bottom