Someone on the Microsoft VBA general board asked how to highlight all the text between quotation marks in a document. I worked on it for a while and thought I would share the code which uses (1) a CountWord function from the MVP site, (2) the browser object, the wdExtend action, and (4) some simple message boxes.
Question: How would I use the Range Object instead of the Selection Object? Is there any advantage to doing so?
Anyway, it works and I’m proud of it.
Sub HighlightBetweenQuotes() ' ' HighlightBetweenQuotes Macro ' Macro written 11/19/01 by Charles Kyle Kenyon ' ' Get number of quotation marks in document using CountWord function from MVP site ' That function is copyrighted and not repeated here, however, original ' can be downloaded and used from the MVP FAQ site ' As of the date this was written, the URL is: ' "]http://www.mvps.org/word/FAQs/MacrosVBA/Ge...placements.htm>[/url] ' CountWord is the second function on that page. ' Dim sResponse As String Dim iNumber As Integer Dim iCheck As Integer sResponse = """" iNumber = CountWord(sResponse) ' ' Check for even number of quotation marks (pairing) ' iCheck = iNumber / 2 If iCheck * 2 iNumber Then ' not an even number MsgBox Prompt:="Sorry, there are " & iNumber _ & " quotation marks in this document." _ & vbCrLf & "This will only work with paired " _ & "quotation marks.", _ Title:="Odd number of quotation marks!", _ Buttons:=vbExclamation Exit Sub End If ' ' ' Go to beginning of document Selection.HomeKey Unit:=wdStory ' ' Set Up Find " in Browse Object ' ' With Selection.Find .ClearFormatting .Text = """" .Replacement.Text = """" .Forward = True .Wrap = wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False .Execute End With Selection.Collapse (wdCollapseStart) Application.Browser.Target = wdBrowseFind ' For iNumber = 1 To iCheck 'number of pairs ' ' Select First quotation mark and move past it ' Application.Browser.Next Selection.Collapse (wdCollapseEnd) ' ' Extend Selection to next quotation mark and then bring back inside ' Selection.Extend Application.Browser.Next Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend ' ' Highlight selection ' Selection.Range.HighlightColorIndex = wdYellow Application.Browser.Next Application.Browser.Next Selection.MoveRight Unit:=wdCharacter, Count:=1 Next iNumber ' ' Return to beginning of document ' Selection.HomeKey Unit:=wdStory ' ' Let User know what happened. ' MsgBox Prompt:="All text between quotation marks highlighted in " _ & iCheck & " pairs of quotation marks. Finished." _ & vbCrLf & vbCrLf & "Please look it over now. Use Undo (Ctrl-Z) " _ & "to fix if necessary.", _ Title:="Finished with Highlighting", _ Buttons:=vbInformation End Sub
Here’s a “hot” link to Get the Number of Replacements Done (including CountWord function) on the MVP site – since the preformatted version doesn’t go live on this system. It’s not supposed to, I know.
Wanted to share and brag a bit. Any thoughts or refinements appreciated.