Solved

Replace a string in a word file

Posted on 2004-10-09
4
181 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

867 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now