Results 1 to 3 of 3

Thread: Finding and Replacing words in a Microsoft Word Document

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2000
    Location
    Atlanta, GA
    Posts
    177

    Unhappy 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

  2. #2
    Frenzied Member Lightning's Avatar
    Join Date
    Oct 2002
    Location
    Eygelshoven
    Posts
    1,611
    You can use the macro-recorder to see how it can be done:
    VB Code:
    1. With Selection.Find
    2.         .Text = "S"
    3.         .Replacement.Text = "6"
    4.         .Forward = True
    5.         .Wrap = wdFindContinue
    6.         .Format = False
    7.         .MatchCase = False
    8.         .MatchWholeWord = False
    9.         .MatchWildcards = False
    10.         .MatchSoundsLike = False
    11.         .MatchAllWordForms = False
    12.     End With
    13.     Selection.Find.Execute Replace:=wdReplaceAll

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2000
    Location
    Atlanta, GA
    Posts
    177
    Thanks I Will try this.
    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
  •  



Click Here to Expand Forum to Full Width