Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / January 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tomas - 03 Jan 2007 16:11 GMT
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
Helmut Weber - 03 Jan 2007 16:42 GMT
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

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Tomas - 03 Jan 2007 17:05 GMT
Helmut,

Thanks for your rapid response! Your code helped me.

Best regards from a rainy Irland,

Tomas

> Hi Tomas,
>
[quoted text clipped - 33 lines]
> Win XP, Office 2003
> "red.sys" & Chr$(64) & "t-online.de"
Doug Robbins - Word MVP - 03 Jan 2007 17:46 GMT
I assume that is *fuzzy* for Ireland.

Signature

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

> Helmut,
>
[quoted text clipped - 41 lines]
>> Win XP, Office 2003
>> "red.sys" & Chr$(64) & "t-online.de"
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.