Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Replacing text in Word from VBScript

658 views
Skip to first unread message

Andrew Miller

unread,
Jan 9, 2002, 9:09:56 AM1/9/02
to
*** post for FREE via your newsreader at post.newsfeed.com ***

I need to be able to Highlight text in a word document from VBScript.

I created the following macro in Word, which highlights all instances of the
word 'report' using the replace command:

----------------------------------------

Sub HighlightAll()
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Replacement.Highlight = True
.Text = "report"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With
End Sub

----------------------------------------

I then used this macro to create the following VBScript file, which creates
a test word document and calls a function which performs the Highlight:

----------------------------------------

Function HighlightAll (Word, Text)
With Word.Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Replacement.Highlight = True
.Text = Text
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll ' Script fails on this line
End With

Set Word = nothing
Set Doc = nothing
End Function

Dim W, Doc, ST

Set W = CreateObject ("Word.Application")
Set Doc = W.Documents.Add
W.Visible = true
Doc.Content.InsertAfter "Test Report"
ST = "report"

HighlightAll W, ST

----------------------------------------

The script fails on the .Execute command with the error message 800A0400
Expected statement. Is there any way to fix this problem?

Thanks,

Andrew


-----= Posted via Newsfeed.Com, Uncensored Usenet News =-----
http://www.newsfeed.com - The #1 Newsgroup Service in the World!
-----== 90,000 Groups! - 17 Servers! - Unlimited Download! =-----

Mike Musterd

unread,
Jan 9, 2002, 9:29:36 AM1/9/02
to
wdReplaceAll is a constant that is not known to WSH.
Try substituting with the actual value of the constant. (it is 2)
Values such as this can be found with the object browser within VBA editor.

Mike

"Andrew Miller" <A.J.M...@bcs.org.uk> wrote in message
news:3c3c...@post.newsfeed.com...

Andrew Miller

unread,
Jan 9, 2002, 9:47:29 AM1/9/02
to
*** post for FREE via your newsreader at post.newsfeed.com ***

I changed the line

.Execute Replace:=wdReplaceAll

to

.Execute Replace:=2

but it still produces the error 800A0400 Expected statement

If i remove the colon from the line I get the error 800A01C2 Wrong number of
arguments or invalid property assignment: 'Replace'

Astrid

unread,
Jan 9, 2002, 4:44:31 PM1/9/02
to
Hi Andrew,

> With Word.Selection.Find

The selection isn't an object that belongs to the Word application itself.
It's part of the Word.ActiveDocument
Try adding a range variable (set oRange = oDoc.Content) and do the find and
replace actions on the objectvariable oRange.

Hope this helps,
regards,
Astrid

So that all can benefit from the discussion, please post all follow-ups to
the newsgroup.
Visit the MVP Word FAQ site at http://www.mvps.org/word/

"Andrew Miller" <A.J.M...@bcs.org.uk> schreef in bericht
news:3c3c...@post.newsfeed.com...

Michael Harris (MVP)

unread,
Jan 9, 2002, 9:45:50 PM1/9/02
to
VBScript doesn't support named arguments. You need to look up the Execute
method in the docs and code the argument by position, omitting optional
arguments that proceed it but accounted for with commas...

Function Execute([FindText], [MatchCase], [MatchWholeWord],
[MatchWildcards], [MatchSoundsLike], [MatchAllWordForms], [Forward], [Wrap],
[Format], [ReplaceWith], [Replace], [MatchKashida], [MatchDiacritics],
[MatchAlefHamza], [MatchControl]) As Boolean

which translates to

.Execute ,,,,,,,,,,2

--
Michael Harris
Microsoft.MVP.Scripting
--


"Andrew Miller" <A.J.M...@bcs.org.uk> wrote in message
news:3c3c...@post.newsfeed.com...

Andrew Miller

unread,
Jan 10, 2002, 5:54:48 AM1/10/02
to
*** post for FREE via your newsreader at post.newsfeed.com ***


"Michael Harris (MVP)" <mik...@mvps.org> wrote in message
news:eLJwqDYmBHA.1936@tkmsftngp04...


> VBScript doesn't support named arguments. You need to look up the Execute
> method in the docs and code the argument by position, omitting optional
> arguments that proceed it but accounted for with commas...
>
> Function Execute([FindText], [MatchCase], [MatchWholeWord],
> [MatchWildcards], [MatchSoundsLike], [MatchAllWordForms], [Forward],
[Wrap],
> [Format], [ReplaceWith], [Replace], [MatchKashida], [MatchDiacritics],
> [MatchAlefHamza], [MatchControl]) As Boolean
>
> which translates to
>
> .Execute ,,,,,,,,,,2
>
> --
> Michael Harris
> Microsoft.MVP.Scripting

Thanks that works fine now

0 new messages