Visual C++ 6 Unleashed

Visual C++ 6 Unleashed

By MICKEY WILLIAMS and David Bennett

Using VBScript to Write Developer Studio Macros

VBScript is an interpreted scripting language similar to Visual Basic. Like Visual Basic, it has loose rules about declaring subroutines and variable names. Unlike C++, it is not compiled, which means that each line is executed with the help of an interpreter.

VBScript macros are not stored as part of a Developer Studio workspace or project. When you write a VBScript macro, it is stored in a macro file with a DSM extension.

Declaring Variables

VBScript does not automatically require you to declare variables. This opens up a hole that makes it very easy to create new variables simply by misspelling an existing variable, thus introducing a bug that can be very difficult to fix.

You can change VBScript's default behavior so that it requires variables to be defined by changing the declaration mode to explicit. Insert this line at the top of your VBScript source file:

option explicit

It's a very good idea to use explicit declarations, because it will prevent a common source of bugs in scripting languages.

To declare a variable, use the dim keyword:

dim strName

Unlike variable declarations in C++, you don't specify the type of the variable—just its name. All VBScript variables have the VARIANT type.

VBScript Subroutines

The VBScript equivalent of a C++ function is a subroutine. A subroutine always begins with the sub keyword, followed by the name of the subroutine and a pair of parentheses:

sub PrintName()

Any formal parameters used with the function are included within the parentheses, much as they are in a C++ function:

sub PrintNameAndAge(strName, ByVal nAge, ByRef strErr)

There are two differences in the way that formal parameters are declared for a VBScript subroutine when compared to a C++ function:

Prefixing a parameter with ByVal indicates that the parameter is passed by value, just like in C++. If ByRef is used, the parameter is passed by reference, which is the same thing as using a C++ reference parameter. If no prefix is used, ByVal is assumed as the default.

Following the declaration of the subroutine is the body of the function, which consists of zero or more statements. For example, the following subroutine displays a message box:

sub dispMsg()
    MsgBox("Hello")
end sub

The end of the subroutine is marked by an end sub statement:

end sub

When this line is encountered, the subroutine is completed, and flow of control returns to the caller.

You can exit a subroutine from inside the subroutine body with the exit sub statement:

exit sub

When exit sub is encountered, the subroutine is exited, and the flow of control is returned to the caller.

Using Functions in VBScript

VBScript also includes functions that are exactly like subroutines, except that they return a value to the caller. A function looks very much like a subroutine, as Listing 17.1 shows.

Example 17.1. A VBScript Function That Returns an Age

function GetAge(strName)
    if strName = "Zaphod" then
        GetAge = 42
    elseif strName = "Ford" then
        GetAge = 420
    end if
end function
						

Creating a VBScript Macro

There are two steps to creating a VBScript macro. First create a new macro file, and then add a macro to it. To create a new macro file, follow these steps:

  1. Choose Macros from the Tools menu. The Macros dialog box appears.
  2. Click the Options button to expand the Macros dialog box.
  3. Click the New File button. The New Macro File dialog box appears.
  4. Enter a name and description for the new macro file. For this example, enter Unleashed as the macro file name.
  5. Click OK.

To add a macro to the Unleashed macro file, click the Edit button, and enter a description for the new macro. For this example, enter numberLines as the macro name, and click OK. Developer Studio opens the macro file and creates a skeleton of the new macro for you. Edit the macro file so that it looks like the source code shown in Listing 17.2. The accompanying CD-ROM also contains the UNLEASHED.DSM macro.

Example 17.2. A Macro That Adds Line Numbers to a Source File

option explicit
dim dsSelect
sub SelectNextLine()
    'Selecting a line selects the entire line - including the
    'carriage return. This can lead to skipping one line, so
    'this function moves down one line, then up one line, to
    'prevent this problem from occurring.
    ActiveDocument.Selection.SelectLine
    ActiveDocument.Selection.LineDown
    ActiveDocument.Selection.LineUp
    ActiveDocument.Selection.StartOfLine
end sub

sub numberLines()
    'Line numbers that contain absolute line numbers. For example
    'line 25 in the file is always line 25, even if a subset of
    'the file is selected.
    dim lineFileCurrent 'The current file's line number
    dim lineFileEnd     'The last line in the file to be written
    'Line numbers that are relative to the selected text. For
    'example, line 25 might be referred to by a different number
    'if a subset of the file is selected.
    dim lineFileWrite   'The current line number to be written
    dim CurrText

    if ActiveDocument.type <> "Text" Then
        MsgBox("The active file is not a source file.")
    else
        lineFileWrite = 1
        lineFileEnd = ActiveDocument.Selection.BottomLine
        lineFileCurrent = ActiveDocument.Selection.TopLine

        ActiveDocument.Selection.GoToLine lineFileCurrent, dsSelect
        do
            CurrText = ActiveDocument.Selection
            ActiveDocument.Selection = cstr(lineFileWrite) + ": " + CurrText
            lineFileWrite = lineFileWrite + 1
            lineFileCurrent = lineFileCurrent + 1
            if lineFileCurrent > lineFileEnd then
                exit do
            end if
            SelectNextLine()
        loop

    end if

end sub
						

To test the numberLines macro, open a source file and select a box of text such as a function. Next, open the Macros dialog box and double-click on the numberLines macro in the list box. Every line in the block of selected text will be prefixed with a line number, like this:

1: int getAnswer()
2: {
3:     doSomethingForALongTime();
4:     return 42;
5: }
						

Removing a Developer Studio Macro File

There is no obvious way to remove a macro file once it has been loaded into Developer Studio. You can enable or disable the macro through the Macro dialog box, but if you really want to remove the macro, you must edit the Registry.

You should attempt the following steps only if you are comfortable editing Registry entries. You can easily render your machine inoperable if you make a mistake in the following steps. You always should back up your Registry before modifying it in any way.

Macro filenames are stored at

HKEY_CURRENT_USER/Software/Microsoft/DevStudio/6.0/Macros

Developer Studio queries the value of this key to determine which macro files to load. By removing value entries from this key, you can trim the number of macro files loaded by Developer Studio.

Example Macros Included with Visual C++

Visual C++ includes sample macros that illustrate how you can interact with the Developer Studio object model. The sample macros include the following:

The sample macro file is named SAMPLE.DSM and is located in the Common\MSDev98\Macros subdirectory under the Visual Studio installation directory.

The sample macros are not loaded by default—you must load the macro file manually at least once. To load the sample macro file for the first time, follow these steps:

  1. Choose Customize from the Developer Studio Tools menu. The Customize dialog box appears.
  2. Select the Add-ins and Macro Files tab. One of the items displayed on this property page is a list of the currently loaded macro files.
  3. Click the Browse button; a standard File Open common dialog box appears. Navigate to the Common\MSDev98\Macros subdirectory and select the SAMPLES.DSM macro file. If this file is already loaded, you only need to check the associated checkbox to enable the macros.

After the macro file is loaded, you can run individual macros by choosing Macros from the Tools menu. You also can add macros to the toolbar for easy access, as described later in the section "Adding a VBScript Macro to the Toolbar."

Debugging a Visual Studio Macro

There are two ways to debug Visual Studio macros:

Neither of these two options are as sophisticated as the debugger included with Visual Studio. Because macros are generally fairly small, they usually require very little debugging.

Adding a VBScript Macro to the Toolbar

To add a VBScript macro to the toolbar, follow these steps:

  1. Load the macro file, if it isn't currently loaded.
  2. Choose Customize from the Tools menu.
  3. Select the Commands tab, and select Macros from the Categories drop-down list. A list box of all the macros available for use appears.
  4. Drag and drop the macro name to the toolbar that will contain the macro's button. The Button Appearance dialog box appears so that you can select the text or image that is displayed for the macro.
  5. Select an image for the macro and click OK.

To remove a toolbar item, open the Customize dialog box and drag the item from the toolbar.

Share ThisShare This

Informit Network