Solved

Replace a string in a word file

Posted on 2004-10-09
4
182 Views
Last Modified: 2010-05-03
Hi all,

I have word document. I have a particular string in that document. I want to replace that string with another given string using Visual Basic. My original file has some tables and some images which should be modified. I only have to replace a string with another one.

Can you help me out.
If you can provide the source code, it would really help.

It is urgent.

thanks
mrigank
0
Comment
Question by:mrigank
  • 4
4 Comments
 
LVL 9

Accepted Solution

by:
Shahid Thaika earned 40 total points
ID: 12267297
Create a new Standard EXE project, add two text boxes, a command button and finally set reference to 'Microsoft Word 9.0 Object Library' (or similar. You should have word installed) Copy the code below into your source window. Text1 takes the text to be found and Text2, the word to replace the found text.
I have entered '2' as the parameter for the replace function...
2 = Replace All
0 = Replace None
1 = Replace First






Option Explicit
Private WApp As Word.Application
Private WordD As Word.Document

Private Sub Command1_Click()
Set WApp = New Word.Application
Set WordD = WApp.Documents.Open("C:\My Documents\myfile.doc")

With WApp.Selection.Find
    .Text = Text1
    .ClearFormatting
    .Replacement.ClearFormatting
    .Replacement.Text = Text2
    .Execute Replace:=2
    MsgBox "Done"
End With

If MsgBox("Do you want to save changes?", vbYesNo) = vbYes Then
    WApp.Documents.Close True
Else
    WApp.Documents.Close True
End If
End Sub


Hope this answers your question :).
0
 
LVL 9

Expert Comment

by:Shahid Thaika
ID: 12267325
I forgot to add the following in the second last line before 'End Sub'

WApp.Quit

But it won't make much difference if you don't include it. You just have to manually close word :).
0
 
LVL 9

Expert Comment

by:Shahid Thaika
ID: 12269642
Oops, one more mistake found (Silly of me). The code should be 'WApp.Documents.Close True' after the else part of the If statement. Not 'True'.
0
 
LVL 9

Expert Comment

by:Shahid Thaika
ID: 12332881
Dear mrigank, does the code work or needs any change?
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

775 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question