Create index of all words in document & page No. they appear on

G

Guest

I would like to create a simple index of all the words in a Word document &
at the end of each line show the page number on which that word appears.
 
H

Herb Tyson [MVP]

The best way to do this would probably be to write a macro that selects a
word, creates an index entry, then moves on to the next word, and repeats.
One way to do this would be to record a macro as you perform the steps for
the first one, then modify the macro to add control statements that cause
the macro to repeat until reaching the end of the document.

For help with this, your best best would be to visit one of the communities
that is more geared to using VBA in Word, such as
microsoft.public.word.vba.general or microsoft.public.word.vba.beginners.
 
P

Poprivet

Herb said:
The best way to do this would probably be to write a macro that
selects a word, creates an index entry, then moves on to the next
word, and repeats. One way to do this would be to record a macro as
you perform the steps for the first one, then modify the macro to add
control statements that cause the macro to repeat until reaching the
end of the document.
For help with this, your best best would be to visit one of the
communities that is more geared to using VBA in Word, such as
microsoft.public.word.vba.general or
microsoft.public.word.vba.beginners.

That would probably work, but ... such an index is a monster to make useful.

e.g. an index for the sentence above would contain EVERY word in it! e,g,

that,
would,
probably,
work,
but,
such,
an,
index,
etec
etc
etc..

but alphabetically arranged, or course.

Do that on a whole document and you have a real mess, and a large one at
that <g>.

HTH
Pop`
 
H

Herb Tyson [MVP]

Oh. I fully agree. I was simply providing an answer to the question that was
asked. If someone is assembling a monster, I'm happy enough to show where
the neck bolts are kept. :)

But, on a serious note... sometimes you have to try something in order to
get an idea of how it might be done better.
 
G

Guest

Hi
Thanks for all your help.
Found a macro from PC Magazine that does the listing of the words but
doesn't do the page on which the word appears and I'm afraid that it might be
a problem beyond my abilities. This is the text of the macro and I wonder if
either of you know how to modify it to do the page number on which the word
appears.
Once again thanks

CreateListOfWords macro
PCUser Magazine

To run this macro:

1 First put this code into your Normal.dot file (instructions below).

2 Open a file you want to create a word list from. Run the macro
createListOfWords on the file (Tools, Macro, Macros, createListOfWords and
click Run). You will end up with a list of words only, one per line. You know
the macro is finished when you see a message box on the screen. Wait! It will
take some time to run, depending on the file size and the speed of your
computer.

3 Save this file as a plain text file (File, Save As, from the Save as type
list choose Plain Text (*.txt) and give it a name.

4 Open Excel. Open the saved text file (you may have to choose .txt from the
Excel Files of type list in the Open dialog to see the file).

5 Press Finish when the Import Text dialog appears – there’s nothing you
need do.

6 Run the Excel macro calculateFrequency to calculate the frequencies of the
words in the file.


Problems?
If you get a message saying "The macros in the project are disabled. Please
refer to the online help or documentation of the host application determine
how to enable macros", change the macro security level by choosing Tools,
Macro, Security, Security Level tab. Select the Medium option and click OK.
Exit and restart Word for the change to take place. Now, when you open this
file, or any other file which and contains macros, you'll be prompted to
enable or disable the macros -- to run a macro in a file you must choose
Enable Macros.

Macro code
This is the text of the macro:

Sub CreateListOfWords()
'strip out the word "the" and all punctuation. Place one word per line
Set myRange = ActiveDocument.Content
myRange.Find.Execute FindText:=" the ", ReplaceWith:=" ",
Replace:=wdReplaceAll
myRange.Find.Execute FindText:="^p", ReplaceWith:=" ", Replace:=wdReplaceAll
myRange.Find.Execute FindText:=" ", ReplaceWith:=" ", Replace:=wdReplaceAll
myRange.Find.Execute FindText:=" ", ReplaceWith:="^p", Replace:=wdReplaceAll
myRange.Find.Execute FindText:="^p^p", ReplaceWith:="^p",
Replace:=wdReplaceAll
myRange.Find.Execute FindText:=",", ReplaceWith:="", Replace:=wdReplaceAll
myRange.Find.Execute FindText:=".", ReplaceWith:="", Replace:=wdReplaceAll
myRange.Find.Execute FindText:="!", ReplaceWith:="", Replace:=wdReplaceAll
myRange.Find.Execute FindText:=";", ReplaceWith:="", Replace:=wdReplaceAll
myRange.Find.Execute FindText:=":", ReplaceWith:="", Replace:=wdReplaceAll
myRange.Find.Execute FindText:="(", ReplaceWith:="", Replace:=wdReplaceAll
myRange.Find.Execute FindText:=")", ReplaceWith:="", Replace:=wdReplaceAll
myRange.Find.Execute FindText:="~", ReplaceWith:="", Replace:=wdReplaceAll
myRange.Find.Execute FindText:="1", ReplaceWith:="", Replace:=wdReplaceAll
myRange.Find.Execute FindText:="2", ReplaceWith:="", Replace:=wdReplaceAll
myRange.Find.Execute FindText:="3", ReplaceWith:="", Replace:=wdReplaceAll
myRange.Find.Execute FindText:="4", ReplaceWith:="", Replace:=wdReplaceAll
myRange.Find.Execute FindText:="5", ReplaceWith:="", Replace:=wdReplaceAll
myRange.Find.Execute FindText:="6", ReplaceWith:="", Replace:=wdReplaceAll
myRange.Find.Execute FindText:="7", ReplaceWith:="", Replace:=wdReplaceAll
myRange.Find.Execute FindText:="8", ReplaceWith:="", Replace:=wdReplaceAll
myRange.Find.Execute FindText:="9", ReplaceWith:="", Replace:=wdReplaceAll
myRange.Find.Execute FindText:="^t", ReplaceWith:="", Replace:=wdReplaceAll

'remove all 2 and 1 letter words
fcount = ActiveDocument.Paragraphs.Count
For i = fcount - 1 To 1 Step -1
If Len(ActiveDocument.Paragraphs(i).Range) < 4 Then
ActiveDocument.Paragraphs(i).Range.Delete
ElseIf ActiveDocument.Paragraphs(i).Range = "the" Then
ActiveDocument.Paragraphs(i).Range.Delete
End If
Next i
MsgBox "Save the file as a text file and then open it in Excel and run the
frequency macro."
End Sub

You can add more lines to the first part of the macro to strip out other
characters as required. Right now it strips out numbers and basic punctuation
but you may find you use other characters in your text so feel free to add
lines to strip them out too.

Add the macro to Normal.dot
To add this macro to your own Normal.dot file so it is always available when
you start Word, with the file createlistofwords.doc open on the screen,
choose Tools, Macro, Macros, Organizer, Macro Project Items tab. You should
see a dialog with createlistofwords.doc on the left and Normal.dot on the
right. Select the createlistofwords.doc option in the box on the left and
click Copy to move it to the box on the right. Click Close to finish.

Add it to a toolbar button
To add a toolbar button to run this macro, right click any toolbar and
choose Customize, Commands tab. Scroll down to locate the Macros entry in the
Categories list and from the Commands list drag the Normal.
createlistofwords.doc. createlistofwords.doc entry up and onto the toolbar.
Right click the new button and in the Name area give it a shorter name and
click Close in the Customize dialog.
 

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