Index auto "Mark Entry ..." for index

S

Silvio

Hello, I need help in writing a macro that will
automatically "Mark Entry" - for indexing purpose - for
all the words in my document. I was able to make a macro
years ago with MSWord 6 but I am lost using VB in
Wond2003.

I know should be something like :

Selection.MoveRight Unit:=wdWord, Count:=1,
Extend:=wdExtend
'select whatever word is on the right side of the cursor.

ActiveDocument.Indexes.MarkEntry Range:=Selection.Range,
Entry:="Test", _

EntryAutoText:="Test",
'the name of the Mark Entry would be the same of the word
selected, so, the word "Test" in the above example would
actually be a variable (e.g. i = current selected word).

So, what i am missing here is to declare a variable that
would work in this routine and create a loop that would
run to the end of the document "EOF"

Any help to finalize code is apreciate.

Thank you,
Silvio


Help!!!!!!
 
C

Chad DeMeyer

I recommend you don't use the Selection object. Instead, use the For Each
construct, something like this:

Dim oWord As Range
For Each oWord in ActiveDocument.Words
...
your code here
...
Next oWord

Regards,
cjd
 
S

Silvio

Thank you cjd. That helpd however for some reason the
macro seems to work fine just for the first few words and
then starts to select the same word over and over again
and vener ends. Any idea?

Thank you,
Silvio
 
S

Silvio

This is what I have done:

Dim oWord As Range
For Each oWord In ActiveDocument.Words
Selection.MoveRight Unit:=wdWord, Count:=1,
Extend:=wdExtend
ActiveWindow.ActivePane.View.ShowAll = True
ActiveDocument.Indexes.MarkEntry
Range:=Selection.Range, Entry:=Selection.Range, _
EntryAutoText:=Selection.Range
Next oWord
 
C

Chad DeMeyer

Dim oWord As Range
For Each oWord In ActiveDocument.Words
ActiveDocument.Indexes.MarkEntry Range:=oWord, Entry:=Trim(oWord.Text)
Next oWord

regards,
cjd
 
G

Guest

Very nice, however, it does not work for my 29 pages doc.
word stop responding and nothing happend. However, it
does work fine for few lines single page.

:-(
 
C

Chad DeMeyer

Try turning off view of hidden text, as I think that might cause the index
entries you are adding to be added to the Words collection, causing an
endless loop. BTW, if a macro gets stuck in an endless loop, Ctrl+Break
will enable you to End or Debug the macro.

Regards,
cjd
 
S

Silvio

I tried but it does the same thing. After pressing
Ctrl+Break, while executing, I then pressed F8 for step-
by-step execution of the macro. I noticed that it select
the entire text document insted of one word at a time
(the all document text is highlighted in black). I can't
win :-(
 
C

Chad DeMeyer

Okay, one more try:

Dim oWord As Range, iWord as Integer, iCount As Integer
iCount = ActiveDocument.Words.Count
For iWord = 1 To iCount
Set oWord = ActiveDocument.Words(iWord)
ActiveDocument.Indexes.MarkEntry Range:=oWord, Entry:=Trim(oWord.Text)
Next iWord

I tested this, just to be sure. It worked, although as written it doesn't
filter things like sentence punctuation (e.g., periods, commas) and
paragraph marks, both of which Word treats as members of the Words
collection.

Regards,
cjd
 
S

Silvio

It does not work for long documents. MSWord stops
responding. It seems to work well with 1 or 2 pages
documents but NOT for my 29-pages long document. :-(
 
J

Jay Freedman

Hi Silvio,

This may or may not be the problem: The integer variables iCount and
iWord are limited to values up to 32767. Try replacing the line

with this one:

Dim oWord As Range, iWord as Long, iCount As Long
 
S

Silvio

Not working! When I ran the macro the first time it
appears that it is doing somenthing but nothing really
happend. Then when trying to run it for the second time
that's when MSWord stops responding. This is starting to
get frustrating...
 

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