Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8913227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:24:23+00:00 2026-06-15T04:24:23+00:00

Here is my code, but it doesn’t make replace! First I change AnyText to

  • 0

Here is my code, but it doesn’t make replace!
First I change AnyText to TEMP, after I make a cycle for replace TEMP with elements of Array TargetList, but can’t replace TEMP.

Sub First()
'
' First Macro

    Dim i As Long
    i = 0
    Dim j As Long
    Dim myWord As String
    Dim msg As String
    myWord = "TEMP"
    TargetList = Array("AnyText", "NewWord1", "NewWord2", "NewWord3", "NewWord4") 
    For Each myStoryRange In ActiveDocument.StoryRanges
     With myStoryRange.Find
         .Text = "AnyText"
         .Replacement.Text = myWord
         .Wrap = wdFindContinue
         .Execute Replace:=wdReplaceAll
     End With
    Next myStoryRange

     For Each myStoryRange In ActiveDocument.StoryRanges
     With myStoryRange.Find
            Do While .Execute(FindText:=myWord, Forward:=True) _
                = True
                j = j + 1
            Loop
            msg = msg & "The string " & myWord & _
            " found " & j & " times."
        End With
        msg = msg & vbCrLf & vbCrLf
    MsgBox msg

     Next myStoryRange

       For Each myStoryRange In ActiveDocument.StoryRanges
     With myStoryRange.Find
         Do While j > -1
             .Text = "TEMP"
             .Replacement.Text = TargetList(i)
             msg = msg & "The string " & myWord & _
                " j = " & j & " i = " & i & TargetList(i) & " times."
              msg = msg & vbCrLf & vbCrLf
        MsgBox msg

             j = j - 1
             i = i + 1

             If i = 5 Then
                i = 0
             End If

         Loop
         .Wrap = wdFindContinue
         .Execute Replace:=wdReplaceAll
      End With
   Next myStoryRange
End Sub
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T04:24:24+00:00Added an answer on June 15, 2026 at 4:24 am

    This code will replace any instances of “AnyText” with “TEMP”, then will replace any instances of “TEMP” with the current (count of finds) mod 5:

    Sub First()
        Dim i As Long
        i = 0
        Dim myWord As String
        myWord = "TEMP"
        TargetList = Array("AnyText", "NewWord1", "NewWord2", "NewWord3", "NewWord4")
        With ActiveDocument.Content.Find
             .Text = "AnyText"
             .Replacement.Text = myWord
             .Wrap = wdFindContinue
             .Execute Replace:=wdReplaceAll
        End With
    
        With ActiveDocument.Content.Find
            .Text = "TEMP"
            .Replacement.Text = TargetList(i)
            .Wrap = wdFindContinue
            Do While .Execute(Replace:=wdReplaceOne) = True
                i = i + 1
                If i = 5 Then i = 0
                .Replacement.Text = TargetList(i)
            Loop
        End With
    End Sub
    

    Note that this will change all instances of “AnyText” – even if they’re part of a larger word (for example “myAnyTextWord” will be changed to “myTEMPWord”). If you only want to search for whole words, add .MatchWholeWord = True to each With block.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code, which compiles but doesn't bring back any data. Here
I want to disable <script> tags. Here's my code, but it doesn't work. document.getElementsByTagName('script').disabled
How can I set a turtle's color from an array? Here's my code but
here is my code it shares the folder but that does not work correctly
I've seen other questions here about PNS, in terms of code, but how does
Got this line of code here but its not working. private void Button_Click(object sender,
i have implemented code like here but it's not working for iOS 5.0, it
I used the code described here but now, when I do a for ...
enter code here Want to remove items from a Main list , but give
Just some example code here, but I have lists of strings that I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.