Consulting

Results 1 to 5 of 5

Thread: Insert text then run multiple FIND/REPLACEs on the newly inserted text

  1. #1
    VBAX Regular
    Joined
    May 2019
    Location
    NJ
    Posts
    12
    Location

    Question Insert text then run multiple FIND/REPLACEs on the newly inserted text

    HELP!

    I can insert the text, using the correct STYLE, indent, etc. (code not included), but I cannot seem to get the FIND/REPLACE logic below to work on the newly inserted text only. My SourceText Array is loaded in order of what I expect to find. e.g. run thru the list until it finds a match, replace it, then stop. The code below changes none of the newly inserted test - however it sometimes changes text later on in the doc, way past the newly inserted code.

        For i = LBound(SourceText) To UBound(SourceText)
            Selection.Find.ClearFormatting
            Selection.Find.Text = SourceText(i)
    
            Selection.Find.Replacement.Text = TargetText(i)
            Selection.Find.Replacement.ClearFormatting
            Selection.Find.Replacement.Highlight = vbYellow
    
            Selection.Find.MatchCase = True
            Selection.Find.MatchWholeWord = True
            Selection.Find.MatchWildcards = False
            Selection.Find.MatchSoundsLike = False
            Selection.Find.MatchAllWordForms = False
            Selection.Find.Format = True
            Selection.Find.Forward = True
            Selection.Find.Wrap = wdFindStop ' wdFindContinue
    
            Selection.Find.Execute Replace:=wdReplaceOnce
            If Selection.Find.Found = True Then
                Debug.Print "Found"
                Selection.Find.Replacement.Font.Bold = True
                Exit For
            End If
        Next i
    Last edited by o1sowise; 05-28-2019 at 01:51 PM.

  2. #2
    It all revolves around 'Selection'. Your code gives no indication of what the Selection is or how you have inserted the text into the document and indeed whether that inserted text is selected in order for it to be the selection.
    You would be better working with ranges rather than the selection object and reset the start range for each iteration of the loop.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Regular
    Joined
    May 2019
    Location
    NJ
    Posts
    12
    Location
    THANK YOU, Graham!
    That's the logic piece of the puzzle I've been missing!!!

    It now works like a charm!!!!!
    Thanks again!
    Last edited by o1sowise; 05-29-2019 at 10:34 AM.

  4. #4
    VBAX Regular
    Joined
    May 2019
    Location
    NJ
    Posts
    12
    Location

    Question

    One final question:

    My code now inserts the text and changes certain phrases, just like I need.
    My only remaining issue is that the new text is indented differently than the text above it.
    How do I query the text above and make the indent of the new text match it?

  5. #5
    VBAX Regular
    Joined
    May 2019
    Location
    NJ
    Posts
    12
    Location
    I've moved this new question to a new post. Please answer there.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •