How replace text with the contents of a variable

Posted on 2009-05-26
Medium Priority
Last Modified: 2012-05-07
I can find and replace text using a macro but can't seem to figure out how to find text and replace it with the contents of a variable. I thought the following code would work but it doesn't. 'Plane' is the variable and 'Count' is a For-Next loop. So the first pass the replacement text would be the contents of Plane1, then Plane2, then Plane3 etc
.Replacement.Text = Plane & Count

Open in new window

Question by:agregson
  • 3
  • 2
LVL 77

Expert Comment

ID: 24472995
If you want to replace every occurrence of some text, then you don't need a VBA loop. You can just need to use something like this:

With ActiveDocument.Range.Find
      .Text = "Some Text"
      .Replacement.Text = Plane
      .Execute Replace:=wdReplaceAll
End With

Author Comment

ID: 24473121
Thanks Graham but that isn't what I need. Basically what is happening is this:

Find "[text" & Count & "]" (so on first loop this is [text1])
Insert contents of filename:="file" & Count & ".txt"
Then find [MoreText] and replace with the contents of Plane & Count (so on first loop this is Plane1)

This will then loop until the For-Next is finished.

Hope this makes sense.
LVL 77

Expert Comment

ID: 24475000
Perhaps you need something like this:
Sub FindLoop()
Dim Count As Integer
Dim Limit As Integer
Dim rng As Range
Limit = 100
For Count = 1 To Limit
    Set rng = ActiveDocument.Range
    With rng.Find
        .Text = "[text" & Count & "]"
        If .Execute(Replace:=wdReplaceNone) Then
            rng.InsertFile "file" & Count & ".txt"
            rng.End = ActiveDocument.Range.End
            .Text = "More Text"
            .Replacement.Text = "Plane" & Count
            If .Execute(Replace:=wdReplaceOne) Then
                MsgBox """More Text"" not found after  ""[text" & Count & "]"""
                Exit Sub
            End If
            MsgBox """[text" & Count & "]"" not found"
            Exit Sub
        End If
    End With
Next Count
End Sub

Open in new window


Author Comment

ID: 24485367
Thanks Graham but I think this is the line I'm having problems with

.Replacement.Text = "Plane" & Count

Everytime the code loops this will replace text with Plane1, Plane2 etc, but Plane1, Plane2 etc are variables that could contain anything e.g.
Plane1 = Monitor
Plane2 = Hair Cut
Plane3 = 5050

So I need to be able to replace the text with the contents of the variable and not the variable name. Hope this makes sense.

Accepted Solution

agregson earned 0 total points
ID: 24529458
Turns out I just had to create an array and use the following code

        .Replacement.Text = LinesVar(Count)

where Count is just the counter in a For-Next loop.

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
This video teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question