View Single Post
 
Old 05-01-2021, 10:22 AM
Steve Kunkel Steve Kunkel is offline Windows 10 Office 2019
Advanced Beginner
 
Join Date: May 2019
Location: Seattle area
Posts: 81
Steve Kunkel is on a distinguished road
Default In Find and Replace, Wrap is not acting as expected.

Hi Folks,

I've attached the .dotm file. When the template is opened, a userform (FormMain) collects input from the user, then makes a bunch of replacements. "[n]" gets changed to the student's name, and so on. A function, "DoFindReplace," gets called for this.

The "[n]" codes (tokens?) exist in my QuickParts too, so after doing all the replacements, a bunch of Custom Properties are also created/assigned. Then, later when I'm working on the student's report, and insert a QuickPart, I can run an additional module called "FixParts." This reads the Custom Properties, and calls DoFindReplace, to put the student's information into the report.

This was all working nicely, but I wanted to speed up the Find and Replace function, so I tried to remove some redundancy... So now I have this in the FormMain code, and in the FixParts code:
Code:
    ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument ' Define these here, so they're not in the func.
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
       ' .Text = FindText
       ' .Replacement.Text = ReplaceText
        .Forward = True
        .Wrap = wdFindContinue
        .MatchWildcards = 0 'Never set this to true.
       ' .MatchCase = vCase
    End With
...then, inside the DoFindReplace function is the bare minimal:
Code:
Sub DoFindReplace(FindText As String, ReplaceText As String, vCase As String)
  'This module gets called from the MainForm code
    With Selection.Find
        .Text = FindText
        .Replacement.Text = ReplaceText
        .MatchCase = vCase
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
End Sub
So.... The FormMain code still works as expected when I first create a document from the .dotm file. However the FixParts module is not working anymore. It will make the first replacement then stop. I have it assigned to a Ribbon button (Word 2019). If I keep clicking the button, it doesn't help. BUT if I click to a different place in the document and run FixParts again, then it does make ALL the replacements.

So I'm guessing that I'm not using the ".Wrap = wdFindContinue" at Line 18 correctly??

Sorry about the long post -- I can explain more if needed. File is attached. General recommendations are also welcomed. And folks are welcome to use the psych report template for personal use, if anyone has a use for it.
-Steve
Attached Files
File Type: dotm @PsychRep - to Share.dotm (93.8 KB, 8 views)
Reply With Quote