Word Number Index

G

George Lee

If I find a word in the document (such as through myWord.Find.Execute), how
can tell which word number this is within the whole document?
 
H

Helmut Weber

Hi George,

what is myWord?
The selection range?

Then like this, according to Word's definition of a word:

Sub test000123()
Dim rSlc As Range
Set rSlc = Selection.Range
With rSlc.Find
.Text = "quick"
If .Execute Then
rSlc.Start = ActiveDocument.Range.Start
MsgBox rSlc.Words.Count
End If
End With
End Sub

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
J

Jonathan West

George Lee said:
If I find a word in the document (such as through myWord.Find.Execute),
how
can tell which word number this is within the whole document?

Take a look at this article

Determine the index number of the current paragraph, table, section ...
http://www.word.mvps.org/FAQs/MacrosVBA/GetIndexNoOfPara.htm


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
G

George Lee

Sorry if that was unclear, myWord is a Range.

I understand your code (rSlc.Words.Count) shows the number of words in the
selection range and you could even specify the exact word (rSlc.Words(x)).
However, I was asking to find out what that word is in the overall document.
Some structures have an index concept (that that item is the x-th item in the
document) and I was wanting to get that information for a word.

For other reasons I need to use a seperate range.
 
H

Helmut Weber

Hi George,

have you tried it?
I understand your code (rSlc.Words.Count) shows the number
of words in the selection range

The word you were searching for,
is the nth word in the documents main story.
As before counting the words, I reset the range's
start to be the start of the document's range.

rSlc.Start = ActiveDocument.Range.Start

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
G

George Lee

Thanks to you and Helmut.

It's one of those things that isn't intuitive but after thinking about it,
it does seem obvious, doesn't it?
 

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