PDA

View Full Version : Solved: How to shorten and loop this macro



bcn
12-07-2012, 01:41 AM
Hi,

I'm having a hard time trying to write this macro. I think I'm taking the long way and I still cannot figure out how to loop it. Any help will be highly appreciated!

What I want to do

I have a huge document full of button names that are in small caps and enclosed in » «. They look like this:

Click »OK«. (Where "OK" is in small caps.)
To import the data, click »Import« (Where "Import" is in small caps.)

Since some of the button names may not have the small caps format applied to them, I want the macro to look for all strings enclosed in » and « and:

1. Change the » to CharacterNumber:=-3971, Font:="Wingdings 3", Unicode:=True and the « to CharacterNumber:=-3972, Font:="Wingdings 3", Unicode:=True

2. Apply a style called "Button" to the string and the two special characters.

3. Loop it (make the macro find all the cases and process them throughout the document).

What I have so far is:

Sub LSreplaceOldButtonStyle()
Dim oRng As Word.Range
Dim text As String

Selection.Find.ClearFormatting
With Selection.Find
.text = "»"
.Replacement.text = ""
.Forward = True
.Wrap = wdFindContinue
End With
Selection.Find.Execute
Selection.InsertSymbol CharacterNumber:=-3971, Font:="Wingdings 3", Unicode:=True

Selection.Find.ClearFormatting
With Selection.Find
.text = "«"
.Replacement.text = ""
.Forward = True
.Wrap = wdFindAsk
End With
Selection.Find.Execute
Selection.InsertSymbol CharacterNumber:=-3972, Font:="Wingdings 3", Unicode:=True

Selection.Extend
Selection.Find.ClearFormatting
With Selection.Find
.text = ChrW(-3971)
.Replacement.text = ""
.Forward = False
.Wrap = wdFindContinue
End With
Selection.Find.Execute
Selection.Style = ActiveDocument.Styles("Button")




Thanks a lot!

Daniel

bcn
12-07-2012, 05:32 AM
Here I added a looping chunk of code that I stole from Maxey. But it only loops two times. What am I doing wrong?

Sub LSreplaceOldButtonStyle()
Dim oRng As Word.Range
Dim text As String
Dim bFirstLoop As Boolean
bFirstLoop = True
RepeatLoop:
Selection.Find.ClearFormatting
With Selection.Find
.text = "»"
.Replacement.text = ""
.Forward = True
.Wrap = wdFindContinue
End With
Selection.Find.Execute
Selection.InsertSymbol CharacterNumber:=-3971, Font:="Wingdings 3", Unicode:=True

Selection.Find.ClearFormatting
With Selection.Find
.text = "«"
.Replacement.text = ""
.Forward = True
.Wrap = wdFindAsk
End With
Selection.Find.Execute
Selection.InsertSymbol CharacterNumber:=-3972, Font:="Wingdings 3", Unicode:=True

Selection.Extend
Selection.Find.ClearFormatting
With Selection.Find
.text = ChrW(-3971)
.Replacement.text = ""
.Forward = False
.Wrap = wdFindContinue
End With
Selection.Find.Execute
Selection.Style = ActiveDocument.Styles("Button")
If bFirstLoop Then
bFirstLoop = False
GoTo RepeatLoop
End If
lbl_Exit:
Selection.Collapse wdCollapseStart
Exit Sub

End Sub


And... isn't it still too long?

Thanks,

Daniel

gmaxey
12-07-2012, 06:15 AM
Daniel,
It seems we have seen this sort of thing before. I've tried to show you how to use ranges (rather than selection) but you seem to insist on reverting back to using selection. BTW way, it is Greg, or Mr. Maxey or CDR Maxey or the portly genteman that posts as gmaxey, but please not Maxey.

Sub LSreplaceOldButtonStyle()
Dim oRng As Word.Range
Dim text As String
Set oRng = ActiveDocument.Range
With oRng.Find
.ClearFormatting
.Replacement.ClearFormatting
.text = "«(*)»"
.Replacement.text = ChrW(-3971) & "\1" & ChrW(-3972)
.Replacement.Style = "Button"
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
While .Execute(Replace:=wdReplaceOne)
oRng.Characters(1).Font.Name = "Wingdings 3"
oRng.Characters.Last.Font.Name = "Wingdings 3"
oRng.Collapse wdCollapseEnd
Wend
End With
End Sub

bcn
12-07-2012, 06:19 AM
Oh, feekl like if I would be talking to myself, to an imaginary character or something.

Ok, I think I found the solution. I don't know if it can be shortened a lot more, but at least it works:

Sub LSreplaceOldButtonStyle()
Dim oRng As Word.Range
Dim text As String
Dim result As Boolean

Do
Selection.Find.ClearFormatting
With Selection.Find
.text = "»"
.Replacement.text = ""
.Forward = True
.Wrap = wdFindContinue
End With
result = Selection.Find.Execute
If result = False Then
Selection.Collapse wdCollapseStart
End
End If

Selection.InsertSymbol CharacterNumber:=-3971, Font:="Wingdings 3", Unicode:=True

Selection.Find.ClearFormatting
With Selection.Find
.text = "«"
.Replacement.text = ""
.Forward = True
.Wrap = wdFindAsk
End With
Selection.Find.Execute
Selection.InsertSymbol CharacterNumber:=-3972, Font:="Wingdings 3", Unicode:=True

Selection.Extend
Selection.Find.ClearFormatting
With Selection.Find
.text = ChrW(-3971)
.Replacement.text = ""
.Forward = False
.Wrap = wdFindContinue
End With
Selection.Find.Execute
Selection.Style = ActiveDocument.Styles("Button")
Loop

End Sub

bcn
12-07-2012, 06:22 AM
Oh, Greg. I did not see your answer before posting this third comment above.

Sorry for the use of Maxey instead of Greg or Mr. Maxey. That is the problem with being a Spaniard. :)

I revert to whatever comes to mind or whatever I find in the Internet. Not that I am so stubborn as it seems. I still don't understand much of the logic behind this code. So please bear with me.

Thank you very much for your help. Once again!

Daniel

gmaxey
12-07-2012, 06:27 AM
Daniel,
Thanks. Unless you want to continue to rely on others to write your code, and one day that will come with the expectation of payment, you should abandon the wild goose chases and try to learn the logic behind the code.