Consulting

Results 1 to 7 of 7

Thread: Apply braces around strikethrough string of text

  1. #1

    Apply braces around strikethrough string of text

    Hi All: Trying to find a macro for Word to apply brackets around underline text and remove underline. Also need to apply braces around string of strikethrough text.

    Text like this should look [like this].
    Text like this (strikethrough) should look {like this}. (keeping strikethrough)

    This code works for the underlined text, but still need to address the strikethrough text.

    Sub Tag_Under_Line()
    Selection.ClearFormatting
     Selection.HomeKey wdStory, wdMove
    Selection.Find.Font.Underline = wdUnderlineSingle
    Selection.Find.Execute ""
    Do Until Selection.Find.Found = False
        Selection.Font.Underline = wdUnderlineNone
        Selection.InsertBefore "["
        Selection.InsertAfter "]"
        Selection.MoveRight
        Selection.Find.Execute ""
    Loop
    End Sub
    Appreciate any help!
    Last edited by Aussiebear; 06-07-2023 at 12:57 PM. Reason: Added code tags

  2. #2
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,057
    Location
    welcome to VBAX Docspecial. Please be patient, for one of our resident Word gurus will along shortly.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  3. #3
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,057
    Location
    You could try this code....

    Sub Tag_Strikethroughs()
      Dim aRng As Range
      Set aRng = ActiveDocument.Range
      With aRng.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Font.StrikeThrough = True
        .Text = ""
        Do While .Execute = True
          aRng.InsertBefore "{"
          aRng.InsertAfter "}"
          aRng.Characters.Last.Font.StrikeThrough = False
          aRng.Start = aRng.End
          aRng.End = ActiveDocument.Range.End
        Loop
      End With
    End Sub
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    Option Explicit
    
    Sub ScratchMacro()
    'A basic Word Macro coded by Gregory K. Maxey
    Dim oRng As Range
      Set oRng = ActiveDocument.Range
      With oRng.Find
        .Font.StrikeThrough = True
        While .Execute
          oRng.InsertBefore "{"
          oRng.InsertAfter "}"
          oRng.Collapse wdCollapseEnd
          If oRng.End = ActiveDocument.Range.End Then Exit Do
        Wend
      End With
      Set oRng = ActiveDocument.Range
      With oRng.Find
        .Font.Underline = wdUnderlineWords
        Do While .Execute
          oRng.InsertBefore "["
          oRng.Collapse wdCollapseEnd
          oRng.Select
          oRng.Underline = wdUnderlineNone
          oRng.InsertAfter "]"
          If oRng.End = ActiveDocument.Range.End Then Exit Do
        Loop
      End With
      Set oRng = ActiveDocument.Range
      With oRng.Find
        .Font.Underline = wdUnderlineWords
        Do While .Execute
          oRng.Underline = wdUnderlineNone
          oRng.Collapse wdCollapseEnd
          If oRng.End = ActiveDocument.Range.End Then Exit Do
        Loop
      End With
    lbl_Exit:
      Exit Sub
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  5. #5
    This works and will save us lots of time!

    One addition is that the underlined text should be bolded and bracketed [ ] without underlining. I know this should be easy to add to the code, however, I can’t figure it out.

    Would you please take a look?

    Thank you!

  6. #6
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,057
    Location
    Any chance you posted this elsewhere docspecial?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  7. #7
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    Sub ScratchMacro()
    'A basic Word Macro coded by Gregory K. Maxey
    Dim oRng As Range
      Set oRng = ActiveDocument.Range
      With oRng.Find
        .Font.StrikeThrough = True
        Do While .Execute
          oRng.InsertBefore "{"
          oRng.InsertAfter "}"
          oRng.Collapse wdCollapseEnd
          If oRng.End = ActiveDocument.Range.End Then Exit Do
        Loop
      End With
      Set oRng = ActiveDocument.Range
      With oRng.Find
        .Font.Underline = wdUnderlineWords
        Do While .Execute
          oRng.InsertBefore "["
          oRng.Collapse wdCollapseEnd
          oRng.InsertAfter "]"
          If oRng.End = ActiveDocument.Range.End Then Exit Do
        Loop
      End With
      Set oRng = ActiveDocument.Range
      With oRng.Find
        .Font.Underline = wdUnderlineWords
        Do While .Execute
          oRng.Underline = wdUnderlineNone
          oRng.End = oRng.End - 1
          oRng.Font.Bold = True
          oRng.Collapse wdCollapseEnd
          If oRng.End + 1 = ActiveDocument.Range.End Then Exit Do
        Loop
      End With
    lbl_Exit:
      Exit Sub
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

Posting Permissions

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