• Need Macro for Parens

    Author
    Topic
    #486316

    [INDENT]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[/INDENT]

    Viewing 3 reply threads
    Author
    Replies
    • #1358018

      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

    • #1359318

      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.

    • #1359432

      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
      [Fmr MS MVP - Word]

      • #1359807

        Thanks, Paul. It works exactly like the Quotes macro, just what I was looking for! It will save me a lot of time.

    • #1359711

      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.

    Viewing 3 reply threads
    Reply To: Need Macro for Parens

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

    Your information: