Results 1 to 5 of 5
  1. #1
    Silver Lounger Charles Kenyon's Avatar
    Join Date
    Jan 2001
    Location
    Sun Prairie, Wisconsin, Wisconsin, USA
    Posts
    2,049
    Thanks
    124
    Thanked 119 Times in 116 Posts

    Code to highlight all text between quotation marks (Word VBA)

    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.

    <pre>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:
    ' <URL: http://www.mvps.org/word/FAQs/MacrosVBA/Ge...placements.htm>
    ' 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
    </pre>


    Here's a "hot" link to <A target="_blank" HREF=http://www.mvps.org/word/FAQs/MacrosVBA/GetNoOfReplacements.htm> Get the Number of Replacements Done (including CountWord function)</A> on the MVP site - since the preformatted version doesn't go live on this system. <img src=/S/grin.gif border=0 alt=grin width=15 height=15> It's not supposed to, I know.

    Wanted to share and brag a bit. Any thoughts or refinements appreciated.
    Charles Kyle Kenyon
    Madison, Wisconsin

  2. #2
    Silver Lounger Charles Kenyon's Avatar
    Join Date
    Jan 2001
    Location
    Sun Prairie, Wisconsin, Wisconsin, USA
    Posts
    2,049
    Thanks
    124
    Thanked 119 Times in 116 Posts

    Re: Code to highlight all text between quotation marks (Word VBA)

    Although I was impressed by my work, turns out that it was overkill. As often happens, a better use of built-in features is much easier.

    The following was a posting by Klaus Linke responding to the same question. It suggests use of the * wild card.:
    -------------------------Start----
    Assuming your quotes are smart quotes, the following *wildcard*
    Find/Replace should work, too:
    Find what: "*"
    Replace with: ^& ((choose "More > Format > Highlighted"))

    The "Find what"-text should contain smart (= typographic = curly)
    quotes; you can copy/paste them from the text into the dialog, or type
    them with Alt-0147 and Alt-0148.

    If your text doesn't contain smart quotes, you can change the quotes
    to smart quotes with "Format > AutoFormat"; uncheck everything but
    "Smart quotes" in "Options...".
    -----------------------------------End---

    If it matters that the quotation marks are also being highlighted (rather than just the text between them) then it should be a simple matter to run a second replace to strip the highlight formatting.

    Oh well...
    Charles Kyle Kenyon
    Madison, Wisconsin

  3. #3
    5 Star Lounger
    Join Date
    May 2001
    Location
    Stuttgart, Baden-W, Germany
    Posts
    931
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code to highlight all text between quotation marks (Word VBA)

    Hi Charles,

    Your solution was very ingenious. I have never used Application.Browser: That is definitely something I'll keep in mind <img src=/S/cool.gif border=0 alt=cool width=15 height=15>

    With the wildcard search (as with most wildcard searches), you should jump to the beginning of the document first, so the search doesn't start in the middle of a quote.
    The wildcard search works with straight quotes, too ("*" matches everything from the first quote to (and including) the second quote, then starts searching again after the second quote. But as you said in your solution, it's pretty risky if there are any unmatched quotes: The replacement will get "out of step".

    (Sorry Charles, had asked about WordCount, and deleted it a few minutes later when I read the FAQ article at www.mvps.org/word)

    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16>Klaus

  4. #4
    Silver Lounger Charles Kenyon's Avatar
    Join Date
    Jan 2001
    Location
    Sun Prairie, Wisconsin, Wisconsin, USA
    Posts
    2,049
    Thanks
    124
    Thanked 119 Times in 116 Posts

    Re: Code to highlight all text between quotation marks (Word VBA)

    To continue this resource, the following related macro appeared in the Nov 19 issue of <A target="_blank" HREF=http://www.VitalNews.com/WordTips/Gold.htm> Word Tips Gold</A>. It strips out the quotation marks and changes the quoted text to Italics.

    <pre> Sub QuotesToItalic()
    Dim Redo As Integer, Ptr As Integer, Ptr1 As Integer
    Dim P As String, P1 As String
    If Selection.ExtendMode Then Exit Sub
    Redo = -1
    While Redo
    Selection.StartOf Unit:=wdParagraph, Extend:=wdMove
    Selection.MoveEnd Unit:=wdParagraph
    P = Selection.Text
    Ptr = InStr(P, Chr(34))
    If Ptr = 0 Then Ptr = InStr(P, Chr$(147))
    If Ptr > 0 Then
    Selection.MoveLeft Unit:=wdCharacter, Extend:=wdMove
    Selection.MoveRight Unit:=wdCharacter, Count:=Ptr
    Selection.MoveEnd Unit:=wdParagraph
    P1 = Selection.Text
    Ptr1 = InStr(P1, Chr(34))
    If Ptr1 = 0 Then
    Ptr1 = InStr(P1, Chr$(148))
    EndChar = Chr$(148)
    Else
    EndChar = Chr$(34)
    End If
    If Ptr1 > 0 Then
    Selection.MoveLeft Unit:=wdCharacter, Count:=2, Extend:=wdMove
    Selection.Delete Unit:=wdCharacter
    Selection.MoveRight Unit:=wdCharacter, Count:=Ptr1 - 1, _
    Extend:=wdExtend
    Selection.Font.Italic = True
    Selection.MoveRight Unit:=wdCharacter, Extend:=wdMove
    Selection.Delete Unit:=wdCharacter
    Else
    Selection.MoveRight Unit:=wdCharacter, Extend:=wdMove
    Redo = 0
    End If
    Else
    Selection.MoveRight Unit:=wdCharacter, Extend:=wdMove
    Redo = 0
    End If
    Wend
    End Sub
    </pre>


    There are always different ways to do the same thing.
    Charles Kyle Kenyon
    Madison, Wisconsin

  5. #5
    5 Star Lounger
    Join Date
    May 2001
    Location
    Stuttgart, Baden-W, Germany
    Posts
    931
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code to highlight all text between quotation marks (Word VBA)

    The last macro works for nearly all combinations of straight and curly quotes, it only fails for curly opening quotes and straight closing quotes.
    If lines 10 and 11 are replaced with
    <pre> Ptr = InStr(P, Chr(147))
    If Ptr = 0 Then Ptr = InStr(P, Chr$(34))</pre>

    it works for all combinations.

    Also, the macro runs on one paragraph and then stops.
    To make it run on all paragraphs to the end of the document, you can remove the two lines "Redo=0" and put the following query at the end:

    <pre> If Selection.End + 1 = ActiveDocument.Content.End Then
    Redo = 0
    End If
    Wend
    End Sub</pre>


    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16>Klaus

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •