Loop to find and replace in a Word document
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/p[img]htps:/abc123.jpg[/img] fbcde5678 mr smith[/url]
to be replace with just [img]htps:/abc123.jpg[/img]
htps:/abc.kr/p[img]htps:/dce456.jpg[/img] kabcd1234 mr smith[/url]
replace with [img]htps:/dce456.jpg[/img]
...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 [img] and [/img] including those tags.
Any help appreciated. Thanks.
Can I ask for a similar solution here?
Quote:
Originally Posted by
gmaxey
Code:
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
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:
Code:
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.