?
Solved

Replace a string in a word file

Posted on 2004-10-09
4
Medium Priority
?
187 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
4 Comments
 
LVL 9

Accepted Solution

by:
Shahid Thaika earned 160 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month10 days, 22 hours left to enroll

770 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