PDA

View Full Version : Formatting brackets with if condition



cincinatikid
02-22-2020, 04:46 AM
Hello there

I need to have a makro for word-documents with the following function:

(If every word in brackets is italic, set brackets italic) -> (If every word in brackets is italic, set brackets italic)

So that wouldn't be that difficult, but there is the following exeption:

(If there is at least one word in normal font, brackets should stay in normal font). So a solution, where brackets are set in italic, when the first/last character is in italic doesn't work resp. isn't the solution I need.

I did quite a lot of research and didn't find any solution for this - and I'm too unfamiliar with VBA to adapt or combine existing codes to get it.

Does anyone have a solution for this?

Any help is greatly appreciated!

gmayor
02-22-2020, 07:53 AM
The following should do it


Sub Macro1()
Dim oRng As Range

Set oRng = ActiveDocument.Range
With oRng.Find
Do While .Execute(findText:="\(*\)", MatchWildcards:=True)
oRng.Start = oRng.Start + 1
oRng.End = oRng.End - 1
If oRng.Font.Italic = True Then
'MsgBox oRng.Text & " - True"
oRng.Start = oRng.Start - 1
oRng.End = oRng.End + 1
oRng.Characters.First.Font.Italic = True
oRng.Characters.Last.Font.Italic = True
Else
'MsgBox oRng.Text & " - False"
oRng.Start = oRng.Start - 1
oRng.End = oRng.End + 1
oRng.Characters.First.Font.Italic = False
oRng.Characters.Last.Font.Italic = False
End If
oRng.Collapse 0
Loop
End With
Set oRng = Nothing
End Sub

cincinatikid
02-22-2020, 08:55 AM
Perfect - thank you very much!