PDA

View Full Version : Solved: Move Word by Word throughout the document



fcoreyesv
04-15-2005, 02:31 PM
Hello,

I would like to know the way to walk throughout a document word by word.
And being able to identify if that word is highlighted or not.

The purpose is to count the words which are highlighted in a given document.

Thanks in Advance for your help and comments.

Joey:hi:

TonyJollans
04-15-2005, 03:59 PM
Hi Joey,

To step through every word you can use something like this:

For Each rngWord In ActiveDocument.Words
lHighlightedWords = lHighlightedWords - (rngWord.HighlightColorIndex <> wdNoHighlight)
Next
MsgBox lHighlightedWords & " words are highlighted"

but it would be pretty slow on a document of any size and you'ld probably find it quicker to use Find. Find, however, will count a word more than once if more than one part of it is highlighted so a slight correction is needed:

Selection.HomeKey wdStory
Selection.Find.ClearFormatting
Selection.Find.Highlight = True
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Do While Selection.Find.Execute
lHighlightedWords = lHighlightedWords + Selection.Words.Count
If Save = Selection.Words(1).Start Then _
lHighlightedWords = lHighlightedWords - 1
Save = Selection.Words(Selection.Words.Count).Start
Loop
MsgBox lHighlightedWords & " words are highlighted"

Howard Kaikow
04-16-2005, 10:04 AM
Hello,

I would like to know the way to walk trhoughout a document word by word.
And being able to identify if that word is highlighted or not.

The purpose is to count the words which are highlighted in a given document.

Thanks in Advance for your help and comments.

Joey:hi:

Fastest way is to use the Range object with the Expand method.

MOS MASTER
04-16-2005, 03:05 PM
Fastest way is to use the Range object with the Expand method.
Hi Howard, :D

Well I've done a little test (see attacht document) and I found that Tony's Find sub was over 50% faster then using the words or range collection.

I've added a sub that Iterates through the range with the expand Method but that sub is about running the same speed as tony's Words collection loop.

I'm shure you would program the sub different but that's the interesting part..
Can you program the Range sub using the expand method so that it's faster than the Find object sub?

@Tony,
I've altered you're sub a little bit because it was giving me one highlighted word to little...It also improved the total speed execution when commenting those lines out..

Could you guys both take a look at it to see if the code to be improved..Or any one else for that mather? :rofl:

Howard Kaikow
04-16-2005, 03:59 PM
Hi Howard, :D

Well I've done a little test (see attacht document) and I found that Tony's Find sub was over 50% faster then using the words or range collection.

I've added a sub that Iterates through the range with the expand Method but that sub is about running the same speed as tony's Words collection loop.

I'm shure you would program the sub different but that's the interesting part..
Can you program the Range sub using the expand method so that it's faster than the Find object sub?

@Tony,
I've altered you're sub a little bit because it was giving me one highlighted word to little...It also improved the total speed execution when commenting those lines out..

Could you guys both take a look at it to see if the code to be improved..Or any one else for that mather? :rofl:

In general, Expand will be faster.

Find may be faster when you are looking for specific criteria, such as fonts or styles or ...

Almost never use the Selection object.

MOS MASTER
04-17-2005, 03:26 AM
In general, Expand will be faster.

Find may be faster when you are looking for specific criteria, such as fonts or styles or ...

Almost never use the Selection object.
Hi Howard, :D

True; like I mentioned before I try to avoid the Selection Object as much as possible. Not only to gain on speed but it has brought me to strange results in the past.

Off course it's always a good thing to experient with different approaches when you're programming a specific task. (there's almost always a tool that does the job the best)

I was triggerd by you're call that the Range Object with the Expand method would be the fastes way to achieve this.
I've never used this object to do this perticular task so I tried it out..

I'm going to try it in the future on other jobs to see if it's speeding things up.

I'm still wondering if anyone else got another approach on frying this chicken because it still seams to run a little bit slow. (on big documents with lot's of highlighting)

See Yah! :thumb

TonyJollans
04-17-2005, 04:07 AM
Hi Joost,

On your test document I found using Find consistently a little faster (perhaps 6 or 7%). When I did it originally I just ran a couple of quick tests with far fewer words highlighted and I saw a very noticeable difference without a need for a timer. I guess the gain (or otherwise) depends on what percentage of the document is highlighted.

The reason for my adjustment was to cope with individual words with multiple discontiguous highlighted sections e.g. highlighted (where the red indicates highlighting - I'm sure there's a way to post highlighted text but I don't know it off the top of my head); you don't have any of these in your test and it would also appear from your test that another adjustment is needed.

As for using the Selection object, I do agree that it should be avoided in general but it is not always the case in Word. In my experience, Find is one of those things which often seems to work best with the Selection; there seem to be some oddities when trying to use it with other Range objects like it is in some way always tied to the Selection.

I find the Range / Expand approach slightly convoluted in this case although there may be a way to use it effectively. I'm still thinking about other methods

MOS MASTER
04-17-2005, 04:38 AM
Hi Joost,

On your test document I found using Find consistently a little faster (perhaps 6 or 7%). When I did it originally I just ran a couple of quick tests with far fewer words highlighted and I saw a very noticeable difference without a need for a timer. I guess the gain (or otherwise) depends on what percentage of the document is highlighted.

Hi Tony, :D

This is strange because on my computer you're find sub is really fast compared to the other 2..the times: (Names off toolbar buttons)
Procedure 1: 12,83984 Seconds
Procedure 2: 5,6875 Seconds
Procedure 3: 13,16016 Seconds

So you see on my computer the noticeable difference is very large..:rofl: (but every computer is different off course)


The reason for my adjustment was to cope with individual words with multiple discontiguous highlighted sections e.g. highlighted (where the red indicates highlighting - I'm sure there's a way to post highlighted text but I don't know it off the top of my head); you don't have any of these in your test and it would also appear from your test that another adjustment is needed.
Yes in case off words being partially marked the sub would need alteration. (The question off course must be do you really want to take this into consideration?)


As for using the Selection object, I do agree that it should be avoided in general but it is not always the case in Word. In my experience, Find is one of those things which often seems to work best with the Selection; there seem to be some oddities when trying to use it with other Range objects like it is in some way always tied to the Selection.

Yes Find is one off those execeptions..and there are more cases on witch you'd better use the selection object...
But I guess we alle agree on not using this object if there is a better alternative. :whistle:


I find the Range / Expand approach slightly convoluted in this case although there may be a way to use it effectively. I'm still thinking about other methods

I'm thinking off those as well Tony and will take it in consideration in future speed tests.

Howard Kaikow
04-17-2005, 10:30 AM
Hi Howard, :D

True; like I mentioned before I try to avoid the Selection Object as much as possible. Not only to gain on speed but it has brought me to strange results in the past.

Selection depends too much on various setting the user might be using in Tools | Options. To avoid such issues one has to initialize the envirinment with known settings for, anong others, whether typing replaces selection.


Off course it's always a good thing to experient with different approaches when you're programming a specific task. (there's almost always a tool that does the job the best)

Over time, one learns these things from seeing others posts.

There are general rules, but oh my there are exceptions as we recently learned reharding te ProofReadingErrors collection.


I was triggerd by you're call that the Range Object with the Expand method would be the fastes way to achieve this.
I've never used this object to do this perticular task so I tried it out..

I'm going to try it in the future on other jobs to see if it's speeding things up.

My experience has been that using Expand is the fastest wat to move thru a Selectio/Range, other things being equal.

I'll comment on Tony's case in a response to his reply.


I'm still wondering if anyone else got another approach on frying this chicken because it still seams to run a little bit slow. (on big documents with lot's of highlighting)

Dag nab it!
Now I have this urge to eat chicken and I've got none in the house!


Hi Joost,

On your test document I found using Find consistently a little faster (perhaps 6 or 7%). When I did it originally I just ran a couple of quick tests with far fewer words highlighted and I saw a very noticeable difference without a need for a timer. I guess the gain (or otherwise) depends on what percentage of the document is highlighted.

For your app, Find will be faster than using Expand because Find has the built-in smarts to look for formatting, highlighting, etc.

With Exapand, one has to do the testing oneself.


As for using the Selection object, I do agree that it should be avoided in general but it is not always the case in Word. In my experience, Find is one of those things which often seems to work best with the Selection; there seem to be some oddities when trying to use it with other Range objects like it is in some way always tied to the Selection.

My experience has been that Selection.Find may be faster than Range.Find only when something must selected after being found.


I find the Range / Expand approach slightly convoluted in this case although there may be a way to use it effectively. I'm still thinking about other methods

For your app Find should be faster.

fcoreyesv
04-18-2005, 10:35 AM
Thank you guys.

I am testing all of the procedures. Look slike the 2nd is the fastest. The document I am analysing is 750+ pages long with half a million words.

Looks that by selection chunks of highlighted words it works faster (in theory it should).

Thanks to all.

TonyJollans
04-18-2005, 11:08 AM
Hi Joost,

Consistently in my tests with you document, I get results similar to these (average of 3 runs of each):

Procedure 1: 14.27017
Procedure 2: 12.99479
Procedure 3: 15.14063

I know different machines/environments can be different but am surprised at this. Might there be a setting somewhere that affects speed of Find?

(I am running WIn XP SP1, Office 2K3 (both installed about 2 weeks ago so a pretty clean machine), on an Athlon 1G+.)

MOS MASTER
04-18-2005, 11:27 AM
Hi Joost,

Consistently in my tests with you document, I get results similar to these (average of 3 runs of each):

Procedure 1: 14.27017
Procedure 2: 12.99479
Procedure 3: 15.14063

I know different machines/environments can be different but am surprised at this. Might there be a setting somewhere that affects speed of Find?

(I am running WIn XP SP1, Office 2K3 (both installed about 2 weeks ago so a pretty clean machine), on an Athlon 1G+.)
@fcoreyesv, :D
You're welcome...looking at you're spec's...you'd sure need the fastes sub to help you on you're way!

Please report back if you stumble on something faster.

Hi Tony, :D

Yes it's a very big difference between you're results and mine. but I know the target machine makes the differance.

I don't know what you're Ram-size is but mine is 1024 PC 2700 DDR Ram (266)
My Laptop is a P4 2GH
I have XP (SP2) Office 2003 PRO on it.

So I think looking at the machines can explain the results. (But if someone has the same machine as mine please do the test and post the results)

@Howard, :D
LOL: Well I've got some chicken left...so if you're still craving at it....:rofl:

TonyJollans
04-18-2005, 12:06 PM
Ah, yes, RAM could make a difference - I only have 512K

MOS MASTER
04-18-2005, 12:10 PM
Ah, yes, RAM could make a difference - I only have 512K
Seams to be the answer...I couldn't explain it otherwise...

Howard Kaikow
04-18-2005, 02:26 PM
@fcoreyesv, :D

@Howard, :D
LOL: Well I've got some chicken left...so if you're still craving at it....:rofl:

Thanx, but I had a chicken sandwich today, and I'll have another tomorrow.

MOS MASTER
04-18-2005, 02:28 PM
Ok Bon Appetit! :whistle:

fcoreyesv
04-21-2005, 09:12 AM
Thanks guys.

The second option worked just fine. analysing the document in 9 minutes (750 pages) time good enough to take a break and get a soda.

Thanks for the code and your comments.

Jose Reyes

MOS MASTER
04-24-2005, 06:37 AM
The second option worked just fine. analysing the document in 9 minutes (750 pages) time good enough to take a break and get a soda.

Hi Jose, :D

Glad you're happy. Still seams like a lot of time but a brother needs a break once in a while...

So Enjoy! :thumb