- Table of Contents
- Copyright
- About the Authors
- About the Contributors
- Acknowledgments
- Tell Us What You Think!
- Introduction
- How to Use This Book
- What You Need to Use This Book
- What's New in Visual C++ 6.0
- Contacting the Main Author
- Part I: Introduction
- Chapter 1. The Visual C++ 6.0 Environment
- Part II: MFC Programming
- Chapter 2. MFC Class Library Overview
- Chapter 3. MFC Message Handling Mechanism
- Chapter 4. The Document View Architecture
- Chapter 5. Creating and Using Dialog Boxes
- Chapter 6. Working with Device Contexts and GDI Objects
- Chapter 7. Creating and Using Property Sheets
- Chapter 8. Working with the File System
- Chapter 9. Using Serialization with File and Archive Objects
- Part III: Internet Programming with MFC
- Chapter 10. MFC and the Internet Server API (ISAPI)
- Chapter 11. The WinInet API
- Chapter 12. MFC HTML Support
- Part IV: Advanced Programming Topics
- Chapter 13. Using the Standard C++ Library
- Chapter 14. Error Detection and Exception Handling Techniques
- Chapter 15. Debugging and Profiling Strategies
- Chapter 16. Multithreading
- Chapter 17. Using Scripting and Other Tools to Automate the Visual C++ IDE
- The Developer Studio Object Model
- Using VBScript to Write Developer Studio Macros
- Writing Developer Studio Add-Ins
- The SourceInfo Add-In
- Summary
- Part V: Database Programming
- Chapter 18. Creating Custom AppWizards
- Chapter 19. Database Overview
- Chapter 20. ODBC Programming
- Chapter 21. MFC Database Classes
- Chapter 22. Using OLE DB
- Chapter 23. Programming with ADO
- Part VI: MFC Support for COM and ActiveX
- Chapter 24. Overview of COM and Active Technologies
- Chapter 25. Active Documents
- Chapter 26. Active Containers
- Chapter 27. Active Servers
- Chapter 28. ActiveX Controls
- Part VII: Using the Active Template Library
- Chapter 29. ATL Architecture
- Chapter 30. Creating COM Objects Using ATL
- Chapter 31. Creating ActiveX Controls Using ATL
- Chapter 32. Using ATL to Create MTS and COM+ Components
- Part VIII: Finishing Touches
- Chapter 33. Adding Windows Help
- Part IX: Appendix
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:
- The parameter name doesn't include a type.
- The parameter name sometimes is preceded by the ByVal or ByRef keyword.
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:
- Choose Macros from the Tools menu. The Macros dialog box appears.
- Click the Options button to expand the Macros dialog box.
- Click the New File button. The New Macro File dialog box appears.
- Enter a name and description for the new macro file. For this example, enter Unleashed as the macro file name.
- 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:
- CloseExceptActive closes all of the open editor windows except the currently active window.
- CommentOut can be used to comment out a block of text.
- ToggleCommentStyle is used to swap comment styles between /* and // formats.
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:
- Choose Customize from the Developer Studio Tools menu. The Customize dialog box appears.
- 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.
- 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:
- Add output statements to your macro so that your macro can print out information as it executes.
- Add message boxes to your macro so that execution stops while a message is displayed.
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:
- Load the macro file, if it isn't currently loaded.
- Choose Customize from the Tools menu.
- Select the Commands tab, and select Macros from the Categories drop-down list. A list box of all the macros available for use appears.
- 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.
- 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.
Writing Developer Studio Add-Ins | Next Section

Account Sign In
View your cart