Solved

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

Posted on 2013-02-05
4
384 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

623 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