• Macro to delete certain wording within curly brackets (braces)

    Home » Forums » AskWoody support » Productivity software by function » MS Word and word processing help » Macro to delete certain wording within curly brackets (braces)

    Author
    Topic
    #508357

    Hi, I have quite a lot of documents to amend and house style and one of the tasks is deleting all wording within {Note } and I wondered whether a macro would do the job. I have attached an example document. Any help would be very much appreciated. Thanks Shelley

    Viewing 7 reply threads
    Author
    Replies
    • #1593037

      You don’t need a macro. Replace will do it with Wild Cards. I expect someone will give you what you need to put in. I do not know.

    • #1593045

      I’ve just done a test on your Word Doc. On the Replace tab of find, click on more , then click on Use wildcards. The strings I entered are on the attachment. What you replace it with is up to you (you say words within the curly braces). Leave the replace field blank to replace the string with, well nothing.

      46912-word

      The back slashes are required because { and } are special characters

      Eliminate spare time: start programming PowerShell

    • #1593065

      Hi, I’ve tried what you have suggested and it hasn’t worked for me. Where it has {Note all the wording contained within the green back ground before the next } needs to be deleted which is why I thought a macro might be best placed for this type of deletion. Any ideas? Shelley

    • #1593072

      OK, next iteration. You need to use {Note*} to find the relevant text. The capital of Note is required. Something to be aware of is that when it finds the string, it will put the final } at the top of the string (I’m assuming that the first time through you’ll do a find first before the replace to check it’s picking up the right text). Once you’re happy it’s doing what you want then a ‘replace all’ will be in order. When I did a replace all it removed 7 blocks of text – I assume that’s how many were in it.
      Good luck!

      Eliminate spare time: start programming PowerShell

    • #1593084

      You can’t specify the background shading as a Find/Replace parameter, besides which, the content you want to delete doesn’t all have that shading (e.g. {Note … }).

      The basic Find/Replace macro code you’d incorporate into your larger process would be something like:

      Code:
        With .Find
          .ClearFormatting
          .Replacement.ClearFormatting
          .Text = "{Note*}"
          .Replacement.Text = ""
          .Forward = True
          .Wrap = wdFindContinue
          .Format = False
          .MatchWildcards = True
          .Execute Replace:=wdReplaceAll
        End With

      If the shading is an issue, another approach will be required.

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

      • #1593093

        You can’t specify the background shading as a Find/Replace parameter, besides which, the content you want to delete doesn’t all have that shading (e.g. {Note … }).

        The basic Find/Replace macro code you’d incorporate into your larger process would be something like:

        Code:
          With .Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Text = "{Note*}"
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchWildcards = True
            .Execute Replace:=wdReplaceAll
          End With

        If the shading is an issue, another approach will be required.

        Hi, many thanks for your responses to my query on this very much appreciated. I have managed to do a find and replace. There are a few places where the notes have not deleted and I can’t understand why. I’ve also added into the macro to delete the extra paragraphs. I attach an original document before I’ve run the macro and an updated document where I have run the macro. Thanks. Shelley

        Code:
        Sub DPU_deletenotesdwf()
        ‘
        ‘ DPU_deletenotesdwf Macro
        ‘
        ‘
            Selection.Find.ClearFormatting
            Selection.Find.Replacement.ClearFormatting
            With Selection.Find
                .Text = “{Note*}”
                .Replacement.Text = “”
                .Forward = True
                .Wrap = wdFindContinue
                .Format = False
                .MatchWildcards = True
            End With
            Selection.Find.Execute Replace:=wdReplaceAll
            Selection.WholeStory
            Selection.ParagraphFormat.Alignment = wdAlignParagraphJustify
            Selection.Find.ClearFormatting
            Selection.Find.Replacement.ClearFormatting
            With Selection.Find
                .Text = “[^13]{2,}”
          .Replacement.Text = “^p”
                .Forward = True
                .Wrap = wdFindContinue
                .Format = False
               .Forward = True
          .MatchWildcards = True
          .Wrap = wdFindContinue
            End With
            Selection.Find.Execute Replace:=wdReplaceAll
        End Sub
    • #1593094

      Shelley, I found one where it didn’t delete completely (under Charge) and that didn’t because there’s an embedded {} in the note – and neither my, or the macro’s methods can cope with that as they would terminate the find after 1s}

      {Note
      Charge{chargeNum IsMoreThan 1s}
      List here those financial charges that are intended to be discharged on or before completion, so that the Property will be sold free of them. The definition is used primarily in relation to clause 8.
      }

      Doing it my way you would have to check for those sorts of constructs. Perhaps Paul would be able to add such a thing to his macro (that’s beyond my pay-scale!)

      Eliminate spare time: start programming PowerShell

      • #1593098

        I know what you mean about pay scale, I’m just a secretary trying to fathom this out with no support from IT so I really do appreciate all the help I get from here its made a massive difference to how I can get the mountain of work done and I can’t thank everyone enough really. Thanks for your help. Shelley

    • #1593263

      There is no practical way of extending the range to accommodate nested braces other than testing the found range, checking for any opening braces after ‘Note’ then extending by however many times as are necessary until one ends up with a matching count of opening and closing braces. That would require quite a different approach than what till now was a fairly simple Find/Replace.

      PS: Shelley, I’d have thought by now – especially after all the code you’ve been given (or at least helped with) – you’d have learnt to do better than just use the macro recorder. All that ‘Selection’ stuff makes my eyes water.

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

      • #1593341

        Hi Paul, I couldn’t get your macro to work so had to just do a quick fix for the day to get the work done but will revise my macro try yours again and tidy it all up as you are quite right it is very messy. Thanks for your help. Shelley

    • #1593350

      Try:

      Code:
      Sub Demo()
      Application.ScreenUpdating = False
      With ActiveDocument.Range
        With .Find
          .ClearFormatting
          .Replacement.ClearFormatting
          .Text = "{Note*}"
          .Replacement.Text = ""
          .Forward = True
          .Wrap = wdFindStop
          .Format = False
          .MatchWildcards = True
          .Execute
        End With
        Do While .Find.Found
          Do While Len(Replace(.Text, "{", ""))  Len(Replace(.Text, "}", ""))
            .MoveEndUntil "}", wdForward: .End = .End + 1
          Loop
          .Delete
          .Collapse wdCollapseEnd
          .Find.Execute
        Loop
      End With
      Application.ScreenUpdating = True
      End Sub

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

    Viewing 7 reply threads
    Reply To: Macro to delete certain wording within curly brackets (braces)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: