Creating a Macro Project


Visual Basic .NET Unleashed
By Paul Kimmel
Table of Contents
Chapter 4.  Macros and Visual Studio Extensibility

Creating a Macro Project

You can create a new macro project by selecting Tools, Macros, New Macro Project in the VS .NET IDE or by selecting New Macro Project from the Macros Explorer context menu. When you're in the Macros IDE, you cannot create a new macro project.

Follow the numbered steps and refer to the listing to create a macro that creates subfolders . (The example creates a subfolder for managing sample projects for this book.)

  1. In the VS .NET IDE, choose Tools, Macros, New Macro Project.

  2. Browse to the location where you would like to store the macro and type MakeDirectory for the Name of the macro project.

  3. By default the module will be named Module1. Rename the module to Directory.

  4. Double-click on the Directory module to open the Macros IDE with focus set on this new module.

  5. Add the code shown in Listing 4.5 to complete the macro.

Listing 4.5 A utility macro that creates project folders for new chapter examples
  1:  Option Strict On  2:  Option Explicit On  3:  Imports EnvDTE  4:  Imports System.IO  5:   6:  Public Module Directory  7:   8:  Private Function GetSourceDirectory() As String  9:  Return _  10:  "C:\ Books\ Sams\ Visual Basic .NET Unleashed\ Source\ "  11:  End Function  12:   13:  Private Sub GetPath(ByRef Directory As String)  14:  Directory = _  15:  InputBox("Enter subfolder name:", _  16:  "Create Sub-Folder", Directory)  17:  End Sub  18:   19:  Private Function GetFullPath(_  20:  ByVal Directory As String) As String  21:   22:  If (Directory = "") Then GetPath(Directory)  23:  Return GetSourceDirectory() & Directory  24:  End Function  25:   26:  Private Sub DoMakeChapterDirectory(_  27:  ByVal Path As String)  28:  Try  29:  MkDir(Path)  30:  Catch  31:  MsgBox(Path & " alread exists", _  32:  MsgBoxStyle.Exclamation)  33:  End Try  34:  End Sub  35:   36:  Public Sub MakeChapterDirectory(_  37:  Optional ByVal Directory As String = "")  38:  DoMakeChapterDirectory(GetFullPath(Directory))  39:  End Sub  40:   41:  End Module 

The sample listing employs Option Strict On and Option Explicit On settings for the same reason as we use them for applications.

The listing also imports System.IO, demonstrating the use of other CLR namespaces within a macro. The query function GetSourceDirectory is a hard-coded method rather than a literal or string by choice. GetPath prompts the user for an input path. The motivation for this decision is that you cannot pass arguments to macros. Macros either have to be parameterless subroutines or all parameters have to have optional parameters with default values. In the sample listing, the macro is MakeChapterDirectory with an Optional parameter, Directory, with a default value of "". If the code is used in some other context, we can pass an argument to the macro procedure.

GetPath prompts the user for a subfolder and concatenates the subfolder to the book's path. DoMakeDirectory attempts to create the subfolder. If the subfolder already exists, the Catch block beginning on line 30 handles the error by telling the operator that the subfolder already exists.


Visual BasicR. NET Unleashed
Visual BasicR. NET Unleashed
Year: 2001
Pages: 222 © 2008-2017.
If you may any questions please contact us: