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
"Andrew Miller" <A.J.M...@bcs.org.uk> wrote in message
news:3c3c...@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'
> 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...
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...
"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