Log in

View Full Version : [SOLVED:] Loop to find and replace in a Word document



finn_2018
04-05-2017, 08:58 AM
Hi All,

I'm new to VBA and havent got a clue where to start on this. I need help on a macro that i can run that will find all instances of a string and replace it with only the middle part of the string e.g.

htps:/abc.kr/phtps:/abc123.jpg fbcde5678 mr smith[/url]

to be replace with just htps:/abc123.jpg

htps:/abc.kr/phtps:/dce456.jpg kabcd1234 mr smith[/url]

replace with htps:/dce456.jpg
...etc...

i.e. all strings will start with "htps:/abc.kr/p" and end with "mr smith[/url]"

I want to replace all these and only want to keep the section between the and including those tags.

Any help appreciated. Thanks.

gmaxey
04-05-2017, 10:28 AM
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oRng As Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "(htps:/abc.kr/p)(*)( *mr smith\[/url\])"
.Replacement.Text = "\2"
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
lbl_Exit:
Exit Sub
End Sub

finn_2018
04-05-2017, 12:19 PM
Superb Greg - thank you very much!

macropod
04-06-2017, 02:26 AM
Do be aware that you don't actually need a macro for this. You can do the same with a wildcard Find/Replace (which is all the macro uses), where:
Find = (htps:/abc.kr/p)(*)( *mr smith\[/url\])
Replace = \2

finn_2018
04-09-2017, 04:14 AM
Thanks Paul, very useful, i couldnt find any help online for Find and Replace that reflected that. Cheers

Paul_Hossler
04-09-2017, 07:15 AM
MS tends to hide all the really powerful stuff like Paul's suggestion


In the [Replace] dialog, click [More>>] and [Wildcards] which makes different rules apply. Word uses something close to Regular Expressions


[Special] opens another list of Word-peculiar symbols, including the RegEx grouping \n one. Some work in Find and some only work in Replace (MS's way to keep you on your toes)

18892

macropod
04-09-2017, 05:28 PM
i couldnt find any help online for Find and Replace that reflected that
See:
https://support.office.com/en-us/article/Find-and-replace-text-and-other-data-in-a-Word-document-c6728c16-469e-43cd-afe4-7708c6c779b7
http://wordmvp.com/FAQs/General/UsingWildcards.htm
http://www.gmayor.com/replace_using_wildcards.htm

elnaz_sn
11-11-2020, 04:06 PM
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oRng As Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "(htps:/abc.kr/p)(*)( *mr smith\[/url\])"
.Replacement.Text = "\2"
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
lbl_Exit:
Exit Sub
End Sub


I have a word document which has drop down lists inserted in some parts of tables and main texts inside the document:


https://i.stack.imgur.com/hjUr2.png (https://i.stack.imgur.com/hjUr2.png)


I need to be able to search inside the document , including the drop down list options (whether the dropdown item is selected or not), for the phrase "INSERT" and "Select", and when either is found, replace them numerically with "Field 1" , "Field 2". For the numbering, 1 is the first found instance at the top of the document.
My main current issue is no advanced or basic "Find" function includes the dropdown lists in word, how can I search them? I initially tried to do lists separately, and got this far:



Sub LoadSchedule()
Dim objCC As Integer
Dim objCL As Integer

For objCC = 1 To ActiveDocument.ContentControls.Count
If ActiveDocument.ContentControls(objCC).Type = wdContentControlComboBox Or _
ActiveDocument.ContentControls(objCC).Type = wdContentControlDropdownList Then

For objCL = 1 To objCC.DropdownListEntries.Count
ActiveDocument.ContentControls(objCC).DropdownListEntries(objCL).Select

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting

'Here do the search inside selected area and name each found
'area iterativley
Next
End If
Next
End Sub



However Ideally the whole thing would be done in one loop, any and all help is appreciated.

macropod
11-11-2020, 04:28 PM
Instead of resurrecting old threads just to hijack them with something quiet different to what the original thread is about, kindly start a new thread of your own, cross-referencing this one if appropriate.

Moreover, don't post the same question multiple times as you have done in this case.