VBA translated to VBScript

F

Fred Goldman

Something that should be so simple and it's driving me nuts! I can't find any
kind of documentation.

I have this VBA code that simply adds two character styles and then does two
replacements. I need to get this into VBScript syntax and on every line I am
getting errors, or it is simply doing nothing. Does anyone know how to
translate this, I would be very thankful.


Set Word = CreateObject("Word.Application")
Word.Visible = True
Word.Documents.Open ("c:\Siman35.doc")
On Error Resume Next
Word.ActiveDocument.Styles.Add "Hebrew", Type:=wdStyleTypeCharacter
On Error Resume Next
Word.ActiveDocument.Styles.Add Name = "Hebrew Bold",
Type:=wdStyleTypeCharacter
Word.Selection.Find.ClearFormatting
Word.Selection.Find.Replacement.ClearFormatting
Word.Selection.Find.Format = True
Word.Selection.Find.Font.Bold = True
Word.Selection.Find.Wrap = wdFindContinue
Word.Selection.Find.Format = True
Word.Selection.Find.Replacement.Style = ActiveDocument.Styles("Hebrew
Bold")
Word.Selection.Find.Execute Replace:=wdReplaceAll
Word.Selection.Find.ClearFormatting
Word.Selection.Find.Replacement.ClearFormatting
Word.Selection.Find.Font.Bold = False
Word.Selection.Find.Forward = True
Word.Selection.Find.Wrap = wdFindContinue
Word.Selection.Find.Format = True
Word.Selection.Find.LanguageID = wdHebrew
Word.Selection.Find.Replacement.Style = ActiveDocument.Styles("Hebrew")
Word.Selection.Find.Execute Replace:=wdReplaceAll
 
S

Steve Yandl

Fred,

One thing you need to change is the constants. Running VBA in Word, there
is no problem using a constant like 'wdStyleTypeCharacter' but that isn't
the case in vbScript. You could either have a line like
Const wdStyleTypeCharacter = 2
at the top of your vbs file, or you could simply change the line
Word.ActiveDocument.Styles.Add "Hebrew", Type:=wdStyleTypeCharacter
to be
Word.ActiveDocument.Styles.Add "Hebrew", 2

Also, you can't use
Type:=
or
Replace:=
etc.
Instead, list all required arguments separated by commas and simply don't
type anything between commas for those arguments not needed for your
scenario.

I didn't take the time to create a script and test it so there may be other
issues but I think those changes might take care of it for you. You can
find the numeric values for all the other Word VBA constants using Help
within your VBE window.


Steve Yandl
 
D

Doug Robbins - Word MVP

I suspect that the problem might be with your use of the word "Word". Try
replacing it with objWord or something similar so that the program does not
get confused.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
F

Fred Goldman

Oh, I can't thank you enough. That was the key, to get the numeric value of
those constants. Thank you!

I don't know if naming the object Word was a problem, but I changed it to
objWord just incase.
 
S

Steve Yandl

Fred,

You're welcome.

From vbScript, using Word instead of objWord probably wasn't a problem but
if you ever expand the script it's much easier to follow what you're doing
if you use objWord to remind yourself that you're working with the Word
application object.

For future scripts, you are allowed to use the 'With.....End With' structure
in a vbs file to save typing. Of course, if you've recorded a macro and
used copy and paste, it doesn't hurt to leave all the extra typing in your
code.

Also, if you do other scripts working with Word or other Office
applications, you can find a fairly good set of examples with good
explanations at the Microsoft technet script center.
http://www.microsoft.com/technet/scriptcenter/resources/qanda/office.mspx


Steve Yandl
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top