PDA

View Full Version : Loop exits too soon (sometimes ...)



kobber
07-01-2014, 08:27 AM
Hi everyone,

First post for me in the forum, although I've been through here many times. Thanks for everything so far.

Here my problem. I'm writing a macro to format Word documents. Nothing too complicated (font and paragraph formatting, search and replace, etc.). The main macro 'Formatting' calls up five separate macros one after the other.

One is made to search for hidden text (DecFormat05_HiddenText). If found, it makes it visible, applies font colour red and then moves on the the next (Do While Loop). It also counts the number of occurrences and posts a message warning the user that these changes were made.

This macro works fine when I run it on its own, but when I run it through the main macro it finds the first instance and then exits the loop (with the message). No idea what it could be. I would be grateful for any pointers.

Just for info, most of my macros are intially recorded then improved through online research if at all possible. I really should get some formal training to take it to the next level ...

Thanks in advance!

Kobber

Here is the code:

1. the main macro which calls the others


Sub Formatting()
Call TCs_TurnOFF ' Turns off Track Changes
Call DecFormat01_SearchReplace ' Does some search and replace operations
Call DecFormat02_HardSpacesDashes ' Inserts hard spaces and dashes as necessary
Call DecFormat03_Page ' Page formatting
Call DecFormat04_FontAndParagraph ' Font and paragraph formatting
Call DecFormat05_HiddenText ' Reveals hidden text
Selection.HomeKey Unit:=wdStory
MsgBox ("Formatting complete.")
End Sub

2. DecFormat05_HiddenText


Sub DecFormat05_HiddenText()
Dim i As Integer
i = 0
ActiveWindow.ActivePane.View.ShowAll = True
Selection.Find.ClearFormatting
Selection.Find.Font.Hidden = True
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Font.Hidden = False
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceOne 'All
Selection.Font.Color = wdColorRed
If Selection.Find.Found = True Then
Do While Selection.Find.Found
Selection.Find.Execute Replace:=wdReplaceOne 'All
Selection.Font.Color = wdColorRed
Selection.Collapse Direction:=wdCollapseEnd
i = i + 1
Loop
MsgBox ("Hidden text was detected " & i & " time(s) in this document. " & _
"It has been made visible and formatted to appear red. " & vbCr & vbCr & _
"Please check this text as soon as this macro has finished running.")
End If
End Sub

macropod
07-01-2014, 04:52 PM
Try:

Sub DecFormat05_HiddenText()
Dim i As Long
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Format = True
.Font.Hidden = True
.Forward = True
.Wrap = wdFindStop
.Execute
End With
Do While .Find.Found
.Font.Hidden = False
.Font.Color = wdColorRed
.Collapse Direction:=wdCollapseEnd
.Find.Execute
i = i + 1
Loop
End With
MsgBox ("Hidden text was detected " & i & " time(s) in this document. " & _
"It has been made visible and formatted to appear red. " & vbCr & vbCr & _
"Please check this text as soon as this macro has finished running.")
End Sub

kobber
07-01-2014, 11:27 PM
Hi macropod,

Thanks very much for your help, it works perfectly. I just added an if-condition around the message box at the end so that it only appears if i > 0 and doesn't bother the user telling them there were 0 occurrences in the text.

Have a great day.

Kobber