Results 1 to 9 of 9
  1. #1
    Lounger
    Join Date
    Jul 2008
    Posts
    36
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Need macro for quotation marks

    Hi!
    My document has sentences in quotation marks.
    Some of the phrases in quotation marks opened but not closed .
    I need a macro that can do.
    *If it has even number of quotation marks, it is true and go on.
    *If the number of quotation marks in a paragraph is odd, it is false and stop.
    How can I count a quota caracter (") in a paragraph and compare If there are odd number of caracters, stop the macro and show a message "stop !!! missing a quota character "
    "This is a true sentence." (2 quotation marks)
    "This is a false sentence. (1 quotation mark)

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts
    I don't know whether Word natively counts characters in a string. That would be nice. But not wanting to do a lot of research, I used Split() and counted the number of array dimensions (note: the correct count is one less than the number of elements in the array; since the array counter starts at zero, you can use the upper bound of the array). To account for straight and curved quotation marks, I fed all three into the function.

    Give it a try and see what you think (change the paragraph index from 2 to whatever you like, or use the selection.range):

    Code:
    Sub CountQuotes()
    Dim intChars As Integer
    intChars = ComputeCharCount(ActiveDocument.Paragraphs(2).Range, Array(Chr(34), Chr(147), Chr(148)))
    MsgBox "The paragraph contains " & intChars & " quotation marks, which " & _
        IIf(intChars Mod 2 = 0, "is", "is not") & " an even number."
    End Sub
    
    Function ComputeCharCount(rng As Word.Range, var As Variant) As Integer
    Dim intTotal As Integer, intCount As Integer, sarrTemp() As String
    intTotal = 0
    If IsArray(var) Then
        For intCount = 0 To UBound(var)
            sarrTemp() = Split(rng.Text, CStr(var(intCount)))
            intTotal = intTotal + UBound(sarrTemp)
        Next
    Else
        sarrTemp() = Split(rng.Text, CStr(var))
        intTotal = intTotal + UBound(sarrTemp)
    End If
    ComputeCharCount = intTotal
    End Function

  3. #3
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts
    Perhaps I should do the other part, too, looping through the paragraphs in the document.

    Code:
    Sub SelectUnbalancedParas()
    Dim intCount As Integer
    With ActiveDocument.Paragraphs
        For intCount = 1 To .Count - 1
            If ComputeCharCount(.Item(intCount).Range, Array(Chr(34), Chr(147), Chr(148))) Mod 2 <> 0 Then
                .Item(intCount).Range.Select
                MsgBox "The selected paragraph contains unbalanced quotation marks.", vbExclamation + vbOKOnly
                Exit Sub
            End If
        Next
    End With
    MsgBox "No unbalanced quotation marks found.", vbInformation + vbOKOnly
    End Sub
    
    Function ComputeCharCount(rng As Word.Range, var As Variant) As Integer
    Dim intTotal As Integer, intCount As Integer, sarrTemp() As String
    intTotal = 0
    If IsArray(var) Then
        For intCount = 0 To UBound(var)
            sarrTemp() = Split(rng.Text, CStr(var(intCount)))
            intTotal = intTotal + UBound(sarrTemp)
        Next
    Else
        sarrTemp() = Split(rng.Text, CStr(var))
        intTotal = intTotal + UBound(sarrTemp)
    End If
    ComputeCharCount = intTotal
    End Function

  4. #4
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    I'd take a different approach, so that straight & smart quotes aren't assumed to be balanced (eg “xxx" or "xxx”) where a straight quote is paired with a smart one, or pairs of opening or closing smart quotes (eg “xxx“ or ”xxx”) aren't assumed to be matched sets. Plus, my code is simpler:
    Code:
    Sub TestQuotes()
    Dim oPara As Paragraph
    For Each oPara In ActiveDocument.Paragraphs
      With oPara.Range
        If (Len(.Text) - Len(Replace(.Text, Chr(34), vbNullString))) Mod 2 <> 0 Then
          .Select
          MsgBox "Selected paragraph has unmatched plain quotes", vbExclamation
          Exit Sub
        End If
        If Len(Replace(.Text, Chr(147), vbNullString)) <> Len(Replace(.Text, Chr(148), vbNullString)) Then
          .Select
          MsgBox "Selected paragraph has unmatched smart quotes", vbExclamation
          Exit Sub
        End If
      End With
    Next
    End Sub
    You could add logic to allow the user to continue where the quote is known to span multiple paragraphs. By one convention, each such paragraph begins with an opening quote, but only the final one has a closing quote.
    Last edited by macropod; 2012-11-27 at 18:23. Reason: Code fix.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  5. #5
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts
    Quote Originally Posted by macropod View Post
    I'd take a different approach, so that straight & smart quotes aren't assumed to be balanced (eg “xxx" or "xxx”) where a straight quote is paired with a smart one, or pairs of opening or closing smart quotes (eg “xxx“ or ”xxx”) aren't assumed to be matched sets. Plus, my code is simpler:
    Oh, that is slick. I think in the second test you meant to compare Chr(147) with Chr(148)?

  6. #6
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Hi Jefferson,

    Code fixed. Thanks.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  7. #7
    New Lounger
    Join Date
    Nov 2012
    Location
    Los Angeles
    Posts
    1
    Thanks
    0
    Thanked 1 Time in 1 Post

    bless you

    Quote Originally Posted by macropod View Post
    I'd take a different approach, so that straight & smart quotes aren't assumed to be balanced (eg “xxx" or "xxx”) where a straight quote is paired with a smart one, or pairs of opening or closing smart quotes (eg “xxx“ or ”xxx”) aren't assumed to be matched sets. Plus, my code is simpler:
    Code:
    Sub TestQuotes()
    Dim oPara As Paragraph
    For Each oPara In ActiveDocument.Paragraphs
      With oPara.Range
        If If (Len(.Text) - Len(Replace(.Text, Chr(34), vbNullString))) Mod 2 <> 0 Then
          .Select
          MsgBox "Selected paragraph has unmatched plain quotes", vbExclamation
          Exit Sub
        End If
        If Len(Replace(.Text, Chr(147), vbNullString)) <> Len(Replace(.Text, Chr(148), vbNullString)) Then
          .Select
          MsgBox "Selected paragraph has unmatched smart quotes", vbExclamation
          Exit Sub
        End If
      End With
    Next
    End Sub
    You could add logic to allow the user to continue where the quote is known to span multiple paragraphs. By one convention, each such paragraph begins with an opening quote, but only the final one has a closing quote.
    Macropod, I know it's been a while since you posted this but I wanted you to know it was a life-saver for me yesterday. In 5 mins I found more missing quote problems than I want to admit in a novel I wrote. THANK YOU!

  8. The Following User Says Thank You to Zaeboe For This Useful Post:

    gjdalentini (2013-09-13)

  9. #8
    New Lounger
    Join Date
    Oct 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Question

    I don't know if you're still looking at this thread Macropod, but I have a question. I use single quotes for speech rather than double. I adapted the macro using the character codes to:

    Code:
    Sub TestQuotes()
    Dim oPara As Paragraph
    For Each oPara In ActiveDocument.Paragraphs
      With oPara.Range
        If (Len(.Text) - Len(Replace(.Text, Chr(39), vbNullString))) Mod 2 <> 0 Then
          .Select
          MsgBox "Selected paragraph has unmatched plain quotes", vbExclamation
          Exit Sub
        End If
        If Len(Replace(.Text, Chr(145), vbNullString)) <> Len(Replace(.Text, Chr(146), vbNullString)) Then
          .Select
          MsgBox "Selected paragraph has unmatched smart quotes", vbExclamation
          Exit Sub
        End If
      End With
    Next
    End Sub
    The trouble is, like this it also counts apostrophes. Is there any way to get it to ignore them?
    Last edited by macropod; 2014-10-16 at 06:55. Reason: Added code tags

  10. #9
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Using single quote marks isn't exactly standard practice.

    While it would be possible to use rather more elaborate code (a complete re-write would be required) to disregard apostrophes within words (e.g. it's), it simply isn't possible to do so for words that correctly end in apostrophes.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

Posting Permissions

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