[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]
![]() |
There are isolated problems with current patches, but they are well-known and documented on this site. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
Home » Forums » AskWoody support » Productivity software by function » MS Word and word processing help » Need Macro for Parens
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
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.
A fairly simple macro-based test is:
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]
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
S | M | T | W | T | F | S |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.