Results 1 to 7 of 7
  1. #1
    Star Lounger
    Join Date
    Dec 2009
    Location
    Poole, UK
    Posts
    95
    Thanks
    22
    Thanked 2 Times in 2 Posts

    Question Spotting missing brackets in Word?

    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

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts
    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.
    Last edited by jscher2000; 2011-07-18 at 19:48.

  3. The Following User Says Thank You to jscher2000 For This Useful Post:

    Victor Delta (2011-07-21)

  4. #3
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    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
    [MS MVP - Word]

  5. The Following User Says Thank You to macropod For This Useful Post:

    Victor Delta (2011-07-21)

  6. #4
    Star Lounger
    Join Date
    Dec 2009
    Location
    Poole, UK
    Posts
    95
    Thanks
    22
    Thanked 2 Times in 2 Posts
    Many thanks to you both. I'll give these macros a go and report back!

  7. #5
    Star Lounger
    Join Date
    Dec 2009
    Location
    Poole, UK
    Posts
    95
    Thanks
    22
    Thanked 2 Times in 2 Posts
    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?)

  8. #6
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts
    Quote Originally Posted by Victor Delta View Post
    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...

  9. #7
    Star Lounger
    Join Date
    Dec 2009
    Location
    Poole, UK
    Posts
    95
    Thanks
    22
    Thanked 2 Times in 2 Posts
    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

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
  •