|
-
Aug 19th, 2003, 11:36 PM
#1
Thread Starter
Addicted Member
Finding and Replacing words in a Microsoft Word Document
Hello All,
Can anyone show me some sample code on how to insert/replace a word in a Microsoft Word Document. The code below will find the word, but how do I insert the replacement word? Please HELP! Thanks in advance.
Public Sub OpenWord()
Dim obWordApp As Word.Application
Dim obWordDoc As Word.Document
Dim myRange As Word.Range
'Dim wdActiveDoc As Word.Document
Dim gOptions
Dim gCount As Integer
Dim TextToFind As String
Dim TextToChange As String
Dim Result As Integer
Set obWordApp = CreateObject("Word.Application")
Set obWordDoc = obWordApp.Documents.Open("C:\Temp\BLANK.doc")
'Set myRange = obWordDoc.Content
TextToFind = "F3"
TextToChange = "I Work"
myRange.Find.Execute FindText:=TextToFind, Forward:=True
'gOptions = rtfWholeWord
'Result = obWordDoc.Content.Find(TextToFind, 0, , gOptions)
If myRange.Find.Found <> True Then
MsgBox "Text not found"
Else
MsgBox "Text Found"
'How do I insert TextToChange
End If
obWordDoc.Quit
Set obWordDoc = Nothing
End Sub
212 will lead you to the truth
-
Aug 20th, 2003, 01:21 AM
#2
You can use the macro-recorder to see how it can be done:
VB Code:
With Selection.Find
.Text = "S"
.Replacement.Text = "6"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
-
Aug 20th, 2003, 07:06 AM
#3
Thread Starter
Addicted Member
212 will lead you to the truth
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|