Link to home
Start Free TrialLog in
Avatar of Amzad Mohammad
Amzad MohammadFlag for India

asked on

Count the double quotes and Single Quotes and highlight them...

i) How we can count the double Quotes opening and closing ?
ii) How we can count the single quotes but not that quote which exist in between of the word i.e. con't (Ignore them)?

We have to highlight those quotes and count those using VBA Macro..
sample.doc
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

You can count particular characters or strings without using VBA. Do a Find and Replace, using the same character/string  for each.
Avatar of Amzad Mohammad

ASKER

Will you please give the vba macro to get the quotes exactly, because i am trying it but not able to do that...
It is not as simple as it seems...

Waiting for your positive response...

Thanks in Advance.
Sub CountQuotes()
Dim strWords() As String
Dim lngWord As Long
Dim intPos As Integer
Dim intSingleCount As Integer
Dim intDoubleCount As Integer
Dim strContent As String

strWords = Split(Application.ActiveDocument.Content, " ")

For lngWord = 0 To UBound(strWords)
    intPos = InStr(1, strWords(lngWord), Chr(145)) ' Starting smart single quote
    If intPos = 0 Then
        intPos = InStr(1, strWords(lngWord), Chr(146)) ' Ending smart single quote
    End If
    If intPos > 0 Then
        Debug.Print lngWord & " " & strWords(lngWord)
        If intPos > 1 And intPos < Len(strWords(lngWord)) Then
            intSingleCount = intSingleCount + 1
        End If
    End If
Next

strContent = Application.ActiveDocument.Content
intDoubleCount = Len(strContent) - Len(Replace(strContent, Chr(147), "")) ' Starting smart double quote
intDoubleCount = intDoubleCount + _
                 Len(strContent) - Len(Replace(strContent, Chr(148), "")) ' Ending smart double quote

MsgBox "The document contains " & intSingleCount & " single quotes (ignoring those in contractions) " _
     & " and " & intDoubleCount & " double quotes"
End Sub

Open in new window

another method

Sub CountQuotes2()
    Dim c, c1, c2, c3, c4 As String
    c = Application.ActiveDocument.Content

    c1 = Replace(c, " ‘", "")
    c2 = Replace(c, "’ ", ""): c2 = Replace(c, "’,", ""): c2 = Replace(c, "’.", "")
    c3 = Replace(c, "”", "")
    c4 = Replace(c, "“", "")
    
    Dim sq, dq
    sq = (Len(c) - Len(c1)) / 2
    sd = (Len(c) - Len(c3))
    
    MsgBox "The document contains " _
           & sq & " single quotes (ignoring those in contractions) and " _
           & sd & " double quotes"
End Sub

Open in new window


it is giving 2 and 4...
Hello Martin,
Greetings of the day, Hope you are doing well..

As you can see in the attachment the dialog shows that there are 02 single quotes (ignoring contractions) but if you see the sample (Attached again herewith) there are 02 OPENING single quotes only, that's why in Msgbox it seems that single quotes are perfectly closed because of counting 02 but in actual the counting of single quotes should be 04 ( 02 for opening and 02 for closing) ...

The same i already done before, but not reaching upto the mark...

For example:-
i) 'Martin' is 'Good Human being. It's honor to have some words with you.
Output :- The output should be Opening Single Quotes 02 and Closing Single Quotes 01 excluding contractions.

if possible please make these quotes highlight..

Thanks for your kind concern...
sample.doc
AfterMacro.jpg
This does not break out opening and closing double quotes. Let me know if you want that.
Sub CountQuotes()
Dim strWords() As String
Dim lngWord As Long
Dim intPos As Integer
Dim intOpeningSingleQuote As Integer
Dim intClosingSingleQuote As Integer
Dim intDoubleCount As Integer
Dim strContent As String

strWords = Split(Application.ActiveDocument.Content, " ")

For lngWord = 0 To UBound(strWords)
    strWords(lngWord) = Replace(strWords(lngWord), ",", "")
    strWords(lngWord) = Replace(strWords(lngWord), ":", "")
    strWords(lngWord) = Replace(strWords(lngWord), ";", "")
    intPos = InStr(1, strWords(lngWord), Chr(145)) ' Opening smart single quote
    If intPos = 1 Then
        intOpeningSingleQuote = intOpeningSingleQuote + 1
    End If
    intPos = InStr(1, strWords(lngWord), Chr(146)) ' Closing smart single quote
    If intPos = Len(strWords(lngWord)) Then
        intClosingSingleQuote = intClosingSingleQuote + 1
    End If
Next

strContent = Application.ActiveDocument.Content
intDoubleCount = Len(strContent) - Len(Replace(strContent, Chr(147), "")) ' Starting smart double quote
intDoubleCount = intDoubleCount + _
                 Len(strContent) - Len(Replace(strContent, Chr(148), "")) ' Closing smart double quote

MsgBox "Opening Single Quotes " & intOpeningSingleQuote & " and " & _
       "Closing Single Quotes " & intClosingSingleQuote & " (ignoring those in contractions) and " & _
       "Double Quotes " & intDoubleCount
End Sub

Open in new window

This breaks them out,
Sub CountQuotes()
Dim strWords() As String
Dim lngWord As Long
Dim intPos As Integer
Dim intOpeningSingleQuote As Integer
Dim intClosingSingleQuote As Integer
Dim intOpeningDblQuotes As Integer
Dim intClosingDblQuotes As Integer
Dim strContent As String

strWords = Split(Application.ActiveDocument.Content, " ")

For lngWord = 0 To UBound(strWords)
    strWords(lngWord) = Replace(strWords(lngWord), ",", "")
    strWords(lngWord) = Replace(strWords(lngWord), ":", "")
    strWords(lngWord) = Replace(strWords(lngWord), ";", "")
    intPos = InStr(1, strWords(lngWord), Chr(145)) ' Opening smart single quote
    If intPos = 1 Then
        intOpeningSingleQuote = intOpeningSingleQuote + 1
    End If
    intPos = InStr(1, strWords(lngWord), Chr(146)) ' Closing smart single quote
    If intPos = Len(strWords(lngWord)) Then
        intClosingSingleQuote = intClosingSingleQuote + 1
    End If
Next

strContent = Application.ActiveDocument.Content
intOpeningDblQuotes = Len(strContent) - Len(Replace(strContent, Chr(147), "")) ' Opening smart double quote
intClosingDblQuotes = Len(strContent) - Len(Replace(strContent, Chr(148), "")) ' Closing smart double quote

MsgBox "Opening Single Quotes " & intOpeningSingleQuote & " and " & _
       "Closing Single Quotes " & intClosingSingleQuote & " (ignoring those in contractions) and " & _
       "Opening Double Quotes " & intOpeningDblQuotes & " and " & _
       "Closing Double Quotes " & intClosingDblQuotes

End Sub

Open in new window

Thank You Martin...
Is it possible to highlight all the occurrences of quotes (single,double and opening,closing) in the same code??
Please let me know how you want it highlighted.
As in the attachment highlight the quotes excluding contractions.
highlight.jpg
ASKER CERTIFIED SOLUTION
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank You Martin.....
Thank You Martin...
There was  a little bit changes in your solution to reach desired output..

Finally i did it with the help of you..

Thanks once again..
You're welcome and I'm glad I was able to help.

If you expand the “Full Biography” section of my profile you'll find links to some articles I've written that may interest you.

Marty - Microsoft MVP 2009 to 2017
              Experts Exchange MVE 2015
              Experts Exchange Top Expert Visual Basic Classic 2012 to 2017