Results 1 to 6 of 6
  1. #1
    2 Star Lounger
    Join Date
    Feb 2001
    Location
    McLean, VA
    Posts
    122
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Need Macro for Parens

    Does anybody have a macro to check the left and right parens in a document, to find any mismatches, omissions, etc.?

    Thanks in advance,
    George Tipton

  2. #2
    Star Lounger
    Join Date
    Feb 2011
    Location
    Washington, DC
    Posts
    87
    Thanks
    5
    Thanked 5 Times in 5 Posts
    This is a very hard problem, mostly because it is difficult to define a mismatch unambiguously. Is a mismatch two right parens without a left paren anywhere in the intervening text? If so, what do you do about numbered lists (manually created) in this format: 1) 2) etc?

    In any case, after the first mismatch, every paren in the rest of the doc will look like a mismatch.

    If you'd be happy with a simple match check (ie when the macro encounters a left paren it looks for the next right paren and then checks for intervening left parens), that is doable. But I don't think it would help much. You'd still have to look at each case individually, which you do just as well manually.

    Perhaps I am missing something obvious, and if there is such a macro I'd like to see the thinking behind it.

    - Jessica

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

    georic1 (2012-11-27)

  4. #3
    2 Star Lounger
    Join Date
    Feb 2001
    Location
    McLean, VA
    Posts
    122
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Thank you for responding, Jessica. I should have noted that I try to make sure not to use the format you indicated for the sole purpose of keeping the number of left and right parens equal. I currently use a bogus Find & Replace and note the number of "replacements" that are made, which Word provides automatically in a pop-up. I do this both for the left and right parens and check that the number of "replacements" is the same. But having the same number of left and right parens doesn't necessarily mean they have been employed correctly. That's why I am asking if anyone has a macro that can check that each left has a matching right in the vicinity (however that is defined). Since someone recently posted a macro that does the same thing for quotation marks, I thought that a macro to check parens might be doable by substitution.

  5. #4
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    3,944
    Thanks
    0
    Thanked 203 Times in 184 Posts
    A fairly simple macro-based test is:
    Code:
    Sub TestParens()
    Dim oPara As Paragraph
    For Each oPara In ActiveDocument.Paragraphs
      With oPara.Range
        If (Len(.Text) - Len(Replace(Replace(.Text, "(", vbNullString), ")", vbNullString))) Mod 2 <> 0 Then
          .Select
          MsgBox "Selected paragraph has unmatched parens", vbExclamation
          Exit Sub
        End If
      End With
    Next
    End Sub
    Cheers,

    Paul Edstein
    [MS MVP - Word]

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

    georic1 (2012-11-28)

  7. #5
    Star Lounger
    Join Date
    Feb 2011
    Location
    Washington, DC
    Posts
    87
    Thanks
    5
    Thanked 5 Times in 5 Posts
    Cool! Rerquires eyeballing by a human, but it does draw attention to the suspect paragraphs one by one. You could probably jigger a replacement for this line:

    For Each oPara In ActiveDocument.Paragraphs

    that worked with a section at a time, or some other range you want to check.

  8. #6
    2 Star Lounger
    Join Date
    Feb 2001
    Location
    McLean, VA
    Posts
    122
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Thanks, Paul. It works exactly like the Quotes macro, just what I was looking for! It will save me a lot of time.

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
  •