bcn
05-15-2013, 01:42 AM
Hi,
This macro is supposed to look for all occurrences of GUI element labels (such as "menu", "tab", "dialog box", etc.) and apply a specific style (called "GUI element") to the preceding string in bold. It also changes the color of the string to red. Oh, and it also looks for strings that may already have the "GUI element" style applied to them and asks the user if he/she wants to leave them as is.
It does the trick very well. But when the macro reaches the end of the document, it goes crazy (such as in a loop) and after some seconds Word gives up and goes on strike.
Would be greatful if someone could take a look and help me solve it.
Thanks a lot in advance!
Daniel
Sub FixGUIelements()
'
Dim lngIndex As Long
Dim oRng As Word.range
Dim oRngTest As Word.range
Dim strTest As String
Dim bFirstLoop As Boolean
bFirstLoop = True
RepeatLoop:
Set oRng = ActiveDocument.range
With oRng.Find
.ClearFormatting
.format = True
If bFirstLoop Then
.Font.ColorIndex = wdRed
Else
.Font.Bold = True
End If
While .Execute
Set oRngTest = oRng.Duplicate
oRngTest.MoveEndWhile " ", wdForward
oRngTest.Collapse wdCollapseEnd
oRngTest.Expand wdWord
strTest = Trim(oRngTest)
Select Case strTest
Case "menu", "box", "tab", "dialog", "option", "check"
If oRng.Style <> "GUI element" Then
oRng.Select
Select Case MsgBox("Style applied: " & Selection.Style & "." & vbCr + vbCr _
& "Do you want to apply the GUI Element style to this text?", _
vbQuestion + vbYesNoCancel, "Apply Style")
Case vbYes
oRng.Style = "GUI element"
Case vbCancel
GoTo lbl_Exit
Case Else
End Select
End If
Case Else
If oRng.Style = "GUI element" Then
oRng.Select
Select Case MsgBox("Style applied " & Selection.Style & "." & vbCr + vbCr _
& "This text may have the GUI Element style applied in error. Do you want to remove the GUI Element style?", _
vbQuestion + vbYesNoCancel, "Apply Style")
Case vbYes
oRng.Font.Reset
Case vbCancel
GoTo lbl_Exit
Case Else
End Select
End If
End Select
oRng.Collapse wdCollapseEnd '*****Added
Wend
End With
If bFirstLoop Then
bFirstLoop = False
GoTo RepeatLoop
End If
lbl_Exit:
Selection.Collapse wdCollapseStart
Exit Sub
End Sub
This macro is supposed to look for all occurrences of GUI element labels (such as "menu", "tab", "dialog box", etc.) and apply a specific style (called "GUI element") to the preceding string in bold. It also changes the color of the string to red. Oh, and it also looks for strings that may already have the "GUI element" style applied to them and asks the user if he/she wants to leave them as is.
It does the trick very well. But when the macro reaches the end of the document, it goes crazy (such as in a loop) and after some seconds Word gives up and goes on strike.
Would be greatful if someone could take a look and help me solve it.
Thanks a lot in advance!
Daniel
Sub FixGUIelements()
'
Dim lngIndex As Long
Dim oRng As Word.range
Dim oRngTest As Word.range
Dim strTest As String
Dim bFirstLoop As Boolean
bFirstLoop = True
RepeatLoop:
Set oRng = ActiveDocument.range
With oRng.Find
.ClearFormatting
.format = True
If bFirstLoop Then
.Font.ColorIndex = wdRed
Else
.Font.Bold = True
End If
While .Execute
Set oRngTest = oRng.Duplicate
oRngTest.MoveEndWhile " ", wdForward
oRngTest.Collapse wdCollapseEnd
oRngTest.Expand wdWord
strTest = Trim(oRngTest)
Select Case strTest
Case "menu", "box", "tab", "dialog", "option", "check"
If oRng.Style <> "GUI element" Then
oRng.Select
Select Case MsgBox("Style applied: " & Selection.Style & "." & vbCr + vbCr _
& "Do you want to apply the GUI Element style to this text?", _
vbQuestion + vbYesNoCancel, "Apply Style")
Case vbYes
oRng.Style = "GUI element"
Case vbCancel
GoTo lbl_Exit
Case Else
End Select
End If
Case Else
If oRng.Style = "GUI element" Then
oRng.Select
Select Case MsgBox("Style applied " & Selection.Style & "." & vbCr + vbCr _
& "This text may have the GUI Element style applied in error. Do you want to remove the GUI Element style?", _
vbQuestion + vbYesNoCancel, "Apply Style")
Case vbYes
oRng.Font.Reset
Case vbCancel
GoTo lbl_Exit
Case Else
End Select
End If
End Select
oRng.Collapse wdCollapseEnd '*****Added
Wend
End With
If bFirstLoop Then
bFirstLoop = False
GoTo RepeatLoop
End If
lbl_Exit:
Selection.Collapse wdCollapseStart
Exit Sub
End Sub