Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"Expected Statement"

Status
Not open for further replies.

kmcnamee

Programmer
Jun 12, 2002
3
SE
Hi,

I'm trying to write a VB script that will search & replace stuff in a Word doc. The entire script is between the dashed lines:

------------------------------------------
Dim args
Dim num

Set args = WScript.Arguments
num = args.Count

If num < 3 Then
WScript.Echo &quot;Usage: Replacer.vbs <filename> [<tag> <value> ... ]&quot;
WScript.Quit 1
End If

Dim i
Dim myWord

Set myWord = CreateObject(&quot;Word.Application&quot;)
With myWord
.Documents.Open args(0), , False
.Visible = True
With .Selection.Find
For i = 1 To num - 1 Step 2
.text = args(i)
.ClearFormatting
.Replacement.ClearFormatting
.Replacement.text = args(i + 1)
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
Next
End With
.Documents(1).Save
'.Quit 0
End With

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

Unfortunately I get &quot;Expected Statement&quot; Error at Line 33 Char 30, which is (marked in red):

.Execute Replace:=wdReplaceAll


I have tried different things like:

.Execute args(i), True, False, False, False, True, True, False, args(i+1), wdReplaceAll

All to no avail.

Any help appreciated,
Complete Newbie.
 
kmcnamee,

Hope this helps. This is all I have dabbled with in WORD scripting.

Set WSHShell = WScript.CreateObject(&quot;WScript.Shell&quot;)
' Get a reference to the Word Application object.
Set appWord = Wscript.CreateObject(&quot;Word.Application&quot;)
' Display the application.
appWord.Visible = TRUE ' Open a sample document.
' appWord.Documents.Add
appWord.Documents.Open(&quot;C:\Dev\Scripts\VBScript\doc1.doc&quot;)
appword.Selection.Font.Name = &quot;Arial Black&quot;
appword.Selection.Font.Size = 14
appword.Selection.TypeText &quot;Test string characters.&quot;
appword.Selection.Words(1).Text = &quot; stringer&quot;
appWord.Quit


fengshui_1998
 
Hmmm... not really. I need to use the &quot;Find&quot; feature to search and replace tags in the document.

The error I get is a compile error. Could someone test this to see if they get the same error (no need to worry about arguments) and perhaps explain to me what I'm doing wrong?

Patiently,
kmcnamee
 
Success!

After some more searching I found this link

which explains syntax of Execute on Find. The following compiles and runs correctly.

------------------------------------------
Dim args
Dim num

Set args = WScript.Arguments
num = args.Count

If num < 3 Then
WScript.Echo &quot;Usage: Replacer.vbs <filename> [<tag> <value> ... ]&quot;
WScript.Quit 1
End If

Dim i
Dim myWord

Set myWord = CreateObject(&quot;Word.Application&quot;)
With myWord
.Documents.Open args(0), , False
.Visible = True
With .Selection.Find
For i = 1 To num - 1 Step 2
.Execute args(i), 1, 0, 0, 0, 0, 0, 1, 1, args(i + 1), 2
Next
End With
.Documents(1).Save
'.Quit 0
End With
------------------------------------------

Thanks for your time,
kmcnamee.

PS. Would this make a useful FAQ?
 
THe problem was VBS does NOT support named parameters. They must be passed by their ordinal position.
Also, you can't use constants (wdReplaceAll) of the Word OM, unless you include their definition in your script.

AFA the FAQ - couldnt' hurt. This comes up every few months or so. Jon Hawkins
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top