Solved

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

Posted on 2013-02-05
4
380 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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

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…
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
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 demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

770 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