Unless you are writing very short documents, cleaning up can be one of the hardest parts of writing. The following macro double-checks your document, paragraph by paragraph, to determine if you have a balanced number of parentheses. The macro counts the number of left parentheses in each paragraph of your document and makes sure you have the same number of right parentheses. If you don't, the macro inserts a paragraph before the unbalanced paragraph indicating the error.
Sub CheckParens() Dim WorkPara As String Dim CheckP() As Boolean Dim NumPara As Integer, J As Integer Dim LeftParens As Integer, RightParens As Integer Dim MsgText As String Dim OpenChar As String Dim CloseChar As String OpenChar = "(" CloseChar = ")" MsgText = "***Unbalanced parens in the next paragraph" NumPara = ActiveDocument.Paragraphs.Count ReDim CheckP(NumPara) For J = 1 To NumPara CheckP(J) = False WorkPara = ActiveDocument.Paragraphs(J).Range.Text If Len(WorkPara) <> 0 Then LeftParens = CountChars(WorkPara, OpenChar) RightParens = CountChars(WorkPara, CloseChar) If LeftParens <> RightParens Then CheckP(J) = True End If Next J For J = NumPara To 1 Step -1 If CheckP(J) Then Selection.HomeKey Unit:=wdStory, Extend:=wdMove If J > 1 Then Selection.MoveDown Unit:=wdParagraph, _ Count:=(J - 1), Extend:=wdMove End If Selection.InsertParagraphBefore Selection.MoveLeft Unit:=wdCharacter, Count:=1 Selection.Style = "Normal" Selection.TypeText Text:=MsgText End If Next J End Sub Private Function CountChars(A As String, C As String) As Integer Dim Count As Integer Dim Found As Integer Count = 0 Found = InStr(A, C) While Found <> 0 Count = Count + 1 Found = InStr(Found + 1, A, C) Wend CountChars = Count End Function
Note that there are actually two macros here. The CountChars function is called from within the main CheckParens macro. It is this latter macro (CheckParens) that is the one you should actually invoke on your document. When the macro is finished, you can search through the document, looking for the wording "***Unbalanced parens" to see where you may have problems.
Note, as well, that the way this macro is written, you could easily modify it to search for other "paired" characters. All you would need to do is change what is assigned to the OpenChar, CloseChar, and MsgText variables. For instance, if you wanted to check for unmatched brackets, you could change those variables in this way:
OpenChar = "[" CloseChar = "]" MsgText = "***Unbalanced brackets in the next paragraph"
Note:
WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (1018) applies to Microsoft Word 2007, 2010, 2013, and 2016. You can find a version of this tip for the older menu interface of Word here: Checking for Matching Parentheses.
Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!
Need to delete a range of pages out of the middle of your document? It's easy to do using editing techniques you already ...
Discover MoreOne of the final touches you can add to a document is to hyphenate it. This allows text to flow more smoothly from line ...
Discover MoreTypographers know that not all spaces are created equal. When creating a document, most people use spaces created by ...
Discover MoreFREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
2017-05-02 12:39:26
dave
Brilliant. For amateurs like myself, seeing little programming examples like this helps give my confidence to "take the lid off" VBA and see how I can approach things. Thanks Allen.
2017-05-01 14:15:37
Phil Reinemann
I haven't see an array of binary values used in VBS before so that was a enlightening answer!
The count-down of number of paragraphs was pretty neat too.
Just a couple of things I wouldn't have thought of until I saw this.
Got a version of Word that uses the ribbon interface (Word 2007 or later)? This site is for you! If you use an earlier version of Word, visit our WordTips site focusing on the menu interface.
FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2019 Sharon Parq Associates, Inc.
Comments