#1
|
|||
|
|||
VBA to highlight words if used too much
So far I'm using a simple one that marks all words from a list. But that is too messy if the list is large.
Is it possible to create a macro to highlight only overused words? So that any word from a list is highlighted only if used more that X times in Y successive paragraphs or with less than Z characters between instances? |
#2
|
||||
|
||||
Yes, it's possible, but the processing would be time-consuming in a large document, since every string of Y paragraphs or Z characters must be checked.
Consider a document with 10 paragraphs with Y = 5. That's 6 separate checks (i.e. you need n-y+1 checks, where n = number of paragraphs). And that's just for one word. Do empty paragraphs count? With the inter-character separation, you'd need to find every occurrence and count the characters between them. Do tabs, paragraph breaks, punctuation, etc. count as characters?
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Time consuming for a PC is better than time consuming for me
As for specifics: - paragraph count won't be bigger than 5 full paragraphs - the character count of a doc files is rarely larger than 20 000 - empty paragraphs do exist but that doesn't matter if I could just set paragraph count - empty paragraph - just double ^p - word or character count is optional, paragraphs are much more useful |
#4
|
||||
|
||||
Try:
Code:
Sub Demo() Application.ScreenUpdating = False Dim i As Long, j As Long, k As Long, l As Long, x As Long, y As Long Dim Rng As Range, StrFnd As String With ActiveDocument StrFnd = InputBox("Please input the string to find.") x = InputBox("Please input the allowable string frequency.") y = InputBox("Please input the test paragraph interval.") For i = 1 To .Paragraphs.Count - 1 k = 0: l = 0 For j = i To .Paragraphs.Count Set Rng = .Range(.Paragraphs(i).Range.Start, .Paragraphs(j).Range.End) If Trim(.Paragraphs(j).Range.Text) <> vbCr Then k = k + 1 If k = y Then With Rng.Duplicate With .Find .ClearFormatting .Replacement.ClearFormatting .Format = False .Forward = True .MatchCase = False .MatchWholeWord = True .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False .Text = StrFnd .Replacement.Text = "" .Wrap = wdFindStop .Execute End With Do While .Find.Found If .InRange(Rng) Then l = l + 1 .Collapse wdCollapseEnd .Find.Execute Else Exit Do End If Loop End With If l > x Then With Rng.Find .ClearFormatting .Replacement.ClearFormatting .Format = True .Forward = True .MatchCase = False .MatchWholeWord = True .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False .Replacement.Highlight = True .Text = StrFnd .Replacement.Text = "^&" .Wrap = wdFindStop .Execute Replace:=wdReplaceAll End With End If End If Next Next End With Application.ScreenUpdating = True End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Macro to highlight a list of words | bakerkr | Word VBA | 4 | 10-19-2017 02:23 PM |
Highlight all underlined words | zdodson | Word VBA | 1 | 07-11-2013 04:53 PM |
Macro to highlight words | bertietheblue | Word VBA | 9 | 07-01-2013 12:39 PM |
Highlight and then replace multiple words | redhin | Word VBA | 5 | 03-05-2013 05:42 AM |
Find and highlight all words ending in -ly | RBLampert | Word VBA | 13 | 10-23-2012 04:45 PM |