Trouble with Find and Replace vba routine

Joined
Feb 13, 2018
Messages
3
Reaction score
1
I can't figure out what is wrong with the following Word vba routine that I'm trying to run in Word 2016, and I would appreciate any corrections and insights into what I did wrong, i.e., what I should do to avoid getting a syntax error when I run subtest():


Code:
Sub subtest()
    subFindAndReplaceFirstStoryOfEachTypeTest("Mary E. Jones Living Trust", "Sally Ford")
End Sub


Sub subFindAndReplaceFirstStoryOfEachTypeTest(strTx2Find, strReplacemtTx2bInserted)
    'https://wordmvp.com/FAQs/Customization/ReplaceAnywhere.htm
  Dim rngStory As Range
  For Each rngStory In ActiveDocument.StoryRanges
    With rngStory.Find
      .Text = strTx2Find
      .Replacement.Text = strReplacemtTx2bInserted
      .Wrap = wdFindContinue
      .Execute Replace:=wdReplaceAll
    End With
  Next rngStory

End Sub 'subFindAndReplaceFirstStoryOfEachTypeTest(strTx2Find, strReplacemtTx2bInserted)
 
Last edited by a moderator:
Joined
Feb 13, 2018
Messages
3
Reaction score
1
I solved my own problem. I am embarrassed for having forgotten that vba requires the use of "call", i.e. the subroutine "subtest()" should be coded as follows:

Sub subtest()
call subFindAndReplaceFirstStoryOfEachTypeTest("Mary E. Jones Living Trust", "Sally Ford")
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top