Microsoft Office Forums VBA to highlight words if used too much

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-22-2013, 04:36 AM
aolszewski aolszewski is offline VBA to highlight words if used too much Windows 8 VBA to highlight words if used too much Office 2010 32bit
Novice
VBA to highlight words if used too much
 
Join Date: Nov 2013
Posts: 2
aolszewski is on a distinguished road
Default 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?
Reply With Quote
  #2  
Old 11-22-2013, 05:33 AM
macropod's Avatar
macropod macropod is offline VBA to highlight words if used too much Windows 7 32bit VBA to highlight words if used too much Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 19,597
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

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
[MS MVP - Word]
Reply With Quote
  #3  
Old 11-22-2013, 05:50 AM
aolszewski aolszewski is offline VBA to highlight words if used too much Windows 8 VBA to highlight words if used too much Office 2010 32bit
Novice
VBA to highlight words if used too much
 
Join Date: Nov 2013
Posts: 2
aolszewski is on a distinguished road
Default

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
Reply With Quote
  #4  
Old 11-23-2013, 02:07 AM
macropod's Avatar
macropod macropod is offline VBA to highlight words if used too much Windows 7 32bit VBA to highlight words if used too much Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 19,597
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

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
[MS MVP - Word]
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA to highlight words if used too much Macro to highlight a list of words bakerkr Word VBA 4 10-19-2017 02:23 PM
VBA to highlight words if used too much Highlight all underlined words zdodson Word VBA 1 07-11-2013 04:53 PM
VBA to highlight words if used too much Macro to highlight words bertietheblue Word VBA 9 07-01-2013 12:39 PM
VBA to highlight words if used too much Highlight and then replace multiple words redhin Word VBA 5 03-05-2013 05:42 AM
VBA to highlight words if used too much Find and highlight all words ending in -ly RBLampert Word VBA 13 10-23-2012 04:45 PM


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


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