Microsoft Office Forums How to repeat a search followed by editing text?

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-21-2019, 04:11 AM
jonesy497 jonesy497 is offline How to repeat a search followed by editing text? Windows 10 How to repeat a search followed by editing text? Office 2019
Novice
How to repeat a search followed by editing text?
 
Join Date: Aug 2019
Posts: 6
jonesy497 is on a distinguished road
Default How to repeat a search followed by editing text?


I used the record macro function to record myself doing Ctrl + h to search for "[a-z]/[a-z]" (a / that is preceded and followed by any alphabetic character) and then used the arrow keys to move the cursor and insert spaces either side of the /, without overwriting the alphabetic characters that become highlighted in the search.

What I'm struggling with is how to repeat this action for the rest of the document? I want to search, then edit text, then search again, etc, until the end of the document. The search function alone just does the search, and if I use the search and replace function, because the alphabetic characters are highlighted in the search, replacement text overwrites those characters, which I don't want. I feel like there's some basic loop command or something but I can't find any examples of code that are similar enough to what I want, and I've tried and tested a few different approaches with no luck.

Thanks.
Reply With Quote
  #2  
Old 08-21-2019, 07:14 AM
gmaxey gmaxey is online now How to repeat a search followed by editing text? Windows 10 How to repeat a search followed by editing text? Office 2016
Word MVP 2003-2009
 
Join Date: May 2010
Location: Marble, NC
Posts: 936
gmaxey will become famous soon enoughgmaxey will become famous soon enough
Default

Code:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
  Dim oRng As Range
  Set oRng = ActiveDocument.Range
  With oRng.Find
    .Text = "([a-z])(/)([a-z])"
    .MatchWildcards = True
    .Replacement.Text = "\1 \2 \3"
    .Execute Replace:=wdReplaceAll
  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 08-21-2019, 09:08 AM
jonesy497 jonesy497 is offline How to repeat a search followed by editing text? Windows 10 How to repeat a search followed by editing text? Office 2019
Novice
How to repeat a search followed by editing text?
 
Join Date: Aug 2019
Posts: 6
jonesy497 is on a distinguished road
Default

lbl_Exit:
Exit Sub

What does this part mean?
Reply With Quote
  #4  
Old 08-21-2019, 10:33 AM
gmaxey gmaxey is online now How to repeat a search followed by editing text? Windows 10 How to repeat a search followed by editing text? Office 2016
Word MVP 2003-2009
 
Join Date: May 2010
Location: Marble, NC
Posts: 936
gmaxey will become famous soon enoughgmaxey will become famous soon enough
Default

It is just style and preference. I prefer not to hit End Sub so I always Exit before.
Exit Sub


lblExit: is just a lable e.g., I might have use GoTo lbl_Exit: or Resume lbl_Exit: is some procedures.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #5  
Old 08-22-2019, 12:38 AM
jonesy497 jonesy497 is offline How to repeat a search followed by editing text? Windows 10 How to repeat a search followed by editing text? Office 2019
Novice
How to repeat a search followed by editing text?
 
Join Date: Aug 2019
Posts: 6
jonesy497 is on a distinguished road
Default

Oh my goodness that worked perfectly! But I have no idea how lol

Thank you!
Reply With Quote
  #6  
Old 08-22-2019, 07:20 AM
gmaxey gmaxey is online now How to repeat a search followed by editing text? Windows 10 How to repeat a search followed by editing text? Office 2016
Word MVP 2003-2009
 
Join Date: May 2010
Location: Marble, NC
Posts: 936
gmaxey will become famous soon enoughgmaxey will become famous soon enough
Default

Adding the ( ) around the three elements of the find text term defines them as groups


So in the replace with term you are replacing with the first group of found characters, a space, the second group (your /), a space and finally the third group of found characters.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #7  
Old 08-22-2019, 09:10 AM
jonesy497 jonesy497 is offline How to repeat a search followed by editing text? Windows 10 How to repeat a search followed by editing text? Office 2019
Novice
How to repeat a search followed by editing text?
 
Join Date: Aug 2019
Posts: 6
jonesy497 is on a distinguished road
Default

What does this part mean/do?

" .Replacement.Text = "\1 \2 \3" "
Reply With Quote
  #8  
Old 08-22-2019, 01:38 PM
gmaxey gmaxey is online now How to repeat a search followed by editing text? Windows 10 How to repeat a search followed by editing text? Office 2016
Word MVP 2003-2009
 
Join Date: May 2010
Location: Marble, NC
Posts: 936
gmaxey will become famous soon enoughgmaxey will become famous soon enough
Default

Replace the found text with group 1 "\1" "a space" group 2 "\2" "another space" and finally group 3 "\3"
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #9  
Old 08-23-2019, 03:01 AM
jonesy497 jonesy497 is offline How to repeat a search followed by editing text? Windows 10 How to repeat a search followed by editing text? Office 2019
Novice
How to repeat a search followed by editing text?
 
Join Date: Aug 2019
Posts: 6
jonesy497 is on a distinguished road
Default

Is there a reason this wouldn't work in another macro which also contains "Dim char As Range"?
Reply With Quote
  #10  
Old 08-23-2019, 05:37 AM
Guessed's Avatar
Guessed Guessed is offline How to repeat a search followed by editing text? Windows 10 How to repeat a search followed by editing text? Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 1,260
Guessed has a spectacular aura aboutGuessed has a spectacular aura aboutGuessed has a spectacular aura about
Default

Yes, it doesn't work in your other macro because you have no idea on what you are doing.

If you need help to diagnose why a macro doesn't work, you need to provide the problematic code and give us an idea on what you think the macro should be doing for you.
'Dim char as Range' won't cause a macro to fail unless you have already declared char.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #11  
Old 08-23-2019, 06:58 AM
jonesy497 jonesy497 is offline How to repeat a search followed by editing text? Windows 10 How to repeat a search followed by editing text? Office 2019
Novice
How to repeat a search followed by editing text?
 
Join Date: Aug 2019
Posts: 6
jonesy497 is on a distinguished road
Default

Well then I guess you're right and I do "have no idea on what" I am doing, but maybe that's why I'm here?

I have another macro that does a bunch of stuff, and the Dim char As Range goes towards formatting tables. It's over 500 lines of code and on another PC, so even if I knew how to provide it here, not knowing what could be causing the problem at all and just posting the entire macro seems silly.

When I added gmaxey's code into the beginning of my other macro, it just skipped it altogether, no error messages or anything broken. The only thing I could think was that maybe having 2 x Dim something As Range might cause a problem, but I didn't think it would.
Reply With Quote
  #12  
Old 08-23-2019, 10:32 AM
gmaxey gmaxey is online now How to repeat a search followed by editing text? Windows 10 How to repeat a search followed by editing text? Office 2016
Word MVP 2003-2009
 
Join Date: May 2010
Location: Marble, NC
Posts: 936
gmaxey will become famous soon enoughgmaxey will become famous soon enough
Default

You can have as many range variables as you want.

Dim oRng as Range
Dim oChar as Range
Dim oWord as Range
Dim oOpenRange As Range


If your insert this code in another macro then it should run the same as it did on its own:

Code:
Dim oRng As Range   Set oRng = ActiveDocument.Range   With oRng.Find     .Text = "([a-z])(/)([a-z])"     .MatchWildcards = True     .Replacement.Text = "\1 \2 \3"     .Execute Replace:=wdReplaceAll   End With
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #13  
Old 08-23-2019, 05:37 PM
Guessed's Avatar
Guessed Guessed is offline How to repeat a search followed by editing text? Windows 10 How to repeat a search followed by editing text? Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 1,260
Guessed has a spectacular aura aboutGuessed has a spectacular aura aboutGuessed has a spectacular aura about
Default

Since Greg's macro works by itself, you can choose an alternative approach to including it in your bigger macro - by using a pointer rather than the actual macro content.

Put Greg's macro in the same module as the larger macro. When you want Greg's macro to run, add a line in the bigger macro with the name of Greg's macro eg
Code:
Sub MyBigMacro()
  MsgBox "Working steps"
  ScratchMacro
  MsgBox "More working steps"
End Sub

Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
  Dim oRng As Range
  Set oRng = ActiveDocument.Range
  With oRng.Find
    .Text = "([a-z])(/)([a-z])"
    .MatchWildcards = True
    .Replacement.Text = "\1 \2 \3"
    .Execute Replace:=wdReplaceAll
  End With
lbl_Exit:
  Exit Sub
End Sub
If you step through that code, you can see that one macro runs a second macro. This is often used with Functions rather than Subs but the principle is the same.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
possible to repeat text with numbered list? Burt Word 5 07-14-2019 04:02 PM
outlook search tool locked with key word, it will not allow deleting or editing roni Outlook 0 07-12-2017 07:31 AM
How to repeat a search followed by editing text? Repeat text dgame27 Word 1 07-06-2016 09:14 PM
Automatically Repeat Text in Footer Luxanais Word 2 09-24-2015 10:10 AM
How to repeat a search followed by editing text? Text will repeat in the fields ypurcaro Word 1 02-07-2013 01:46 AM


All times are GMT -7. The time now is 03:15 PM.


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