Solved

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

Posted on 2013-02-05
4
378 Views
Last Modified: 2013-02-06
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.
0
Comment
Question by:mike_VBS
  • 2
4 Comments
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 38858030
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
 
LVL 16

Expert Comment

by:terencino
ID: 38858068
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
 
LVL 16

Expert Comment

by:terencino
ID: 38858091
There are some great resources on the web for example:

activexperts.com VBScript Library
0
 

Author Comment

by:mike_VBS
ID: 38862286
I'll check them both out - thanks for the post!
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now