Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-15-2017, 12:58 PM
sojiro sojiro is offline Count words between quotation marks Windows 8 Count words between quotation marks Office 2016
Novice
Count words between quotation marks
 
Join Date: Jan 2017
Posts: 10
sojiro is on a distinguished road
Default Count words between quotation marks

Hi all,



i have been searching for this for ever and i cannot find any solution.
I have a text with many sentences within quotes.

For example:
blah blah blah “blah blah blah”

I need the code to output a windows with
a)Word count is: 5
b)Word count within quotes is: 3
c)Percentage is: 50%

So far what i do is
search then paste \“*\” then activate wildcard then find in main document then i get the number of the words within quotes and then calculate the percentage.

I would love a more automated way


Any help would be greatly appreciated!!!
Reply With Quote
  #2  
Old 01-15-2017, 09:07 PM
macropod's Avatar
macropod macropod is offline Count words between quotation marks Windows 7 64bit Count words between quotation marks Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

What range are you counting - the entire document, a selection, a bookmarked range, something else?
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 01-16-2017, 08:59 AM
sojiro sojiro is offline Count words between quotation marks Windows 8 Count words between quotation marks Office 2016
Novice
Count words between quotation marks
 
Join Date: Jan 2017
Posts: 10
sojiro is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
What range are you counting - the entire document, a selection, a bookmarked range, something else?
Hi macropod,

i am interested in the entire document.
Out of curiosity, could i do it just for pages 3 to 5 out of 10 pages?
Reply With Quote
  #4  
Old 01-16-2017, 12:56 PM
macropod's Avatar
macropod macropod is offline Count words between quotation marks Windows 7 64bit Count words between quotation marks Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Try:
Code:
Sub Demo()
Application.ScreenUpdating = False
Dim i As Long, j As Long
With ActiveDocument
  j = .ComputeStatistics(wdStatisticWords)
  With .Range
    With .Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .Text = "[“" & Chr(34) & "]*[" & Chr(34) & "”]"
      .Replacement.Text = ""
      .Forward = True
      .Wrap = wdFindStop
      .Format = False
      .MatchWildcards = True
      .Execute
    End With
    Do While .Find.Found
      i = i + .ComputeStatistics(wdStatisticWords)
      .Collapse wdCollapseEnd
      .Find.Execute
    Loop
    MsgBox "This document contains " & j & " words ," & vbCr & _
      "of which " & i & " (" & Format(i * 100 / j, "0.00") & _
      "%) are in quotes."
  End With
End With
Application.ScreenUpdating = True
End Sub
A different macro could be used for a defined range within the document and the simplest way to define that range would be by selecting it.

For PC macro installation & usage instructions, see: http://www.gmayor.com/installing_macro.htm
For Mac macro installation & usage instructions, see: http://word.mvps.org/Mac/InstallMacro.html
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 01-17-2017, 09:22 AM
sojiro sojiro is offline Count words between quotation marks Windows 8 Count words between quotation marks Office 2016
Novice
Count words between quotation marks
 
Join Date: Jan 2017
Posts: 10
sojiro is on a distinguished road
Default

Thanks a lot macropod,
works like a charm.

At some point i will assign the macro to a shortcut so that i can call it faster.

BTW why is it that if after opening the document, running the macro and closing again it asks me if i want to save even thought i have not changed a thing?
Is it normal after running a macro?

P.S.
Also added reputation. Totally deserved!
Reply With Quote
  #6  
Old 01-17-2017, 01:19 PM
macropod's Avatar
macropod macropod is offline Count words between quotation marks Windows 7 64bit Count words between quotation marks Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

I doubt the macro is causing that. Of course, if you added it to the document you're processing rather than to another document or template, that would count as a change.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 01-18-2017, 09:51 AM
sojiro sojiro is offline Count words between quotation marks Windows 8 Count words between quotation marks Office 2016
Novice
Count words between quotation marks
 
Join Date: Jan 2017
Posts: 10
sojiro is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
I doubt the macro is causing that. Of course, if you added it to the document you're processing rather than to another document or template, that would count as a change.
I created the the macro (ALT+F11, copy/paste) and now all i do is call it.
For example:
I open a document
I run the macro
I try to close the document without any change and it asks to save and i save it
I repeat the process and it asks again to save

All i do is open run close (save before closing since it asks)
Reply With Quote
  #8  
Old 01-19-2017, 05:31 PM
macropod's Avatar
macropod macropod is offline Count words between quotation marks Windows 7 64bit Count words between quotation marks Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

On its own, the macro doesn't make any changes to your document. Evidently, though, your document contains some other volatile content that Word is updating. You might be able to prevent the unwanted 'Save' prompts with:
Code:
Sub Demo()
Application.ScreenUpdating = False
Dim i As Long, j As Long, bSvd As Boolean
With ActiveDocument
  bSvd = .Saved
  j = .ComputeStatistics(wdStatisticWords)
  With .Range
    With .Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .Text = "[“" & Chr(34) & "]*[" & Chr(34) & "”]"
      .Replacement.Text = ""
      .Forward = True
      .Wrap = wdFindStop
      .Format = False
      .MatchWildcards = True
      .Execute
    End With
    Do While .Find.Found
      i = i + .ComputeStatistics(wdStatisticWords)
      .Collapse wdCollapseEnd
      .Find.Execute
    Loop
    MsgBox "This document contains " & j & " words ," & vbCr & _
      "of which " & i & " (" & Format(i * 100 / j, "0.00") & _
      "%) are in quotes."
  End With
  .Saved = bSvd
End With
Application.ScreenUpdating = True
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #9  
Old 01-22-2017, 07:13 AM
sojiro sojiro is offline Count words between quotation marks Windows 8 Count words between quotation marks Office 2016
Novice
Count words between quotation marks
 
Join Date: Jan 2017
Posts: 10
sojiro is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
On its own, the macro doesn't make any changes to your document. Evidently, though, your document contains some other volatile content that Word is updating. You might be able to prevent the unwanted 'Save' prompts with:
Code:
Sub Demo()
...
End Sub
Yes this works perfectly and without asking every time to save
I expect that there will be no problems with loosing any data right?
It is a stupid question since i am sure you would not publish such a code but still i need to ask.
Reply With Quote
  #10  
Old 01-22-2017, 08:09 AM
gmaxey gmaxey is offline Count words between quotation marks Windows 7 32bit Count words between quotation marks Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,428
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

sojiro,

Paul knows this but since you didn't inquire about it he may have left it unsaid to keep from complicating things. However, if your documents are more that just simple text (e.g. they have endnotes, footnotes, textboxes, etc.) it is a bit more involved to get a total count and even then "total" is rather elusive ;-).

Add a footnote to your text and run Paul's code. You will see that the count of words in the footnotes are not included in either the total or quoted word count. Try with this:

Code:
Public Sub Demo()
Dim oRng As Word.Range
Dim lngValidator As Long
Dim oShp As Shape
Dim lngWordsInQoutes As Long, lngWords As Long, bSvd As Boolean
  Application.ScreenUpdating = False
  bSvd = ActiveDocument.Saved
  'Fix any skipped blank Header/Footer issue
  lngValidator = ActiveDocument.Sections(1).Headers(1).Range.StoryType
  'Iterate through document story ranges
  For Each oRng In ActiveDocument.StoryRanges
    If oRng.StoryType < 12 Then
    'Iterate through all linked stories
    Do
      lngWords = lngWords + oRng.ComputeStatistics(wdStatisticWords)
      lngWordsInQoutes = lngWordsInQoutes + fcnCountInQuotes(oRng)
      On Error Resume Next
      Select Case oRng.StoryType
        Case 6, 7, 8, 9, 10, 11
          If oRng.ShapeRange.Count > 0 Then
            For Each oShp In oRng.ShapeRange
              If Not oShp.TextFrame.TextRange Is Nothing Then
                lngWordsInQoutes = lngWordsInQoutes + fcnCountInQuotes(oShp.TextFrame.TextRange)
              End If
            Next
          End If
        Case Else
          'Do Nothing
      End Select
      On Error GoTo 0
      'Get next linked story (if any)
      Set oRng = oRng.NextStoryRange
    Loop Until oRng Is Nothing
    End If
  Next
  MsgBox "This document contains " & lngWords & " words ," & vbCr & _
      "of which " & lngWordsInQoutes & " (" & Format(lngWordsInQoutes * 100 / lngWords, "0.00") & _
      "%) are in quotes."
  ActiveDocument.Saved = bSvd
lbl_Exit:
  Exit Sub
End Sub
Function fcnCountInQuotes(ByVal oRng As Word.Range) As Long
  With oRng.Find
    .ClearFormatting
    .Text = "[“" & Chr(34) & "]*[" & Chr(34) & "”]"
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchWildcards = True
    While .Execute
      fcnCountInQuotes = fcnCountInQuotes + oRng.ComputeStatistics(wdStatisticWords)
      oRng.Collapse wdCollapseEnd
    Wend
  End With
lbl_Exit:
  Exit Function
End Function
Then add a few words in a header or footer. Two words in the header of a 1 page document = 2 words. Two words in the header of a 1000 page document = 2 words.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #11  
Old 01-22-2017, 04:10 PM
macropod's Avatar
macropod macropod is offline Count words between quotation marks Windows 7 64bit Count words between quotation marks Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by sojiro View Post
Yes this works perfectly and without asking every time to save
I expect that there will be no problems with loosing any data right?
It is a stupid question since i am sure you would not publish such a code but still i need to ask.
The code won't cause you to lose any data. The modified version simply stores the document's current 'saved' state at the start and restores it at the end. That way, if you'd done some editing before running the macro, you'll still get the usual save prompts, etc. (as you will if you make any edits after running the macro), but you won't get them if you simply open the document, run the macro, then close it without further edits.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #12  
Old 01-29-2017, 12:59 PM
sojiro sojiro is offline Count words between quotation marks Windows 8 Count words between quotation marks Office 2016
Novice
Count words between quotation marks
 
Join Date: Jan 2017
Posts: 10
sojiro is on a distinguished road
Default

At the moment Paul's code is what i need but it is great to have Greg's code also since i am sure i will need it eventually.

so thank you both for your help
I appreciate your effort
Keep up the great job
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Straight Vs Curly Quotation Marks FOsmund Word 4 04-16-2015 01:48 PM
Quotation marks are inserted in wrong font sinz54 Word 5 01-11-2015 06:34 PM
Can't find space before closing quotation marks wardw Word 7 01-16-2014 12:13 PM
How do I select everything that is in quotation marks? Jophassa Word 4 09-02-2011 10:57 AM
Count words between quotation marks problem with automatic formatting on quotation marks Bobosmite Word 3 05-27-2010 07:06 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 07:46 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft