I have come across the code below that searches through an open word document and performs a find and replace within all areas (StoryRanges) of the document. It works fine, however i would like to ask how i could modify this code to look at all documents in a chosen folder and perform the find and replace for all docs within that folder?, rather than just the active document which is open?
My plan is to assign the macro to a button in Excel so that the user can click that, navigate to the folder and action the find and replace across lots of documents at once.
Am I able to amend the 'IN ActiveDocument.StoryRanges' section to look at a folder instead? I'm not sure what i can amend it to. btw... i am new to vba and trying to research & learn as i go... I very much appreciate your time, patience and any help you can give while i'm trying to find my feet with it - Alex.
Dim myStoryRange As Range
For Each myStoryRange In ActiveDocument.StoryRanges
With myStoryRange.Find
.Text = "Text to find to replace goes here"
.Replacement.Text = "And the replacement text goes here"
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End With
Do While Not (myStoryRange.NextStoryRange Is Nothing)
Set myStoryRange = myStoryRange.NextStoryRange
With myStoryRange.Find
.Text = "Text to find to replace goes here"
.Replacement.Text = "And the replacement text goes here"
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End With
Loop
Next myStoryRange
DIRDo a search on SO or Google. You will find plenty of examples.