Looking for a Book(s) on converting VBA to VBS

I'm been searching for a book on using VBS for excel but can't seem to find one. I'd like to find out how to convert a macro recorded in excel and then edit the VBA code to run in VBScript. Or, the perfect world would be a conversion program/application.
mike_VBSAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RobSampsonCommented:
Hi Mike, there's no books that I know of, nor have I looked for any programs, since I do mine myself, but it's a fairly straightforward process since they're very similar in syntax.

There are a few rules you can follow (that I'm made up of the top of my head):

1) Type declarations are not supported, so remove them, eg As String

2) VBA Constants are not implicit when using VBS, so you must declare them in your code.  For example, in VBA, execute MsgBox xlDelimited and you will see the value 1, so in VBS, add
Const xlDelimited = 1

3) The parameter qualifiers for functions are not supported, so remove them, eg change Workbooks.Open(Filename:="C:\MyFile.doc") to Workbooks.Open("C:\MyFile.doc")
Also with function arguments, because of this, you must follow the exact parameter order as documented for that function, and for parameters you don't want to set, just leave it blank.
For example, here is the documentation for the Open method: http://msdn.microsoft.com/en-us/library/office/bb179167(v=office.12).aspx
which shows the parameter list
expression.Open(FileName, UpdateLinks, ReadOnly, Format, Password, WriteResPassword, IgnoreReadOnlyRecommended, Origin, Delimiter, Editable, Notify, Converter, AddToMru, Local, CorruptLoad)

so, the VBS equivalent to open a file readonly (with all other parameters default) is:
objExcel.workbooks.open("C:\MyDoc.doc", , True)

where the UpdateLinks parameter is left blank to accept the default.

4) Make sure that you preceed all object statements with objExcel (or whatever your object name is for the COM object you're controlling).  For example, just using
Workbooks.Open(....
as is shown in VBA, won't work, so you have to use
objExcel.Workbooks.Open(....

Similarly, for "Application" style settings in VBA, like
Application.DisplayAlerts = False

in VBS, you use
objExcel.DisplayAlerts = False

5)  With the code of a recorded macro, there's no need to keep .Activate or .Select or .Selection.<property> statements.  These make for less efficient code, and be replaced by direct references to the range you're working with, such as
objExcel.Sheets(1).Range("A1:A10").Font.Bold = True

6) Unrelated to macro recording, you may come across some code around the place that uses the TextStream Open, Input, and Close methods, as shown here: http://vbadud.blogspot.com.au/2007/06/vba-read-text-files-with-leading.html

In VBS, you can't use that, and have to use the Scripting.FileSystemObject instead, by creating the object with Set objFSO = CreateObject("Scripting.FileSystemObject")



There's probably a couple more quirks, but they're certainly the main stumping points I can think of.

Other than that, just record your macro, and modify it as per the tips above, and it should just about work ;-)

Regards,

Rob.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
terencinoCommented:
Hi Mike, Rob has hit the main points. Opening and closing documents and spreadsheets etc will need some additional work. For example to open an Excel file, run a macro called ShowDialog, then close the file, goes like this:
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\testfile.xlsm")
objExcel.Visible = True
objExcel.Run "ShowDialog"
objExcel.Quit

Open in new window

Here is a useful script that will help convert existing VBA functions to VBS.
Convert VB6/VBA Code to VBS
0
terencinoCommented:
There are some great resources on the web for example:

activexperts.com VBScript Library
0
mike_VBSAuthor Commented:
I'll check them both out - thanks for the post!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.