Microsoft KB Archive/822097

= How to write a macro in Visual Basic .NET or in Visual Basic 2005 that automatically inserts comment text =

Article ID: 822097

Article Last Modified on 11/26/2007

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-





SUMMARY
This step-by-step article describes how to write a macro in Microsoft Visual Basic .NET or in Microsoft Visual Basic 2005 that automatically inserts comment text in functions and in subroutines when you create functions and you create subroutines. The example in this article describes how to insert a comment when you write the function signature and you write the subroutine signature, and then you press the ENTER key.



MORE INFORMATION
A macro is a set of instructions that you save to a file. You can use a macro whenever you require it.

To manually create a macro, type the code in the Macros IDE. You can also use the Macros IDE to record a macro.

After you create a macro, you can run the macro from the Microsoft Visual Studio .NET or Microsoft Visual Studio 2005 Macro Explorer, or you can run the macro by using the keyboard shortcut.

The example in this article uses the following elements to describe how to insert comments:
 * Development Tools Extensibility (DTE) object: This object is the Application object in Microsoft Visual Basic for Applications. The Application object is a top-level object in the Microsoft Visual Studio automation object model.
 * ActiveDocument property: This property returns an active document that has focus.
 * EditPoint object: This object is similar to a TextSelect object. You can use the EditPoint object to handle the text as data in the buffer.

Step-By-Step Example
 Start Visual Studio .NET or Visual Studio 2005. On the Tools menu, point to Macros, and then click Macros IDE. In the Macros IDE window, expand MyMacros on the left pane. Double-click Module1.  On the right pane, add the following code before End Module: '  InsertDocComments goes through the current document by using the Visual Studio .NET code model '  to add documentation style comments to each function. Sub InsertDocComments Dim ep As EditPoint = DTE.ActiveDocument.Selection.ActivePoint.CreateEditPoint Dim currLine As String currLine = ep.GetLines(ep.Line, ep.Line + 1) If (currLine.IndexOf(&quot;End &quot;) = -1) Then If ((TestLine(currLine, &quot;Sub&quot;) = True) Or (TestLine(currLine, &quot;Function&quot;) = True) _                Or (TestLine(currLine, &quot;sub&quot;) = True) Or (TestLine(currLine, &quot;function&quot;) = True)) Then ep.Insert(Microsoft.VisualBasic.Constants.vbCrLf) ep.Insert(Microsoft.VisualBasic.Constants.vbTab & &quot;' Comment is inserted on &quot; & System.DateTime.Now.ToString) End If       End If        ep.Insert(Microsoft.VisualBasic.Constants.vbNewLine) End Sub

'  This function checks for a subroutine keyword or a function keyword that is in the specified string.

Function TestLine(ByVal st As String, ByVal keyWord As String) As Boolean Select Case (st.IndexOf(keyWord)) Case -1 Return False Case 0 If (st.Substring(st.IndexOf(keyWord), keyWord.Length + 1).EndsWith(keyWord + &quot; &quot;)) Then Return True End If           Case Is > 0 If (st.Substring(st.IndexOf(keyWord), keyWord.Length + 1).EndsWith(keyWord + &quot; &quot;) And st.Substring(st.IndexOf(keyWord) - 1, keyWord.Length + 1).StartsWith(&quot; &quot; + keyWord) = True) Then Return True End If           Case Else Return False End Select Return False End Function  On the File menu, click Save MyMacros. Quit the Macros IDE.</li> Return to the Visual Studio .NET or Visual Studio 2005 IDE. On the Tools menu, click Options.</li> On the left pane of the Options window, expand Environment, and then click Keyboard.</li> On the right pane of the Options window, click [Default Settings] on the Keyboard mapping scheme list, and then click Save As.</li> Type AddDefaultComment in the Save this keyboard scheme as text box, and then click OK.</li> On the Show commands containing list, click Macros.MyMacros.Module1.InsertDocComments.</li> In the Use new shortcut in list, click Text Editor.</li> In the Press shortcut key(s) text box, press the ENTER key, and then click Assign.</li> Click OK.</li> To test this macro, point to New on the File menu of the Visual Studio .NET or Visual Studio 2005 IDE, and then click Project.</li> Under Project Types, click Visual Basic Projects.

Note In Visual Studio 2005, click Visual Basic under Project Types.</li> Under Templates, click Console Application, and then click OK.</li> Type Sub or type Function , and then press the ENTER key.

A comment is added after the subroutine header or after the function header.

Note With this macro, you may receive a comment for the existing functions or for the existing subroutines when you press the ENTER key. The functionality of this macro overrides the existing functionality of the ENTER key. You must remove the keyboard mapping for the ENTER key while you are using Visual Studio .NET for programming languages other than Visual Basic .NET, such as Microsoft Visual C# .NET and Microsoft Visual J# .NET or you are using Visual Studio 2005 for programming languages other than Visual Basic 2005, such as Microsoft Visual C# 2005 and Microsoft Visual J# 2005.</li></ol>

<div class="references_section">