| < Free Open Study > |
|
As you develop in .NET, you will most likely develop a project and modules of macros. Additionally, you will notice that Visual Studio comes with sample macro projects. You should take time to examine the numerous macro commands in the Samples set of macros. You can learn how to do things in a macro and in an add-in that otherwise might be very elusive. I certainly appreciate the code samples not only in the Samples set, but also throughout the MSDN help system.
Next, you'll learn how to manipulate code in a code window before you examine the Macros IDE. Listing 8-4 shows a macro command for commenting a selected block of Visual Basic code.
Listing 8-4: Commenting Visual Basic Code with a Macro
Sub CommentVBCode() Dim sel As TextSelection = DTE.ActiveDocument.Selection() Dim stpt As EditPoint = sel.TopPoint.CreateEditPoint() Dim endpt As TextPoint = sel.BottomPoint Try Do While (stpt.LessThan(endpt)) stpt.Insert(""') stpt.LineDown() stpt.StartOfLine() Loop Catch End Try End Sub
To run the macro, first select a block of code in the code window. Next, display the Macro Explorer by clicking its icon. Finally, right-click the CommentVBCode macro command in the Explorer. Figure 8-7 shows the selected code just before it is commented by the macro.
Figure 8-7: Code block before commenting
Next, click the Run menu option. You could double-click the command in the Macro Explorer to execute the command. Figure 8-8 shows the code block immediately after the macro has been run and has commented the code.
Figure 8-8: Code block commented by the macro command
| < Free Open Study > |
|