MS Office Forum / Word / Programming / January 2006
End of document marker?
|
|
Thread rating:  |
Karen - 20 Jan 2006 17:33 GMT Hi All,
I need some help with some code that sometimes works and sometimes enters an infinite loop. Using the 'find' feature in Word, I want to find selections highlighted in either red or turquoise. Depending on the color of the highlight, I increment a counter either full value or half value. Sometimes it works, sometimes it just keeps looping. I think the basic problem is that I some way of find the end of the document to specify the range.
Here's the code:
Sub TestScoring() Dim newcount As Single
'go to top of doc Selection.HomeKey Unit:=wdStory
'clear previous find Selection.Find.ClearFormatting 'set search for highlight on Selection.Find.Highlight = True 'run search Selection.Find.Execute 'while highlights are still found While Selection.Find.Found = True 'if selection is red then count full value If Selection.Range.HighlightColorIndex = wdRed Then newcount = newcount + 1 'if selection is turquoise then count half value ElseIf Selection.Range.HighlightColorIndex = wdTurquoise Then newcount = newcount + 0.5 End If 'continue same search Selection.Find.ClearFormatting Selection.Find.Highlight = True Selection.Find.Execute 'end loop when no more found Wend 'move back to top of doc when done Selection.HomeKey Unit:=wdStory MsgBox (newcount)
End Sub
Any ideas?
Karen
Greg - 20 Jan 2006 18:03 GMT Karen,
Couldn't duplicate your loop. Try:
Sub TestScoring2() Dim newcount As Single Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find .ClearFormatting .Replacement.ClearFormatting .Highlight = True Do While .Execute If oRng.HighlightColorIndex = wdRed Then newcount = newcount + 1 ElseIf oRng.HighlightColorIndex = wdTurquoise Then newcount = newcount + 0.5 End If oRng.Collapse direction:=wdCollapseEnd Loop End With MsgBox newcount End Sub
Karen - 20 Jan 2006 23:05 GMT Dave,
Thanks very much, I tried yours and went into an infinite loop. This is very frustrating!
Karen
Karen,
Couldn't duplicate your loop. Try:
Sub TestScoring2() Dim newcount As Single Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find .ClearFormatting .Replacement.ClearFormatting .Highlight = True Do While .Execute If oRng.HighlightColorIndex = wdRed Then newcount = newcount + 1 ElseIf oRng.HighlightColorIndex = wdTurquoise Then newcount = newcount + 0.5 End If oRng.Collapse direction:=wdCollapseEnd Loop End With MsgBox newcount End Sub
Karen - 20 Jan 2006 23:06 GMT Hi Greg,
Thanks very much, I tried yours and went into an infinite loop. This is very frustrating!
Karen
Karen,
Couldn't duplicate your loop. Try:
Sub TestScoring2() Dim newcount As Single Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find .ClearFormatting .Replacement.ClearFormatting .Highlight = True Do While .Execute If oRng.HighlightColorIndex = wdRed Then newcount = newcount + 1 ElseIf oRng.HighlightColorIndex = wdTurquoise Then newcount = newcount + 0.5 End If oRng.Collapse direction:=wdCollapseEnd Loop End With MsgBox newcount End Sub
Greg - 20 Jan 2006 23:19 GMT E-mail me your document (without the macro). gmaxey@mvps.org
Karen - 20 Jan 2006 23:29 GMT Hi Greg,
Done. Any suggestions are welcome.
Karen Hagerman University of Phoenix Online Faculty
kahager@email.uophx.edu
Phone Number: 206-309-0438 (Leave a Message)
UOP Tech Support: 800-800-3493
E-mail me your document (without the macro). gmaxey@mvps.org
Dave Lett - 20 Jan 2006 18:11 GMT Hi Karen,
I've revised the code somewhat--with Do While .Execute you don't need the Selection.Find.Execute within the loop. The Select Case block is an efficient way to read/write If...Elseif...ElseIf...End If statements. I find it a little easier to read (so it's a personal preference in this case). To be honest, I cannot figure out why your code loops infinitely. My test document didn't have the problem with your routine.
Sub TestScoring() Dim newcount As Single Dim iMultHighlight As Single With Selection 'go to top of doc .HomeKey Unit:=wdStory With .Find 'clear previous find .ClearFormatting 'set search for highlight on .Highlight = True 'run search while highlights are still found Do While .Execute Select Case Selection.Range.HighlightColorIndex Case wdRed newcount = newcount + 1 Case wdTurquoise newcount = newcount + 0.5 Case 9999999 'when a selection has more than one highlight color iMultHighlight = iMultHighlight + 1 Case Else 'do nothing End Select Loop 'move back to top of doc when done End With End With MsgBox (newcount) & vbTab & iMultHighlight
End Sub
HTH, Dave
> Hi All, > [quoted text clipped - 43 lines] > > Karen Tony Jollans - 21 Jan 2006 01:39 GMT The setting of Selection.Find.Wrap controls what happens at the end of your document. As you are not explicitly setting this it will have whatever value it had on the previous Find which may be wdFindContinue. Try explicitly setting it to wdFindStop.
-- Enjoy, Tony
> Hi All, > [quoted text clipped - 43 lines] > > Karen Greg Maxey - 21 Jan 2006 02:32 GMT Tony,
Karen sent me her document and I tied that. No joy
Her document consisted of several yellow highlighted areas, a few red and a few turqoise. I did some testing with msgboxes and founded that it kept looping to a zero length range with yellow hightlight applied afer processing all the other expected found ranges. Her document contained alot of embeded equations that appeared as empty yellow highlighted areas until you put your mouse over one of them. I don't know but these might somehow account for the empty range. Anyway I did a work around that she was satisfied with:
Sub TestScoring2() Dim i As Single Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find .ClearFormatting .Replacement.ClearFormatting .Highlight = True Do While .Execute Select Case oRng.HighlightColorIndex Case wdRed i = i + 1 Case wdTurquoise i = i + 0.5 Case wdYellow If Len(oRng.Text) = 0 Then Exit Do Case Else 'Do Nothign End Select oRng.Collapse direction:=wdCollapseEnd Loop End With MsgBox i End Sub
I will send you her document if you want to take a crack at determining the source of the empty range.
Greg Maxey
 Signature Greg Maxey/Word MVP See: http://gregmaxey.mvps.org/word_tips.htm For some helpful tips using Word.
> The setting of Selection.Find.Wrap controls what happens at the end > of your document. As you are not explicitly setting this it will have [quoted text clipped - 49 lines] >> >> Karen Tony Jollans - 21 Jan 2006 16:28 GMT Sounds odd - yes, I would be interested in having a quick look. Thanks Greg.
-- Enjoy, Tony
> Tony, > [quoted text clipped - 100 lines] > >> > >> Karen Karen - 21 Jan 2006 17:52 GMT Hi Tony,
I had, in one of the iterations with the macro, worked with the Selection.Wrap. I tried wdFindStop and that had no effect, I tried wdFindAsk as a troubleshooting aid but the macro would lock up before the message dialog could be activated. Greg is right, he certainly found what was causing my macro to just cycle and cycle. The document does have several yellow-highlighted text and Mathtype equation objects; those are the answers. The red and turquoise highlighted items are either full value off or partial value. I too wouldn't mind finding that zero-length, yellow-highlighted item that makes the macro not work BUT Greg's code allows this to work and I'm very pleased with it :)
 Signature Karen
The setting of Selection.Find.Wrap controls what happens at the end of your document. As you are not explicitly setting this it will have whatever value it had on the previous Find which may be wdFindContinue. Try explicitly setting it to wdFindStop.
Tony Jollans - 22 Jan 2006 10:16 GMT Thank you Karen,
I can now reproduce this problem and am playing with it. I will post back if I find anything of consequence.
Greg - thank you, but there is no need to send me the document now.
-- Enjoy, Tony
> Hi Tony, > [quoted text clipped - 16 lines] > it had on the previous Find which may be wdFindContinue. Try explicitly > setting it to wdFindStop. Greg Maxey - 22 Jan 2006 16:08 GMT Tony,
Provided I got the e-mail address right, I already have.
 Signature Greg Maxey/Word MVP
-- Greg Maxey/Word MVP See: http://gregmaxey.mvps.org/word_tips.htm For some helpful tips using Word.
> Thank you Karen, > [quoted text clipped - 33 lines] >> it had on the previous Find which may be wdFindContinue. Try explicitly >> setting it to wdFindStop. Tony Jollans - 22 Jan 2006 18:41 GMT Lost in cyberspace, I think :)
-- Enjoy, Tony
> Tony, > [quoted text clipped - 46 lines] > >> it had on the previous Find which may be wdFindContinue. Try explicitly > >> setting it to wdFindStop. Tony Jollans - 22 Jan 2006 17:30 GMT I was looking for something complex but it seems to me that Find in VBA has problems when looking for formatting and always seems to end up in a loop on the last match. The check for text length of zero happens to solve it in this particular instance (I think because the MathType objects are Fields) but doesn't help in the more general case which, I suspect, may require a check for the found range being equal to the previous found range.
-- Enjoy, Tony
> Thank you Karen, > [quoted text clipped - 32 lines] > > it had on the previous Find which may be wdFindContinue. Try explicitly > > setting it to wdFindStop. Karen - 23 Jan 2006 00:41 GMT Tony,
You're right, turns out Greg's fix only worked with one type of document so..... now I've added a teal-highlighted field at the end of the document and one of the Case statements says that if the highlight is 'teal' then 'Exit Do'. It's a workaround but it works :)
Karen
I was looking for something complex but it seems to me that Find in VBA has problems when looking for formatting and always seems to end up in a loop on the last match. The check for text length of zero happens to solve it in this particular instance (I think because the MathType objects are Fields) but doesn't help in the more general case which, I suspect, may require a check for the found range being equal to the previous found range.
-- Enjoy, Tony
"Tony Jollans" <My Forename at My Surname dot com> wrote in message news:eCkFJ0zHGHA.1728@TK2MSFTNGP09.phx.gbl...
> Thank you Karen, > > I can now reproduce this problem and am playing with it. I will post back if
> I find anything of consequence. > [quoted text clipped - 9 lines] > > Selection.Wrap. I tried wdFindStop and that had no effect, I tried > > wdFindAsk as a troubleshooting aid but the macro would lock up before the
> > message dialog could be activated. Greg is right, he certainly found what
> > was causing my macro to just cycle and cycle. The document does have > > several yellow-highlighted text and Mathtype equation objects; those are [quoted text clipped - 15 lines] > > it had on the previous Find which may be wdFindContinue. Try explicitly > > setting it to wdFindStop. Greg Maxey - 23 Jan 2006 00:55 GMT While I have tried and tried, I can't duplicate the continous loop in a simple document with or without the zero range check.
If I type a few lines of text and hightlight some text red and some text yellow it processes as expected with no continous loop.
I'm perplexed.
 Signature Greg Maxey/Word MVP See: http://gregmaxey.mvps.org/word_tips.htm For some helpful tips using Word.
> I was looking for something complex but it seems to me that Find in > VBA has problems when looking for formatting and always seems to end [quoted text clipped - 36 lines] >>> have whatever value it had on the previous Find which may be >>> wdFindContinue. Try explicitly setting it to wdFindStop. Tony Jollans - 23 Jan 2006 10:34 GMT Looks like I made a silly mistake in my code - not for the first time - which rather explains the startling result. I will try again and report back.
-- Enjoy, Tony
> While I have tried and tried, I can't duplicate the continous loop in a > simple document with or without the zero range check. [quoted text clipped - 50 lines] > >>> have whatever value it had on the previous Find which may be > >>> wdFindContinue. Try explicitly setting it to wdFindStop. Tony Jollans - 25 Jan 2006 14:21 GMT I cannot now recreate the problem at all - to be honest I can't be sure I ever did.
If you still have the document, Greg, I would be interested to see it. I never did get your e-mail - Tony at Jollans dot com.
-- Enjoy, Tony
> Looks like I made a silly mistake in my code - not for the first time - > which rather explains the startling result. I will try again and report [quoted text clipped - 58 lines] > > >>> have whatever value it had on the previous Find which may be > > >>> wdFindContinue. Try explicitly setting it to wdFindStop. Karen - 24 Jan 2006 04:43 GMT Hi Greg,
When I first worked on the macro I used a simple doc and it worked. The problem is that these docs are from other users so there is no control over whatever 'odd' things might have happened. To go back to my initial post, I thought if I could find some clear end of document marker then I could use that, but I never could find one so........... now I create my own with the teal-highlighted text and everything is working fine.
I really appreciate the time both you and Tony took to review the code, offer suggestions and, in your case, actually troubleshot a 'real' doc with the error.
Now, if I were king of Microsoft, I'd develop some sort of VBA end of document marker :)
 Signature Karen
While I have tried and tried, I can't duplicate the continous loop in a simple document with or without the zero range check.
If I type a few lines of text and hightlight some text red and some text yellow it processes as expected with no continous loop.
I'm perplexed.
 Signature Greg Maxey/Word MVP See: http://gregmaxey.mvps.org/word_tips.htm For some helpful tips using Word.
Tony Jollans wrote:
> I was looking for something complex but it seems to me that Find in > VBA has problems when looking for formatting and always seems to end [quoted text clipped - 38 lines] >>> have whatever value it had on the previous Find which may be >>> wdFindContinue. Try explicitly setting it to wdFindStop. Tony Jollans - 26 Jan 2006 20:00 GMT I have now had a chance to look at the real document (thanks, Greg) and the problem is caused by formatting being applied to table cells - complete cells rather than just the (textual) content.
I can't say for sure what happens but end-of-cell markers have an uneasy relationship with F&R - you cannot search for them, nor will they be included in the Found Range even if they satisfy the Find criteria.
When a (complete) table cell is formatted (highlighted in this case, but other formatting seems to have the same effect) and that formatting is Found, the Found Range is set to the text in the cell, not the complete cell. When Find then looks forward to find the next instance it doesn't seem unreasonable to assume that it might find the end-of-cell marker and, on setting the found Range to it, automatically select the whole cell (which is what happens when you select the end-of-cell normally) but Find seems to override the cell selection and limit the selection to the (non-existent) space between the last character and the end-of-cell marker. And so it loops, except that ...
... it only seems to happen in VBA (not via the UI), and ... ... it only seems to happen when there are no more instances in the document to find.
There doesn't seem to be any easy way to distinguish formatting applied to cell contents from formatting applied to the cell - although I could have missed something.
Greg's solution of checking for a zero length happened to work but there are other situations where that can occur so it is not a general purpose workaround. All I can think of immediately is checking the range and comparing it to the previous found range. Maybe someone else has an idea.
I hope all that made some kind of sense.
-- Enjoy, Tony
> Hi Greg, > [quoted text clipped - 71 lines] > >>> have whatever value it had on the previous Find which may be > >>> wdFindContinue. Try explicitly setting it to wdFindStop. Karen - 27 Jan 2006 15:22 GMT Hi Tony,
Thanks so much. There is no way I would have found the fundamental problem. So, I'm now inserting my own highlighted end of document marker and it's working fine on several different yet related docs. It's really nice to now know the root of the problem and why it varied from document to document. Now, if I could troubleshoot like that...............:)
 Signature Karen
I have now had a chance to look at the real document (thanks, Greg) and the problem is caused by formatting being applied to table cells - complete cells rather than just the (textual) content.
I can't say for sure what happens but end-of-cell markers have an uneasy relationship with F&R - you cannot search for them, nor will they be included in the Found Range even if they satisfy the Find criteria.
When a (complete) table cell is formatted (highlighted in this case, but other formatting seems to have the same effect) and that formatting is Found, the Found Range is set to the text in the cell, not the complete cell. When Find then looks forward to find the next instance it doesn't seem unreasonable to assume that it might find the end-of-cell marker and, on setting the found Range to it, automatically select the whole cell (which is what happens when you select the end-of-cell normally) but Find seems to override the cell selection and limit the selection to the (non-existent) space between the last character and the end-of-cell marker. And so it loops, except that ...
... it only seems to happen in VBA (not via the UI), and ... ... it only seems to happen when there are no more instances in the document to find.
There doesn't seem to be any easy way to distinguish formatting applied to cell contents from formatting applied to the cell - although I could have missed something.
Greg's solution of checking for a zero length happened to work but there are other situations where that can occur so it is not a general purpose workaround. All I can think of immediately is checking the range and comparing it to the previous found range. Maybe someone else has an idea.
I hope all that made some kind of sense.
-- Enjoy, Tony
> Hi Greg, > > When I first worked on the macro I used a simple doc and it worked. The > problem is that these docs are from other users so there is no control over
> whatever 'odd' things might have happened. To go back to my initial post,I > thought if I could find some clear end of document marker then I could use > that, but I never could find one so........... now I create my own with the
> teal-highlighted text and everything is working fine. > > I really appreciate the time both you and Tony took to review the code, > offer suggestions and, in your case, actually troubleshot a 'real' doc with
> the error. > [quoted text clipped - 60 lines] > >>> have whatever value it had on the previous Find which may be > >>> wdFindContinue. Try explicitly setting it to wdFindStop. Greg - 27 Jan 2006 15:25 GMT Tony,
Yes it makes some kind of sense. Trouble is, I can't seem to be able to apply "highlighting" to a complete cell in a simple document so I can step through it and view your observations.
Tony Jollans - 27 Jan 2006 16:21 GMT So what happens when you select a (complete - non-empty) cell and click on the highlight button?
-- Enjoy, Tony
> Tony, > > Yes it makes some kind of sense. Trouble is, I can't seem to be able > to apply "highlighting" to a complete cell in a simple document so I > can step through it and view your observations. Greg - 27 Jan 2006 16:53 GMT Tony,
Just the text is highlighted and the macro runs as expected with no continous loop.
Tony Jollans - 27 Jan 2006 17:30 GMT Just to be sure I tried again.
New Document New Table (default 2 rows 5 columns but it makes no difference for me) Random text in a single cell (the first but any one does it for me) Select the cell Click on Highlight - the text shows highlighted. Run this:
Sub Macro6() ' ' Macro6 Macro ' Macro recorded 27/01/2006 by Tony ' Selection.Find.ClearFormatting Selection.Find.Highlight = True With Selection.Find .Text = "" .Replacement.Text = "" .Forward = True .Wrap = wdFindContinue .Format = True .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With While Selection.Find.Execute Wend End Sub
It loops. Break out of the loop Undo the highlight Select the text but NOT the cell Click on Highlight Run the code again It finishes cleanly
I am running Word 2003 (on Win XP Pro SP1) but without updates.
Interestingly, I just quickly tried it on another machine (Word 2000 on Win Me) and it didn't loop so some more investigation is called for :)
-- Enjoy, Tony
> Tony, > > Just the text is highlighted and the macro runs as expected with no > continous loop. Greg - 27 Jan 2006 17:44 GMT Tony,
Ran your test here. No loop (Word 2000). Will try tonight at home (Word2003) and suspect that I will see the loop.
Greg - 27 Jan 2006 17:01 GMT Only the text in the cell is highlighted and the macro runs as expected with no continous loop.
|
|
|