Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-16-2021, 06:03 PM
poetofpiano poetofpiano is offline Move Selection to First of Two Search Candidates Windows 10 Move Selection to First of Two Search Candidates Office 2016
Novice
Move Selection to First of Two Search Candidates
 
Join Date: Sep 2015
Posts: 25
poetofpiano is on a distinguished road
Default Move Selection to First of Two Search Candidates

I have a unique case where I want to move the selection forward in a document to the first of either the next highlighted word or the next word with turquoise font color. I know both actions are individually possible with:

Code:
    Selection.Find.ClearFormatting
    Selection.Find.Highlight = True
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindAsk
        .Format = True
        .MatchCase = False
    End With
    Selection.Find.Execute
Code:
    Selection.Find.ClearFormatting
    Selection.Find.Font.ColorIndex = wdTurquoise
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute
But how do I make Word search for both at the same time and jump to the next instance of whichever one comes first?

Thank you!!
Reply With Quote
  #2  
Old 10-17-2021, 04:25 PM
Guessed's Avatar
Guessed Guessed is offline Move Selection to First of Two Search Candidates Windows 10 Move Selection to First of Two Search Candidates Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

There may be a more elegant way to do this but this is my initial thoughts on how to solve it
Code:
Sub ChickenOrEgg()
  Dim aRng As Range, rngHigh As Range, rngTurq As Range  
  Set aRng = Selection.Range
  aRng.End = ActiveDocument.Range.End
  With aRng.Find
    .ClearFormatting
    .Highlight = True
    .Text = ""
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = True
    .MatchCase = False
    If .Execute Then Set rngHigh = aRng.Duplicate
  End With
  
  Set aRng = Selection.Range
  aRng.End = ActiveDocument.Range.End
  With aRng.Find
    .ClearFormatting
    .Font.ColorIndex = wdTurquoise
    .Text = ""
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = True
    .MatchCase = False
    If .Execute Then Set rngTurq = aRng.Duplicate
  End With
  
  If rngHigh Is Nothing Then
    If rngTurq Is Nothing Then
      MsgBox "No hits for either"
    Else
      rngTurq.Select
    End If
  Else
    If rngTurq Is Nothing Then
      rngHigh.Select
    Else
      If rngHigh.Start < rngTurq.Start Then
        rngHigh.Select
      Else
        rngTurq.Select
      End If
    End If
  End If
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 10-19-2021, 01:27 PM
poetofpiano poetofpiano is offline Move Selection to First of Two Search Candidates Windows 10 Move Selection to First of Two Search Candidates Office 2016
Novice
Move Selection to First of Two Search Candidates
 
Join Date: Sep 2015
Posts: 25
poetofpiano is on a distinguished road
Default

Thank you so much for the solution!
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dragging a selection to copy it, not to move it wardw Word VBA 2 11-28-2019 11:09 AM
Move Selection to First of Two Search Candidates How to move a selection one cell up using VBA? Bumba Excel Programming 6 11-19-2019 08:50 AM
Move Selection to First of Two Search Candidates Move Selection to the next Column on a Page (Not a table) GuitarForLife Word VBA 7 02-09-2018 01:59 PM
Search a folder to move an email to koolman31 Outlook 0 02-11-2016 10:29 AM
Move Selection to First of Two Search Candidates Search and reduce the range of a text selection paik1002 Word VBA 1 12-17-2015 04:51 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 05:06 PM.


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