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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using the Find and Replace Object from Word

Status
Not open for further replies.

jeepxo

Programmer
Oct 1, 2002
228
CA
I have a word doc that contains word text boxes (different from say a VB text box)

When I use the Find and Replace function from Word itself (i.e. I have opened the .doc in word and pressed the find button) I can find the text in the text box.

When I try to find that same text from VB it does not find it.

ActiveDocument.Content.Select
With Selection.Find
.ClearFormatting
.Forward = True
.Execute FindText:=stringtofind, _
Replace:=wdReplaceAll, ReplaceWith:=replacementString

End With

This code will find string text in the body of the word document, but it will not find text within text boxes in the document.

Any ideas?
 
Dim oDOC As Word.Document
Dim oStory As Word.Range

Set oDOC = ActiveDocument
For Each oStory In oDOC.StoryRanges
With oStory.Find
.Execute FindText:="hello", _
ReplaceWith:="hi", _
Replace:=2 ' 2 = wdReplaceAll
End With
Next oStory
 
Nope...same result as what I have.

text that is entered on the word document can be found and replaced. If I create a text box on the word doc, and enter text in it that is found.
 
Option Explicit

Sub test()
Dim obj As Word.Shape

For Each obj In ActiveDocument.Shapes
If obj.Type = 12 Then ' 12 = msoOLEControlObject
With obj.OLEFormat.Object
Debug.Print .Text
.Text = "hi" ' do your search and replace here!
End With
End If
Next obj
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top