2
\$\begingroup\$

I have this procedure to realise a find and replace on a word document. Since I have text boxes, using this way to do this is a bit a slower than what I'd like it to be, if it were just text on a plain doc document or tables and using just:

Document.Content.Find.Execute(FindText:=Find, MatchWholeWord:=True, ReplaceWith:=Replace, Replace:=Word.WdReplace.wdReplaceAll)

It goes really fast. Any ideas please?

Sub ReplaceWord(Find As String, Replace As String, Document As Word.Document)
    Dim myStoryRange = Document.Range()
'Iterates thru all the storyranges and finds and replace with the parameters of the sub Find, Replace in Document
    For Each myStoryRange In Document.StoryRanges
        If myStoryRange.StoryType <> Word.WdStoryType.wdMainTextStory Then
            With myStoryRange.Find
                .Text = Find
                .Replacement.Text = Replace
                .Wrap = Word.WdFindWrap.wdFindContinue
                .Execute(Replace:=Word.WdReplace.wdReplaceAll)
            End With
            Do While Not (myStoryRange.NextStoryRange Is Nothing)
                myStoryRange = myStoryRange.NextStoryRange
                With myStoryRange.Find
                    .Text = Find
                    .Replacement.Text = Replace
                    .Wrap = Word.WdFindWrap.wdFindContinue
                    .Execute(Replace:=Word.WdReplace.wdReplaceAll)
                End With
            Loop
        End If
    Next myStoryRange
End Sub
\$\endgroup\$

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.