Consulting

Results 1 to 3 of 3

Thread: Formatting brackets with if condition

  1. #1

    Formatting brackets with if condition

    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!

  2. #2
    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
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    Perfect - thank you very much!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •