• Spotting missing brackets in Word?

    Author
    Topic
    #477872

    When using brackets in Word, I often manage to forget to include the closing bracket, especially if it is quite a long sentence.

    Does anyone please know if there is a macro one could run, or some other way, for spotting this problem – perhaps by checking that there are as many opening brackets as there are closing brackets in a document?

    Thanks

    V

    Viewing 3 reply threads
    Author
    Replies
    • #1288832

      It’s irritating that the grammer checker doesn’t seem to pick these up.

      One approach might be to find the opening punctuation — ( or [ or { as the case may be — and then create a range to the next instance of the same character, or to the end of the document, whichever comes first. Then check the range for the expected closing punctuation.

      About 2 minutes into trying to write this, it occurred to me to run a search. Maybe something like this? Checking for Matching Parentheses (Microsoft Word)

      Okay, here’s my take on it:

      Code:
      Sub CheckPairedPuncTEST()
      CheckPairedPunc ActiveDocument.Content, "(", ")"
      End Sub
      
      Sub CheckPairedPunc(rngStory As Word.Range, strStart As String, strEndPattern As String)
      Dim rngTemp As Word.Range, lngChars As Long, regEx As Object, regExMatches As Object
      Set rngTemp = ActiveDocument.Range(rngStory.Start, rngStory.Start)
      Do
          ' Set start of range on next strStart
          lngChars = rngTemp.MoveStartUntil(strStart)
          If lngChars = 0 Then Exit Do
          ' Set end of range to character just before next "("
          rngTemp.Collapse wdCollapseEnd
          rngTemp.MoveEnd wdCharacter, 1
          lngChars = rngTemp.MoveEndUntil(strStart)
          If lngChars = 0 Then    ' Set end of range to end of content
              rngTemp.MoveEnd wdStory
              rngTemp.MoveEnd wdCharacter, -1
          End If
          If (regEx Is Nothing) Then Set regEx = CreateObject("vbscript.regexp")
          regEx.Pattern = strEndPattern
          regEx.Global = True
          Set regExMatches = regEx.Execute(rngTemp.Text)
          If regExMatches.Count  1 Then
              rngTemp.Select
              If MsgBox("Unbalanced punctuation in the selection. Stop to fix?", vbExclamation + vbYesNo) = vbYes Then
                  GoTo cleanup
              End If
          End If
          rngTemp.Collapse wdCollapseEnd
      Loop
      cleanup:
      Set rngTemp = Nothing
      Set regEx = Nothing
      Set regExMatches = Nothing
      End Sub

      This assumes you have the VBScript library on your computer, but you do not need to add a VBA Reference (the code uses late binding).

      Also, the end pattern for ) requires a backslash because ) is a special character in regular expressions. That’s not so user friendly, it would be nicer if the procedure just handled that detail for you, but it’s what I can do at the moment.

    • #1288844

      At its simplest you could use something like:

      Code:
      Sub BalanceCheck()
      Dim Rng As Range
      ' Turn Off Screen Updating
      Application.ScreenUpdating = False
      Set Rng = ActiveDocument.Range
      With Rng
        'Validate content
        If Len(Replace(.Text, "{", vbNullString))  Len(Replace(.Text, "}", vbNullString)) Then
          MsgBox "Unmatched brace '{}' pairs in the document.", vbCritical + vbOKOnly, "Error!"
          Exit Sub
        End If
        If Len(Replace(.Text, "«", vbNullString))  Len(Replace(.Text, "»", vbNullString)) Then
          MsgBox "Unmatched chevron '«»' pairs in the document.", vbCritical + vbOKOnly, "Error!"
          Exit Sub
        End If
        If Len(Replace(.Text, "[", vbNullString))  Len(Replace(.Text, "]", vbNullString)) Then
          MsgBox "Unmatched square bracket '[]' pairs in the document.", vbCritical + vbOKOnly, "Error!"
          Exit Sub
        End If
        If Len(Replace(.Text, "(", vbNullString))  Len(Replace(.Text, ")", vbNullString)) Then
          MsgBox "Unmatched parenthesis '()' pairs in the document.", vbCritical + vbOKOnly, "Error!"
          Exit Sub
        End If
      End With
      Set Rng = Nothing
      ' Restore Screen Updating
      Application.ScreenUpdating = True
      End Sub

      Beyond that, code could be added to loop though each paragraph for each of the unmatched symbol-pair scenarios, stopping when it found a mismatch. Of course, one has to allow for the possibility that a valid paragraph-level mis-match might occur with the counterpart to an opening symbol being in a later paragraph.

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

    • #1288895

      Many thanks to you both. I’ll give these macros a go and report back!

    • #1289222

      I’ve tried them both and they work well – thank you so much.

      I have to admit a preference for the first macro – thanks jscher2000 – as it highlights each error as it finds it, giving one the option to correct or not, and does not fail the )( test – ie paired brackets but the wrong way round! Great work.

      Thanks again.

      PS Wonder why Word hasn’t built a similar test into their grammar checker (although perhaps the 2007/10 versions do?)

      • #1289606

        PS Wonder why Word hasn’t built a similar test into their grammar checker (although perhaps the 2007/10 versions do?)

        Not in Word 2010. Maybe in the next version…

        • #1289678

          jscher2000

          PS I’ve made one small enhancement to your macro. I’ve added an extra line – MsgBox (“Finished!”) – above ‘End Sub’ so that, even when there is nothing to correct, you know when the macro has finished running.

          V

    Viewing 3 reply threads
    Reply To: Spotting missing brackets in Word?

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

    Your information: