#1
|
|||
|
|||
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!!! |
#2
|
||||
|
||||
What range are you counting - the entire document, a selection, a bookmarked range, something else?
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Quote:
i am interested in the entire document. Out of curiosity, could i do it just for pages 3 to 5 out of 10 pages? |
#4
|
||||
|
||||
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 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] |
#5
|
|||
|
|||
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! |
#6
|
||||
|
||||
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] |
#7
|
|||
|
|||
Quote:
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) |
#8
|
||||
|
||||
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] |
#9
|
|||
|
|||
Quote:
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. |
#10
|
|||
|
|||
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 |
#11
|
||||
|
||||
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] |
#12
|
|||
|
|||
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 |
|
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 |
problem with automatic formatting on quotation marks | Bobosmite | Word | 3 | 05-27-2010 07:06 PM |