Solved

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

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
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…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

789 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