Results 1 to 18 of 18

Thread: Solved: Move Word by Word throughout the document

  1. #1

    Solved: Move Word by Word throughout the document

    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

  2. #2
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Hi Joey,

    To step through every word you can use something like this:
    [VBA]
    For Each rngWord In ActiveDocument.Words
    lHighlightedWords = lHighlightedWords - (rngWord.HighlightColorIndex <> wdNoHighlight)
    Next
    MsgBox lHighlightedWords & " words are highlighted"[/VBA]

    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:
    [VBA]
    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"[/VBA]
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  3. #3
    VBAX Mentor
    Joined
    Sep 2004
    Location
    Nashua, NH, USA
    Posts
    489
    Location
    Quote Originally Posted by fcoreyesv
    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
    Fastest way is to use the Range object with the Expand method.

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Quote Originally Posted by Howard Kaikow
    Fastest way is to use the Range object with the Expand method.
    Hi Howard,

    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?
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  5. #5
    VBAX Mentor
    Joined
    Sep 2004
    Location
    Nashua, NH, USA
    Posts
    489
    Location
    Quote Originally Posted by MOS MASTER
    Hi Howard,

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

  6. #6
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Quote Originally Posted by Howard Kaikow
    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,

    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!
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  7. #7
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    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
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  8. #8
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Quote Originally Posted by TonyJollans
    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,

    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.. (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.
    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.
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  9. #9
    VBAX Mentor
    Joined
    Sep 2004
    Location
    Nashua, NH, USA
    Posts
    489
    Location
    Quote Originally Posted by MOS MASTER
    Hi Howard,

    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!

    Quote Originally Posted by TonyJollans
    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.

  10. #10
    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.

  11. #11
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    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+.)
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  12. #12
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Quote Originally Posted by TonyJollans
    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,
    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,

    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,
    LOL: Well I've got some chicken left...so if you're still craving at it....
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  13. #13
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Ah, yes, RAM could make a difference - I only have 512K
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  14. #14
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Quote Originally Posted by TonyJollans
    Ah, yes, RAM could make a difference - I only have 512K
    Seams to be the answer...I couldn't explain it otherwise...
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  15. #15
    VBAX Mentor
    Joined
    Sep 2004
    Location
    Nashua, NH, USA
    Posts
    489
    Location
    Quote Originally Posted by MOS MASTER
    @fcoreyesv,

    @Howard,
    LOL: Well I've got some chicken left...so if you're still craving at it....
    Thanx, but I had a chicken sandwich today, and I'll have another tomorrow.

  16. #16
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Ok Bon Appetit!
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  17. #17
    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

  18. #18
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Quote Originally Posted by fcoreyesv
    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,

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

    So Enjoy!
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •