Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-20-2018, 04:01 AM
yolandal yolandal is offline How can I combine the code for Search + Replace? Windows 10 How can I combine the code for Search + Replace? Office 2019
Novice
How can I combine the code for Search + Replace?
 
Join Date: Nov 2018
Posts: 2
yolandal is on a distinguished road
Question How can I combine the code for Search + Replace?

Hi there,


For certain documents I'm using this code to make automatisch Search + Replace, but the code is getting very long.
Is there a way to combine, strip or trim this code, as most of it is the same for every change?

In that case I will have a better view of what I'm doing, instead of a code with 2000+ rules.

Thank you for your time.

Yolanda





Sub Ams1()
'
' Ams1 Macro
'
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = " "
.Replacement.Text = " "
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = " "
.Replacement.Text = " "
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False

End Sub
Reply With Quote
  #2  
Old 11-20-2018, 07:04 AM
VBorNotVB VBorNotVB is offline How can I combine the code for Search + Replace? Mac OS X How can I combine the code for Search + Replace? Office 2016 for Mac
Novice
 
Join Date: Oct 2018
Location: Southern California
Posts: 25
VBorNotVB is on a distinguished road
Default

Is this what you're looking for? Pass "" for sReplaceText for find operation only. Function returns True upon success.

Function FindReplaceText(ByVal sFindText As String, ByVal sReplaceText, Optional ByVal bMatchWholeWord As Boolean = False, Optional ByVal bMatchCase As Boolean = False, Optional ByVal bForward As Boolean = True, Optional ByVal iWrap As Integer = 1) As Boolean
FindReplaceText = False
If Documents.Count >= 1 Then
With Selection.Find
.ClearFormatting
.MatchWholeWord = bMatchWholeWord
.MatchCase = bMatchCase
.Text = sFindText
.Replacement.Text = sReplaceText
.Forward = bForward
.Wrap = iWrap ' 0: no wrap, 1: wrap, 2: ask user
FindText = .Execute
End With
End If
End Function
Reply With Quote
  #3  
Old 11-20-2018, 01:49 PM
macropod's Avatar
macropod macropod is offline How can I combine the code for Search + Replace? Windows 7 64bit How can I combine the code for Search + Replace? 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

If you have many Find/Replace expressions to process, you could use arrays. See, for example: https://www.msofficeforums.com/word/...fractions.html
Depending on your requirements, you might even keep your find/replace lists in a Excel workbook. See, for example: https://www.msofficeforums.com/word-...html#post72330
This could even be extended to processing a whole folder's documents. See, for example: https://www.msofficeforums.com/word-...er-footer.html

It also seems to me that, what you regard as "a code with 2000+ rules" might be greatly simplified through the use of wildcards and/or other, more efficient, find/replace expressions.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #4  
Old 11-22-2018, 03:39 AM
yolandal yolandal is offline How can I combine the code for Search + Replace? Windows 10 How can I combine the code for Search + Replace? Office 2019
Novice
How can I combine the code for Search + Replace?
 
Join Date: Nov 2018
Posts: 2
yolandal is on a distinguished road
Default

I'm going to try everything, thank you very much!
Reply With Quote
Reply

Tags
code, replace, search

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I combine the code for Search + Replace? Need some vba code to search a word document an replace it if len = 3 Belleke Word VBA 3 10-24-2016 01:00 AM
Search Replace Code BolligeA1 Word 8 02-22-2016 12:32 PM
How can I combine the code for Search + Replace? Need code to combine PDF files. D1TrueGod Word VBA 4 08-15-2015 06:15 AM
Search and replace/insert HTML code into Master File using tags dave8555 Excel 2 02-23-2014 03:51 PM
How can I combine the code for Search + Replace? Please help me to combine these code together lbf200n Word VBA 3 12-09-2012 04:22 PM

Other Forums: Access Forums

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