Find & Replace Text from VB 6

T

TJ Forshee

I am using VB6 and Word97 - from VB I can open a new doc, I can find text, I
can add text, I can add Inline pics....... but I can't for the life of me get
it to replace text.

I have tried all kinds of examples and ideas......

This works..... but will only do the 1st find... and puts text in even if
the find fails....
msword.Selection.Find.Execute FindText:=oldStuff, Forward:=True
msword.Selection.TypeText Text:=newStuff

This does not work for me.....it does nothing......
msword.Selection.Find.Execute FindText:=oldStuff, ReplaceWith:=newStuff,
Forward:=True

Resetting the Find does not help make the above work.......
With msword.Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With

This should be easy..... HELP ! I liked Word.Basic so much better
 
J

Jonathan West

Hi TJ

The following will replace all the exaples of the search text with the
replacement text within the current selection

With msword.Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "Text to find"
.Replacement.Text = "Text to replace"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With

If you are using late binding, you will have to replace wdReplaceAll with
its value of 2.

If you want to want to do something more sophisticated than a simple
replace, then you can do something like this

With msword.Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "Text to find"
.Replacement.Text = "Text to replace"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
Do While .Execute
'your code goes here, the selection is currently marking the
found text
'this loop is entered if text is found (i.e. the Execute method
returns True
Loop
End With
 
H

Helmut Weber

Hi,
I am using VB6 and Word97 - from VB I can open a new doc, I can find text, I
can add text, I can add Inline pics....... but I can't for the life of me get
it to replace text.
This works..... but will only do the 1st find... and puts text in even if
the find fails....
msword.Selection.Find.Execute FindText:=oldStuff, Forward:=True
msword.Selection.TypeText Text:=newStuff
no wonder, as you have a find methode and a typetext methode,
that are in no way connnect logically.
Text is typed, no matter whether find was successfull or not.
This does not work for me.....it does nothing......
msword.Selection.Find.Execute FindText:=oldStuff, ReplaceWith:=newStuff,
Forward:=True
I guess, that "oldStuff" isn't in the selection, then nothing happens.
Apart from checking, what is actually in the selection,
you'd better avoid it at all. Use the range of the active document,
like this, with reference to the word library, of course:

Dim oWrd As Word.Application
Dim oDcm As Word.Document
Dim rDcm As Word.Range
Set oWrd = New Word.Application
Set oDcm = oWrd.Documents.Open("C:\test\test.doc")
Set rDcm = oWrd.Documents("test.doc").Range
With rDcm.Find
.Text = "x"
.Replacement.Text = ","
.Execute Replace:=wdReplaceAll
End With
oDcm.Save
oDcm.Close
Set oDcm = Nothing
oWrd.Quit
Set oWrd = Nothing
End Sub

Resetting search options beforehand might be a good idea.

Here from within word:

Public Sub ResetSearch()
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
End Sub


Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
T

TJ Forshee

Thanks Jonathan and Helmut..... everything both of you told me was obvious to
me and had been tried by me in my hours of frustration....... Jonathan - I
had put in the same code you suggested several times prior... with out
results.....

Your replies made me start to think past the actual code.......

And the answer is..... (I feel kind of stupid now).....

In my project references, I had MS Office 8.0 included..... but not MS Word
8.0...... which makes sense on why some of it was working, but some
wasn't.....

Everything is working GREAT now.... Thanks !
 
J

Jonathan West

TJ Forshee said:
Thanks Jonathan and Helmut..... everything both of you told me was obvious
to
me and had been tried by me in my hours of frustration....... Jonathan - I
had put in the same code you suggested several times prior... with out
results.....

Your replies made me start to think past the actual code.......

And the answer is..... (I feel kind of stupid now).....

In my project references, I had MS Office 8.0 included..... but not MS
Word
8.0...... which makes sense on why some of it was working, but some
wasn't.....

Everything is working GREAT now.... Thanks !

Its easily done:)

I'm glad that our code samples helped you confirm that there was nothing
wrong with the code and pointed you eventually in the right direction.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top