Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Replace a string in a word file

Posted on 2004-10-09
4
Medium Priority
?
190 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 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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.
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…
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…
Suggested Courses

824 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