Exploring VBA


Visual Basic for Applications (VBA), and its subset version VBScript, is Microsoft's technology for writing your own programs, both those that run in other programs (scripts) and those that run by themselves (custom applications). InDesign works with both VBA and VBScript. The Visual Basic language that underlies both VBA and VBScript is not meant for everyday computer users ‚ a knowledge of programming is very useful in taking advantage of this technology. Although based on the Basic language developed in the 1970s to help new users write their own programs, it has evolved a lot since then and is no longer so simple.

Learning the language

Many of the actions specified in VBA have some degree of "Englishness," such as:

 set myTextFrame =  InDesign.Documents.Item(1).Spreads.Item(1).TextFrames.Add 

or

 mySelection.RotationAngle = 30 

But as you can see, VBA has moved far from English. The first code segment, for example, means to add a text frame to the first spread in the first document. The second means to rotate the selected object by 30 degrees.

Getting more information on VBA

Before you venture too far into scripting, you should review the VBA- related information provided by Microsoft and with InDesign:

  • Microsoft scripting documentation and tools. Microsoft has a lot of information on VBA, Visual Basic, and VBScript on its Web site. Unfortunately, it's not well organized and is hard to find and understand. There's no tutorial that simply explains how a new scripter needs to get started. However, you can search on the Microsoft site for VBA, Visual Basic, and VBScript to get links to documents that may prove useful.

  • InDesign scripting documentation. The InDesign CD contains a 600-plus-page PDF file that explains VBA programming for InDesign. This document, although a bit on the technical side, is a valuable resource. It includes an overview of VBA scripting and the object model, as well as a list of InDesign-specific scripting terms and scripting examples.

If you want still more information about VBA and its two "sister" technologies, several books are available, including Programming Microsoft Visual Basic 6.0 , by Francesco Balena; Visual Basic 6 For Dummies , by Wallace Wang; and VBScript in a Nutshell by Paul Lomax and Ron Petrusha.

 

What you need to write and run scripts

To use InDesign scripting in Windows, you'll need Microsoft Visual Basic or an application that contains Visual Basic for Applications (VBA); these include Microsoft Office, Microsoft Visio, and AutoCAD. In Microsoft Office, you can run the Microsoft Script Editor by choosing Tools Macros Microsoft Script Editor, which lets you create scripts, edit them, test your code, and fix errors. Figure 37-3 shows the editor with a sample script.


Figure 37-3: The Microsoft Script Editor window containing sample VBA text. When you work on a script, the Microsoft Script Editor applies indents automatically.

You can also write scripts in VBScript, a VBA subset, in a text editor such as WordPad. You'll need Microsoft's free Windows Scripting Host ( WSCRIPT.EXE ), which is usually installed with Windows and can be downloaded from Microsoft's Web site.

There's a third choice for your scriptwriting : You can also use the full Microsoft Visual Basic product from Microsoft.

Note ‚  

To use InDesign scripting in Windows, your user profile must have Administrator privileges.

Running your script

To run a VBA, Visual Basic, or VBScript program, simply double-click the script. You can also run the script directly from the application that you create a VBA or Visual Basic script in, such as the Microsoft Script Editor. (For VBScripts, you can run them from the Scripting Host application.) If you've done everything correctly, you'll see InDesign become the active program, and then the actions you put in your script will take place. Voil ƒ   ‚ and congratulations! You can now call yourself a scripter without blushing. That's all there is to creating and running a script.

Saving your script

When you're finished writing and testing a script, choose Save from the script editor's File menu. Name your script and choose its storage location. It's best to save the script in the Scripts folder inside the InDesign folder (usually C:\Program Files\Adobe\InDesign CS ), so it will show up in the Scripts menu (after you restart InDesign).




Adobe InDesign CS Bible
Adobe InDesign CS3 Bible
ISBN: 0470119381
EAN: 2147483647
Year: 2003
Pages: 344
Authors: Galen Gruman

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net