Finding the word-count number for a specific word in a sentence.

T

Tomas

Hi,

I am looking for a command that return a specific words position (in
word-counts, like 'word 5') in a sentence. If possible, return both
the position in word-count as well as the sentence number concerned.

E.g.

"This is a test. Hopefully I get some direction regarding the
test."

Get positions of 'test'

'test' appear 1 sentence, 4 word
'test' appear 2 sentence, 8 word


Any help you could provide would be very much appreciated,

Tomas Nordlander
 
H

Helmut Weber

Hi Tomas,

"Word" and "Sentence" are *fuzzy* concepts of *fuzzy*
natural language, therefore almost impossible to use
in not fuzzy programming.

However, to get you started,
play with this one:

Sub Test()
Dim lngSnt As Long ' counting sentences
Dim rngSnt As Range ' range of a sentence
Dim rngWrd As Range ' range of a word
Dim rngTmp As Range ' a temporary range
Set rngTmp = selection.Range
For Each rngSnt In ActiveDocument.Sentences
lngSnt = lngSnt + 1
rngSnt.Select ' for testing
For Each rngWrd In rngSnt.Words
If rngWrd = "test" Then
rngTmp.start = rngSnt.start
rngTmp.End = rngWrd.End
MsgBox lngSnt & ": " & rngTmp.Words.Count
End If
Next
Next
End Sub

HTH

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Tomas

Helmut,

Thanks for your rapid response! Your code helped me.

Best regards from a rainy Irland,

Tomas
 
D

Doug Robbins - Word MVP

I assume that is *fuzzy* for Ireland.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

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