Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-24-2020, 01:16 PM
eduzs eduzs is offline Highlight every occurrence of ... Windows 10 Highlight every occurrence of ... Office 2019
Expert
Highlight every occurrence of ...
 
Join Date: May 2017
Posts: 260
eduzs is on a distinguished road
Default Highlight every occurrence of ...

How to programmatically (VBA) highlight in yellow any occurrence of " " (two blank spaces) in a document?
The code must select the character before and after, for the highlight to work, like
"? ?".


Thanks!
Reply With Quote
  #2  
Old 06-24-2020, 01:42 PM
gmaxey gmaxey is offline Highlight every occurrence of ... Windows 10 Highlight every occurrence of ... Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,421
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

Code:
Sub ScratchMacroI()
'A basic Word macro coded by Greg Maxey
Dim oRng As Range
  Set oRng = ActiveDocument.Range
    With oRng.Find
      .Text = "  "
      .MatchWildcards = True
      While .Execute
        oRng.InsertBefore "~"
        oRng.InsertAfter "~"
        oRng.HighlightColorIndex = wdYellow
        oRng.Collapse wdCollapseEnd
      Wend
    End With
lbl_Exit:
  Exit Sub
End Sub
Sub ScratchMacroII()
'A basic Word macro coded by Greg Maxey
Dim oRng As Range
  Set oRng = ActiveDocument.Range
    With oRng.Find
      .Text = "  "
      .MatchWildcards = True
      While .Execute
        oRng.MoveStart wdCharacter, -1
        oRng.MoveEnd wdCharacter, 1
        oRng.HighlightColorIndex = wdYellow
        oRng.Collapse wdCollapseEnd
      Wend
    End With
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 06-24-2020, 01:42 PM
eduzs eduzs is offline Highlight every occurrence of ... Windows 10 Highlight every occurrence of ... Office 2019
Expert
Highlight every occurrence of ...
 
Join Date: May 2017
Posts: 260
eduzs is on a distinguished road
Default

Works fine! Thanks!
Reply With Quote
  #4  
Old 06-24-2020, 02:13 PM
eduzs eduzs is offline Highlight every occurrence of ... Windows 10 Highlight every occurrence of ... Office 2019
Expert
Highlight every occurrence of ...
 
Join Date: May 2017
Posts: 260
eduzs is on a distinguished road
Default

Is there a single sub that will do this for a list of words? Based on your code.

This code I was editing is not working:

Code:
Set oDoc = ActiveDocument
aFind = Array(". ,", "house")
Set oRng = oDoc.range
For x = 0 To UBound(aFind)
    With oRng.Find
        .Text = aFind(x)
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        Do While .Execute(Forward:=True) = True: oRng.HighlightColorIndex = wdYellow: Loop
    End With
Next x
Reply With Quote
  #5  
Old 06-24-2020, 02:30 PM
gmaxey gmaxey is offline Highlight every occurrence of ... Windows 10 Highlight every occurrence of ... Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,421
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

If you are going to pass drive-by questions at least show some effort to post structured code and declare your variables.


Code:
Option Explicit
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim lngIndex As Long
Dim oDoc As Document
Dim oRng As Range
Dim arrFind
  Set oDoc = ActiveDocument
  arrFind = Array(". ,", "house")
  For lngIndex = 0 To UBound(arrFind)
    Set oRng = oDoc.Range
    With oRng.Find
      .Text = arrFind(lngIndex)
      .Format = True
      .MatchCase = False
      .MatchWholeWord = False
      .MatchWildcards = False
      .MatchSoundsLike = False
      .MatchAllWordForms = False
      Do While .Execute(Forward:=True) = True: oRng.HighlightColorIndex = wdYellow: Loop
   End With
Next lngIndex
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #6  
Old 06-24-2020, 03:09 PM
eduzs eduzs is offline Highlight every occurrence of ... Windows 10 Highlight every occurrence of ... Office 2019
Expert
Highlight every occurrence of ...
 
Join Date: May 2017
Posts: 260
eduzs is on a distinguished road
Default

Sorry! The code is inside another sub, so I forgot to copy the variable declaration part.
Works fine!
Thank you!
Reply With Quote
  #7  
Old 06-25-2020, 04:04 AM
eduzs eduzs is offline Highlight every occurrence of ... Windows 10 Highlight every occurrence of ... Office 2019
Expert
Highlight every occurrence of ...
 
Join Date: May 2017
Posts: 260
eduzs is on a distinguished road
Default

Hi gmaxey,

How to highlight the first character of a paragraph that starts with lowercase letters?

The search mask should look something like "^ p [a-z]"

Thanks.
Reply With Quote
  #8  
Old 06-27-2020, 12:58 PM
eduzs eduzs is offline Highlight every occurrence of ... Windows 10 Highlight every occurrence of ... Office 2019
Expert
Highlight every occurrence of ...
 
Join Date: May 2017
Posts: 260
eduzs is on a distinguished road
Default

Hi, this thread is solved. Last question is not need anymore.
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
highlight bar freeriding Project 0 12-07-2018 05:25 AM
Highlight every occurrence of ... Deleting all but one occurrence of text from a selection paik1002 Word VBA 3 12-15-2015 05:17 PM
Highlight every occurrence of ... VBA to highlight words if used too much aolszewski Word VBA 3 11-23-2013 02:07 AM
Highlight every occurrence of ... Mysterious bottom row occurrence Ulodesk Word Tables 2 05-26-2013 02:44 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 07:30 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