Werbung: SecurityConsole.de verwaltet Ihre Computer mit Security Essentails aus der Cloud!
30 Tage kostenfrei testen und 20% Rabatt für Ihre Bestellung mit Promocode: WBF2685582
(Promocode gültig bis 31.12.2011)

Group:  English: Windows Server » microsoft.public.windows.server.scripting
Thread: Find and replace in Word

HTVi
TV Discussion Newsgroups

Find and replace in Word
Gordon 6/26/2007 4:41:05 PM
I am relatively new to scripting, but need to write a script to open a file
in Word and make some changes to it.

This code fragment shows the pertinent parts:

Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Open("d:\test.txt")

objWord.Visible = True

with objWord.Selection.Find
.ClearFormatting
.Text = "July"
.Replacement.Text = "August"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False
end With
objWord.Selection.Find.Execute Replace:=wdReplaceAll

The Text and Replacement.Text strings may change, but that is not the problem.

When I attempt to run this, I get an error message on the : in the last
line. The error is a VBScript compilation error and says "Error: Expected
statement". The find and replace part is taken directly from the Word macro
recorder, and I added the "objWord." part. I suspect I need to quote the :
somehow, but don't know how. If I change this to be a find, it works fine.

Any ideas?

RE: Find and replace in Word
culanas 7/4/2007 8:24:02 AM
Hello,
perhaps you need to open the document in Write Mode; I paste something I've
found:

Const ForReading = 1
Const ForWriting = 2

Set objFSo = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("c:\scripts\test.txt", ForWriting)

"Gordon" wrote:

[Quoted Text]
> I am relatively new to scripting, but need to write a script to open a file
> in Word and make some changes to it.
>
> This code fragment shows the pertinent parts:
>
> Set objWord = CreateObject("Word.Application")
> Set objDoc = objWord.Documents.Open("d:\test.txt")
>
> objWord.Visible = True
>
> with objWord.Selection.Find
> .ClearFormatting
> .Text = "July"
> .Replacement.Text = "August"
> .Forward = True
> .Wrap = wdFindContinue
> .Format = True
> .MatchCase = False
> .MatchWholeWord = False
> .MatchAllWordForms = False
> .MatchSoundsLike = False
> .MatchWildcards = False
> end With
> objWord.Selection.Find.Execute Replace:=wdReplaceAll
>
> The Text and Replacement.Text strings may change, but that is not the problem.
>
> When I attempt to run this, I get an error message on the : in the last
> line. The error is a VBScript compilation error and says "Error: Expected
> statement". The find and replace part is taken directly from the Word macro
> recorder, and I added the "objWord." part. I suspect I need to quote the :
> somehow, but don't know how. If I change this to be a find, it works fine.
>
> Any ideas?
>

Home | Search | Terms | Imprint Contact
Newsgroups Reader - provided by WiredBox.Net
Suche nach Orten, Städten, Postleitzahlen, Vorwahlen, Kfz-Kennzeichen