Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find and replace text in document

Status
Not open for further replies.

ssnygeo

Technical User
Mar 19, 2009
1
US
I am trying to create a routine which will search for multiple words in MSWord documents and replace them with user specified text which are entered in textboxes. The search words were defined as constants (T1 and T2) below. My sub does not produce any errors but the search word text is not replaced in the document. I have attached a copy of my script below. Any help sorting this out would be much appreciated.


Private Sub Replace_text_multiple()

Const T1 = "project"
Const T2 = "address"

For i = 1 To 2

With ActiveDocument.Content.Find
.Text = T & i
With .Replacement
.Text = Me.Controls("Textbox" & i).Value
End With
.Execute Replace:=wdReplaceAll
End With
Next

End Sub
 
tip: use the Option Explicit instruction ...
What about this ?
Code:
Private Sub Replace_text_multiple()
Dim i As Long, T As String
For Each T in Array("project", "address")
    i = i + 1
    With ActiveDocument.Content.Find
        .Text = T
        .Replacement.Text = Me.Controls("Textbox" & i).Value
        .Execute Replace:=wdReplaceAll
    End With
Next
End Sub

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top