I've used the following code to find and replace text on every storyrange, although I'm looking specifically for footers/headers and mainbody.
For Each myStoryRange In ActiveDocument.StoryRanges
If myStoryRange.StoryType = wdPrimaryFooterStory Then
myStoryRange.Find.Execute FindText:=footerfindreplace.Item(i).FND, Forward:=True, ReplaceWith:=footerfindreplace.Item(i).replc, Wrap:=wdFindContinue, replace:=wdReplaceAll
End If
If myStoryRange.StoryType = wdFirstPageFooterStory Then
myStoryRange.Find.Execute FindText:=footerfindreplace.Item(i).FND, Forward:=True, ReplaceWith:=footerfindreplace.Item(i).replc, Wrap:=wdFindContinue, replace:=wdReplaceAll
End If
If myStoryRange.StoryType = wdEvenPagesFooterStory Then
myStoryRange.Find.Execute FindText:=footerfindreplace.Item(i).FND, Forward:=True, ReplaceWith:=footerfindreplace.Item(i).replc, Wrap:=wdFindContinue, replace:=wdReplaceAll
End If
Next myStoryRange
It worked allright for the footers and if the document had only one section. However I have documents with more than one section of course, and I'd rather go trough all the document. So I found a different approach:
With ActiveDocument
For Each Rng In .StoryRanges
On Error Resume Next
With Rng.Find
.ClearFormatting
.Replacement.ClearFormatting
.Format = False
.Forward = True
.Wrap = wdFindContinue
.Text = footerfindreplace.Item(i).FND
.Replacement.Text = footerfindreplace.Item(i).replc
.MatchCase = True
.Execute replace:=wdReplaceAll
End With
On Error GoTo 0
Next
For Each Sctn In .Sections
For Each HdFt In Sctn.Headers
With HdFt
If .LinkToPrevious = False Then
With .Range.Find
.ClearFormatting
.Replacement.ClearFormatting
.Format = False
.Forward = True
.Wrap = wdFindContinue
.Text = footerfindreplace.Item(i).FND
.Replacement.Text = footerfindreplace.Item(i).replc
.Execute replace:=wdReplaceAll
End With
End If
End With
Next
For Each HdFt In Sctn.Footers
With HdFt
If .LinkToPrevious = False Then
With .Range.Find
.ClearFormatting
.Replacement.ClearFormatting
.Format = False
.Forward = True
.Wrap = wdFindContinue
.Text = footerfindreplace.Item(i).FND
.Replacement.Text = footerfindreplace.Item(i).replc
.Execute replace:=wdReplaceAll
End With
End If
End With
Next
Next
End With
This works perfectly but has something I dislike, the word becomes unresponsive for about 10 seconds, regardless of the document. I also found there are 17 types of storyranges, and maybe that's the reason why it takes so long.
I know at least that with headers and footers (which are 6 of them) I can use the condition .Exists = true or false, to skip them. But that doesn't improve the result a lot.
I only have 5 words for replacement, Why does it become unresponsive? Is there a way to make it smooth?
Thanks for any help.
Update:
Upon reading the comments, I've tried the following with no avail
With ActiveDocument.StoryRanges(1).Find
.ClearFormatting
.Replacement.ClearFormatting
.Format = False
.Forward = True
.Wrap = wdFindContinue
.Text = footerfindreplace.Item(i).FND
.Replacement.Text = footerfindreplace.Item(i).replc
.MatchCase = True
.MatchAllWordForms = False
.MatchWholeWord = False
.MatchWildcards = False
End With
For Each Rng In ActiveDocument.StoryRanges
On Error Resume Next
With Rng.Find
.Text = footerfindreplace.Item(i).FND
.Replacement.Text = footerfindreplace.Item(i).replc
.Execute replace:=wdReplaceAll
End With
On Error GoTo 0
Next
For Each Sctn In ActiveDocument.Sections
For Each HdFt In Sctn.Headers
With HdFt
If .LinkToPrevious = False Then
If HdFt.Exists = True Then
With .Range.Find
.Text = footerfindreplace.Item(i).FND
.Replacement.Text = footerfindreplace.Item(i).replc
.Execute replace:=wdReplaceAll
End With
End If
End If
End With
Next
For Each HdFt In Sctn.Footers
With HdFt
If .LinkToPrevious = False Then
If HdFt.Exists = True Then
With .Range.Find
.Text = footerfindreplace.Item(i).FND
.Replacement.Text = footerfindreplace.Item(i).replc
.Execute replace:=wdReplaceAll
End With
End If
End If
End With
Next
Next
If I dont place
.Text = footerfindreplace.Item(i).FND
.Replacement.Text = footerfindreplace.Item(i).replc
In every loop, it will not replace. Also the update display is already false. Can anyone help?
Update: Recently I tried to search every section inside mystory ranges, hoping to filter out the order..
For Each storyrang In ActiveDocument.StoryRanges
For Each Sctn In storyrang.Sections
For Each rang In Sctn.Ranges
With rang
For ii = 1 To footerfindreplace.count
Call RngFnd(.Range, footerfindreplace.Item(i).FND, footerfindreplace.Item(i).replc) 'find and replace text in the given range
Next ii
End With
Next
Next
Next
The result however remains not good