Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-26-2020, 06:51 AM
alex100 alex100 is offline Highlight keywords in the pasted text Windows 7 64bit Highlight keywords in the pasted text Office 2016
Advanced Beginner
Highlight keywords in the pasted text
 
Join Date: May 2020
Posts: 79
alex100 is on a distinguished road
Default Highlight keywords in the pasted text

The script below is supposed to highlight certain keywords in the text that was just pasted. The problem is that it's only highlighting the first keyword in the array, without any results for the rest of the keywords.

What could be the problem, please?



Alex

Code:
Dim pasted_content As Word.range
Dim r As Long
Dim Keywords
    
Keywords = Array("keyword1", "keyword2", "keyword3")

Set pasted_content = Selection.range
pasted_content.Paste

For r = 0 To UBound(Keywords)
    With pasted_content.Find
        .Text = Keywords(r)
        .Format = True
        Do While .Execute(Forward:=True) = True
            pasted_content.HighlightColorIndex = wdYellow
        Loop
    End With
Next
Reply With Quote
  #2  
Old 05-26-2020, 07:43 AM
macropod's Avatar
macropod macropod is offline Highlight keywords in the pasted text Windows 7 64bit Highlight keywords in the pasted text Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
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's wrong with a simple Find/Replace loop, rather than a nested one? Far quicker:
Code:
Sub Demo()
Application.ScreenUpdating = False
Dim Rng As Range, arrWords, i As Long
arrWords = Array("keyword1", "keyword2", "keyword3")
Options.DefaultHighlightColorIndex = wdYellow
Set Rng = Selection.Range
With Rng
  .Paste
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Forward = True
    .Wrap = wdFindStop
    .Replacement.Text = "^&"
    .Replacement.Highlight = True
    For i = 0 To UBound(arrWords)
      .Text = arrWords(i)
      .Execute Replace:=wdReplaceAll
    Next
  End With
End With
Application.ScreenUpdating = True
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 05-26-2020, 08:46 AM
alex100 alex100 is offline Highlight keywords in the pasted text Windows 7 64bit Highlight keywords in the pasted text Office 2016
Advanced Beginner
Highlight keywords in the pasted text
 
Join Date: May 2020
Posts: 79
alex100 is on a distinguished road
Default

Very nice, thank you! I have two questions, please...

1. how can I use RGB colors for highlighting? I tried using "Options.DefaultHighlightColorIndex = RGB(190, 190, 190)" but without any success;

2. how can I add an extra condition to the find function so that it will only find (and highlight) those keywords that have not already been highlighted? I tried using ".Font.HighlightColorIndex = wdNoHighlight", but once again, without any results. I am trying to avoid double-highlighting, as for example when I want to highlight keyword "great music" in green and keyword "music" in red. Without a stop in place, you would get "great" highlighted in green, and "music" in red. I'd like both words ("great music") to be shown in green, as a single keyword.

Alex
Reply With Quote
  #4  
Old 05-26-2020, 02:13 PM
macropod's Avatar
macropod macropod is offline Highlight keywords in the pasted text Windows 7 64bit Highlight keywords in the pasted text Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
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

1. You can't - only the 16 indexed highlights can be used.
2. Insert:
Code:
    .Highlight = False
before:
Code:
    .Replacement.Highlight = True
For the situation where you're highlighting both 'music' and 'great music', process 'great music' before you process 'music'.

And, to vary the highlighting, for each expression, use something like:
Code:
Sub Demo()
Application.ScreenUpdating = False
Dim Rng As Range, arrWords, i As Long, h As Long
arrWords = Array("keyword1 & keyword2", "keyword1", "keyword3")
Set Rng = Selection.Range
With Rng
  .Paste
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Forward = True
    .Wrap = wdFindStop
    .Replacement.Text = "^&"
    .Highlight = False
    .Replacement.Highlight = True
    For i = 0 To UBound(arrWords)
      h = i Mod 14
      If h < 6 Then
        h = h + 2
      Else
        h = h + 3
      End If
      Options.DefaultHighlightColorIndex = h
      .Text = arrWords(i)
      .Execute Replace:=wdReplaceAll
    Next
  End With
End With
Application.ScreenUpdating = True
End Sub
This gives 14 highlights, omitting black & white.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 05-27-2020, 03:49 AM
alex100 alex100 is offline Highlight keywords in the pasted text Windows 7 64bit Highlight keywords in the pasted text Office 2016
Advanced Beginner
Highlight keywords in the pasted text
 
Join Date: May 2020
Posts: 79
alex100 is on a distinguished road
Default

It's really important to be able to select the highlighting colors from a large color gamut. I will only use three colors (depending on the importance of the highlighted keyword), and these colors need to be very shallow/light, almost unnoticeable. The colors in the WdColorIndex are very strong. For red, there are only two options, with no light option at all.

While searching Google, I found Greg Maxey's highlighter script, which is based on changing the text shading (Shading.BackgroundPatternColor), giving you endless options in terms of color gamut:

gregmaxey.com/word_tip_pages/highlighter.html

Paul, is there a way to make your initial version of the script work with 'Shading.BackgroundPatternColor' instead of 'HighlightColorIndex'? I tried to do that myself, but nothing seemed to work - i'm simply lost here.

This would be very helpful!

Thank you,
Alex
Reply With Quote
  #6  
Old 05-27-2020, 05:01 AM
macropod's Avatar
macropod macropod is offline Highlight keywords in the pasted text Windows 7 64bit Highlight keywords in the pasted text Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
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

Since you can't us a BackgroundPatternColor property as a Find parameter, you'll need to revert to a nested loop. Try:
Code:
Sub Demo()
Application.ScreenUpdating = False
Dim Rng As Range, arrWords, i As Long, RGB As Long
arrWords = Array("keyword1 & keyword2", "keyword1", "keyword2", "keyword3")
Set Rng = Selection.Range
Rng.Paste
For i = 0 To UBound(arrWords)
  Select Case i
    Case 0: RGB = wdColorGray05
    Case 1: RGB = wdColorPaleBlue
    Case 2: RGB = wdColorLightOrange
    Case 3: RGB = wdColorPink
  End Select
  With Selection
    With .Find
      .ClearFormatting
      .Text = arrWords(i)
      .Replacement.Text = ""
      .Format = False
      .Forward = True
      .Wrap = wdFindStop
    End With
    Do While .Find.Execute
      If .InRange(Rng) = False Then Exit Do
      If .Shading.BackgroundPatternColor = wdColorAutomatic Then .Shading.BackgroundPatternColor = RGB
      .Collapse wdCollapseEnd
    Loop
  End With
  Rng.Select
Next
Application.ScreenUpdating = True
End Sub
Insert your preferred RGB colour values in place of the named constants in the Select Case statement.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 05-27-2020, 06:05 AM
alex100 alex100 is offline Highlight keywords in the pasted text Windows 7 64bit Highlight keywords in the pasted text Office 2016
Advanced Beginner
Highlight keywords in the pasted text
 
Join Date: May 2020
Posts: 79
alex100 is on a distinguished road
Default

Brilliant, thank you!

One more thing, and I'd like to point out that I'm asking this mostly because I know there will be other people that will find this thread and may use your code for themselves. Today I spend about half an hour searching Google for Word highlighters, and while I found quite many, almost no one had the ability to use RGB colors. Except for Greg's Highlighter, they all used the 'HighlightColorIndex' property.

As you wrote it, I understand that each keyword in the 'arrWords' array has a corresponding color in the 'Case 0,1,2,etc' RGB list. So there's a keyword/color pair, and I hope I'm not mistaken. Please let me know if I am.

Like me, I think most people who use such highlighters need to assign a single color to an entire list of keywords. Basically, you want ten keywords to be highlighted in red, 15 keywords in blue, and so on.

With this in mind, I think the best approach would be to have multiple arrays, each one with a corresponding color.

Something like this:

arrWords1 = Array("keyword1", "keyword2")
arrWords2 = Array("keyword1", "keyword2", "keyword3")
arrWords3 = Array("keyword1", "keyword2", "keyword3", "keyword4", "keyword5", "keyword6")

Then, the RGB list should basically assign one color to each array:

arrWords1: RGB (10, 175, 150)
arrWords2: RGB (74, 18, 188)
arrWords3: RGB (150, 175, 190)

What do you think? This is what I am looking for, and I'm sure this solution would be the closest one to most user's needs.

Most probably, this is just a minor change to your code, but with lots of added value.

Alex
Reply With Quote
  #8  
Old 05-27-2020, 04:35 PM
macropod's Avatar
macropod macropod is offline Highlight keywords in the pasted text Windows 7 64bit Highlight keywords in the pasted text Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
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 alex100 View Post
Today I spend about half an hour searching Google for Word highlighters, and while I found quite many, almost no one had the ability to use RGB colors. Except for Greg's Highlighter, they all used the 'HighlightColorIndex' property.
Hardly surprising, since few need anything more than the available highlight options.
Quote:
Originally Posted by alex100 View Post
Like me, I think most people who use such highlighters need to assign a single color to an entire list of keywords. Basically, you want ten keywords to be highlighted in red, 15 keywords in blue, and so on.

With this in mind, I think the best approach would be to have multiple arrays, each one with a corresponding color.

Something like this:

arrWords1 = Array("keyword1", "keyword2")
arrWords2 = Array("keyword1", "keyword2", "keyword3")
arrWords3 = Array("keyword1", "keyword2", "keyword3", "keyword4", "keyword5", "keyword6")

Then, the RGB list should basically assign one color to each array:

arrWords1: RGB (10, 175, 150)
arrWords2: RGB (74, 18, 188)
arrWords3: RGB (150, 175, 190)

What do you think?
I really can't see any benefit in that. And, contrary to what you suppose, implementation would require substantially more code. As it is, the Select Case statement can already be expanded to cover as many shading options as you want and, with only a trivial coding change, could be made to loop through those options multiple times if you have more array entries than shading options. Furthermore, if you want to have:
Quote:
ten keywords to be highlighted in red, 15 keywords in blue, and so on
all that would entail is building the Select Case statement with either:
• 15 blue, 15 red, and so on, then padding out the arrWords array with empty elements for keyword groups that have less than 15 items; or
• only the required number of colour definitions to match what's in the arrWords array,
either of which could be used in conjunction with specifying a range of values for 'i' in the Select Case statement. For example:
Code:
  Select Case i
    Case 0 - 14: RGB = wdColorRed
    Case 15 - 29: RGB = wdColorOrange
    Case 30 - 44: RGB = wdColorYellow
    Case 44 - 59: RGB = wdColorGreen
    Case 60 - 74: RGB = wdColorBlue
  End Select
Note: There's no need for the 'i' values to span equal size ranges in the Select Case statement; they're only used that way here to show how they might work in conjunction with the first bullet point.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #9  
Old 05-28-2020, 09:19 AM
alex100 alex100 is offline Highlight keywords in the pasted text Windows 7 64bit Highlight keywords in the pasted text Office 2016
Advanced Beginner
Highlight keywords in the pasted text
 
Join Date: May 2020
Posts: 79
alex100 is on a distinguished road
Default

I'm sorry, I was under the impression the changes would be minimal in terms of coding.

Anyhow, I did manage to adapt it precisely to my needs, so this has been very helpful!

Thank you!

Alex
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
I am trying to delete text of any format between two bold, recurring keywords or symbols qubie Word VBA 8 03-04-2020 03:20 PM
Highlight keywords in the pasted text how do I make pasted text the same size as the text it's being pasted into? David Lee Word 6 08-16-2015 10:46 AM
Highlight keywords in the pasted text Help with finding multiple keywords in a single document then highlight navyguy Word 2 01-03-2014 12:48 PM
Trying to highlight pasted text in a macro goldengate Word VBA 0 09-14-2010 09:41 PM
find - reading highlight - highlight all / highlight doesn't stick when saved bobk544 Word 3 04-15-2009 03:31 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 10:02 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