PDA

View Full Version : How can i know which Word/Paragraph is selected?



VinoBob
01-05-2010, 08:28 AM
Hello!

I would like to do a simple thing, just select a word or a paragraph in a text, and i want to get its index in the collention. How can i do this?

Thanks!

TonyJollans
01-05-2010, 11:10 AM
'fraid you're out of luck. The elements in the Words collection are Ranges and do not have an index into any collection. What do you want to do with such an index? Maybe there's another way.

VinoBob
01-05-2010, 11:18 AM
I would like to get the paragraph or word where the cursor is currently located.

Lets say that the cursor is in the fifth paragraph at the 20th word in that paragraph then i would like to get in a number(integer, long doesnt matter) its position. something like

i=ActiveDocument.Paragraphs.Current
Debug.Print(Str(i))

5

TonyJollans
01-05-2010, 12:27 PM
Its position with respect to what?

Selection.Words(1).Start

.. will give you the character position in the document - is that what you want?

fumei
01-05-2010, 02:17 PM
with respect to what indeed.

"Lets say that the cursor is in the fifth paragraph at the 20th word in that paragraph then i would like to get in a number(integer, long doesnt matter) its position."

So...what do you want to get? 5 or 20?

"I would like to get the paragraph or word where the cursor is currently located. "


That "or" is a problem. You will have to make up your mind.

However, Tony is of course correct. From what you are apparently asking, there IS no collection to get an index from.

Please be clear as to EXACTLY what you wish to achieve.

You could get a paragraph count from the start of the document to where the Selection (cursor) is. You could get a word count.

Say, in your example, the Selection is in the 20th word of the fifth paragraph.
Dim r As Range
Set r = ActiveDocument.Range(Start:=0, _
End:=Selection.Start)
MsgBox r.ComputeStatistics(wdStatisticParagraphs) + 1
returns: 5.

There you go. You can get the "index' - but it is NOT really - of the paragraph the Selection is in.

However...be very careful. IF the Selection includes the paragraph mark of the current paragraph, the you need to NOT use the +1. IF it does not include the paragraph mark...you DO need the + 1.

To get your 20 count (the 20th word), you could:

1. make a range of the Selection.
2. expand it to the whole paragraph
3. use a different range from the paragraph start to the Selection .Start
4. get a word count.

Dim r As Range
Dim YaddaRange As Range
Set r = Selection.Range
r.Expand Unit:=wdParagraph

Set YaddaRange = ActiveDocument.Range( _
Start:=r.Start, _
End:=Selection.Start)

MsgBox YaddaRange.ComputeStatistics(wdStatisticWords)

Putting it all together....
Option Explicit

Sub FindMeTheNunmbers()

Dim r As Range
Dim YaddaRange As Range
Dim strWordCount As String

Set r = Selection.Range
r.Expand Unit:=wdParagraph

Set YaddaRange = ActiveDocument.Range( _
Start:=r.Start, _
End:=Selection.Start)

strWordCount = YaddaRange.ComputeStatistics(wdStatisticWords)
Set r = Nothing

Set r = ActiveDocument.Range(Start:=0, _
End:=Selection.Start)

MsgBox "Selection is at word " & _
strWordCount & " of paragraph number " & _
r.ComputeStatistics(wdStatisticParagraphs) + 1

End Sub
If the cursor (Selection) is on the 20th word of the fifth paragraph, the above will display:

"Selection is at word 20 of paragraph 5"

Bottom line? Yes, you get significant information,but you have to define precisely what it is you want to get, AND...it takes a brute force method. There is no convenient single index to get the information for you. You have to calculate it.

PLUS.....it will depends on whether of not the Selection includes the paragraph mark, or not. You may also get spurious information if you have any of those "empty" paragraphs from improper use of Styles; whether there are tables between the Selection point and the start of the document (tables can mess up paragraph counts).

In other words, sure, you can play around with numbers, but how useful is it? Who cares? WHY is it important to know the Selection is at the 20th word of the fifth paragraph?

LeeFS
03-29-2010, 09:51 AM
Hello!

I would like to do a simple thing, just select a word or a paragraph in a text, and i want to get its index in the collention. How can i do this?

Thanks!
If I understand you correctly, it's just what led me to this site. See reply by Steiner entitled " GET LINE AND PARAGRAPH NUMBER" - where he gives a few simple functions for getting the current paragraph number (index), etc.

Lee

fumei
03-29-2010, 10:12 AM
"See reply by Steiner "

See it where?????

fumei
03-29-2010, 10:18 AM
Ah, I see. Ok, yes, those are numbers that can be achieved, but, as I stated, it is a brute force method. For example:

GetParNum = rParagraphs.Paragraphs.Count
does indeed get a count, BUT it is not any kind of index number. The function creates a range object from the start of the document (Start:=0) and the selection, and then COUNTS each and every paragraph within that range.

This is exactly the same as my:

Set r = ActiveDocument.Range(Start:=0, _
End:=Selection.Start)