Need Index # of current line/para/word

A

Avi Mak

Hi all,

Same question as before: I need the Index # of the line/para/word of
the current selction so I know where I am after a Search.

I tried this form, as suggested:

ActiveDocument.Range(0, Selection.Range.End).Sentences­­.Count

(boy, that's convoluted - and it probably goes and counts...)

together with:

ActiveDocument.Sentences.Count (changing total)

It is way too slow.

Isn't there anything faster?

tia

Avraham.
 
J

Jonathan West

Hi Avi,

There is no faster way I know of. is there any particular reacon you need to
know the number of sentences rather than something else, such as the number
of paragraphs or characters?


--
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


Hi all,

Same question as before: I need the Index # of the line/para/word of
the current selction so I know where I am after a Search.

I tried this form, as suggested:

ActiveDocument.Range(0, Selection.Range.End).Sentences­­.Count

(boy, that's convoluted - and it probably goes and counts...)

together with:

ActiveDocument.Sentences.Count (changing total)

It is way too slow.

Isn't there anything faster?

tia

Avraham.
 
A

Avi Mak

is there any particular reacon you need to
know the number of sentences rather than something else, such as the
number
of paragraphs or characters?

Thank you. It doesn't have to be the number of sentences (actually
current sentence index, to be more accurate); it can also be paragraphs
or characters or words. I just need to know where I am in the document
after a search hit so I can easily display a progress bar. But I can
see that obtaining the current paragraph / character / word index works
on the same principle as finding the current sentence index, so I don't
think there is much point in trying. Setting up every experiment takes
that bit more time. Do you think that reading* the paragraph index is
faster than reading* the sentence index?

*Actually more like *calculating by VBA*, as far I can see.

tia

Avraham.
 
H

Helmut Weber

Hi Avi,

1 GHz, 768 MB Ram, 200 pages document,
insertion point on the last page

MsgBox ActiveDocument.Range(0, Selection.Range.End).Words.Count

I get the answer 27785 in a second.

Same with paragraphs, same with sentences.

Processing lines is something completely different,
they depend on e.g. pagesetup, printer, font etc.
ActiveDocument.Range(0, Selection.Range.End).Sentences.Count

convoluted?

How about this one, which is essentially the same.
Just a bit easier to read, maybe.

Dim rDcm As Range
Set rDcm = ActiveDocument.Range
rDcm.End = Selection.Range.End
MsgBox rDcm.Words.Count


Greetings from Bavaria, Germany

Helmut Weber, MVP, WordVBA

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

Jonathan West

Avi Mak said:
know the number of sentences rather than something else, such as the
number
of paragraphs or characters?

Thank you. It doesn't have to be the number of sentences (actually
current sentence index, to be more accurate); it can also be paragraphs
or characters or words. I just need to know where I am in the document
after a search hit so I can easily display a progress bar. But I can
see that obtaining the current paragraph / character / word index works
on the same principle as finding the current sentence index, so I don't
think there is much point in trying. Setting up every experiment takes
that bit more time. Do you think that reading* the paragraph index is
faster than reading* the sentence index?

*Actually more like *calculating by VBA*, as far I can see.

The current character index can simply be obtained by reading Selection.End.
I think you will find that this is significantly faster. If all you need is
a rough & ready progress indication, then I think you will find that this
fits the bill.


--
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
 
A

Avi Mak

same with sentences
Well, I am sure that counting sentences does not work very well on our
machines, so if paras and words take as long then they also won't work.
Maybe I will get around to trying them later.

Thanks.

Avraham.
 
A

Avi Mak

Thanks.

I tried it just to step thru it. It DOES seem very fast, but I haven't
got around to putting it in my program yet.

Btw, does this give character index with spaces or without spaces? In
the Doc Properties statistics window both values are given, neither of
them the same as that using ActiveDocument.Characters.Count.

One problem I found: the Selection.End eventually gives a larger figure
than ActiveDocument.Characters.Count(!) I assume that this is because
my processing is deleting part of the document as it goes on, and so
the indexing is getting confused. Also, I therefore need to recalculate
ActiveDocument.Characters.Count each time, and this takes a long time
to do (any faster way?). So my process bar can never be accurate:
either it will fall short or go off the edge.

The stop-gap method I am using so far is to prepocess by searching my
doc using my srch expression and count the number of hits I get, *but
without doing the processing*. This is very fast. Then I go back and
redo the whole loop this time *with* the processing and also recounting
the hits, and so the progress bar is the comparison between the two
numbers. Unfortunately, also in this method I get more hits on the 2nd
pass. Again, this is because the processing is consuming (deleting)
part of the doc, and it is very difficult to work out where exactly the
extra hits are coming from.

Thanks.

Avraham.
 
J

Jonathan West

Avi Mak said:
Thanks.

I tried it just to step thru it. It DOES seem very fast, but I haven't
got around to putting it in my program yet.

Btw, does this give character index with spaces or without spaces?

With spaces
In
the Doc Properties statistics window both values are given, neither of
them the same as that using ActiveDocument.Characters.Count.

I know. ActiveDocument.Characters.Count includes such things as paragraph
marks, table cell end markers and shape anchors in the list of characters,
which are omitted from the Properties window count.

One problem I found: the Selection.End eventually gives a larger figure
than ActiveDocument.Characters.Count(!) I assume that this is because
my processing is deleting part of the document as it goes on, and so
the indexing is getting confused. Also, I therefore need to recalculate
ActiveDocument.Characters.Count each time, and this takes a long time
to do (any faster way?). So my process bar can never be accurate:
either it will fall short or go off the edge.

First, I would recommend you use ActiveDocument.Range.End instead. You are
then cure to be comparing apples with apples. But if you are editing the
document, the number of characters is changing, so if you want to get a
proper progress indication, you will need to read the value of
ActiveDocument.Range.End as well as Selection.End each time you want a
progress update.
The stop-gap method I am using so far is to prepocess by searching my
doc using my srch expression and count the number of hits I get, *but
without doing the processing*. This is very fast. Then I go back and
redo the whole loop this time *with* the processing and also recounting
the hits, and so the progress bar is the comparison between the two
numbers. Unfortunately, also in this method I get more hits on the 2nd
pass. Again, this is because the processing is consuming (deleting)
part of the doc, and it is very difficult to work out where exactly the
extra hits are coming from.

Well, if you suddenly jump from 90% to finished, I doubt that most people
would mind, so it might be reasonable to stay with this.


--
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
 

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