#1
|
|||
|
|||
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 |
#2
|
|||
|
|||
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 |
#3
|
||||
|
||||
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] |
#4
|
|||
|
|||
I'm going to try everything, thank you very much!
|
Tags |
code, replace, search |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
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 |
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 |
Please help me to combine these code together | lbf200n | Word VBA | 3 | 12-09-2012 04:22 PM |