Log in

View Full Version : Help with wicked VBA macro (Word)



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

bcn
05-16-2013, 02:20 AM
Sorry?

macropod
05-19-2013, 11:40 PM
Your macro does nothing at all when I run it ...

bcn
06-04-2013, 02:34 AM
Hi Paul,

Maybe it does not do anything because you don't have the proper keywords and styles in your Word document? Or am I missing something?

Thanks,

Daniel

macropod
06-04-2013, 02:57 AM
Not likely. If a macro calls a Style not present in the document, an error will be thrown. The macro won't just skip over it. Accordingly, for testing I had to reference a Style I actually had.

With or without that Style, no 'craziness' was encountered, regardless of whether any of the "menu", "box", "tab", "dialog", "option", "check" strings were present. The macro runs to the end of the document, prompting as appropriate if something other than those strings are present in a bold font, then exits. Are you sure you haven't formatted the last paragraph break in the document with your "GUI element" Style?

bcn
06-04-2013, 05:19 AM
Hi Paul,

Sorry... I misunderstood your previous message:


Your macro does nothing at all when I run it ...

I thought you meant the macro did not run. Now I understand you mean the macro does not do anything wrong. (By the way... the macro is a kind courtesy of Greg Maxey.)

I checked the document and the last para breaks are Normal.

Daniel

macropod
06-04-2013, 05:33 AM
I checked the document and the last para breaks are Normal.
That's as may be, but does it have your "GUI element" Style (which I assume to be a Character Style, else your macro trips up when it shouldn't) applied?