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



The information in this article applies to:

  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)

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

  1. Start Visual Studio .NET or Visual Studio 2005.
  2. On the Tools menu, point to Macros, and then click Macros IDE.
  3. In the Macros IDE window, expand MyMacros on the left pane.
  4. Double-click Module1.
  5. 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("End ") = -1) Then
                If ((TestLine(currLine, "Sub") = True) Or (TestLine(currLine, "Function") = True) _
                     Or (TestLine(currLine, "sub") = True) Or (TestLine(currLine, "function") = True)) Then
                    ep.Insert(Microsoft.VisualBasic.Constants.vbCrLf)
                    ep.Insert(Microsoft.VisualBasic.Constants.vbTab & "' Comment is inserted on " & 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 + " ")) Then
                        Return True
                    End If
                Case Is > 0
                    If (st.Substring(st.IndexOf(keyWord), keyWord.Length + 1).EndsWith(keyWord + " ") And st.Substring(st.IndexOf(keyWord) - 1, keyWord.Length + 1).StartsWith(" " + keyWord) = True) Then
                        Return True
                    End If
                Case Else
                    Return False
            End Select
            Return False
        End Function
  6. On the File menu, click Save MyMacros.
  7. Quit the Macros IDE.
  8. Return to the Visual Studio .NET or Visual Studio 2005 IDE. On the Tools menu, click Options.
  9. On the left pane of the Options window, expand Environment, and then click Keyboard.
  10. On the right pane of the Options window, click [Default Settings] on the Keyboard mapping scheme list, and then click Save As.
  11. Type AddDefaultComment in the Save this keyboard scheme as text box, and then click OK.
  12. On the Show commands containing list, click Macros.MyMacros.Module1.InsertDocComments.
  13. In the Use new shortcut in list, click Text Editor.
  14. In the Press shortcut key(s) text box, press the ENTER key, and then click Assign.
  15. Click OK.
  16. 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.
  17. Under Project Types, click Visual Basic Projects.

    Note In Visual Studio 2005, click Visual Basic under Project Types.
  18. Under Templates, click Console Application, and then click OK.
  19. Type Sub Sub Name or type Function Function Name, 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.

Modification Type:MinorLast Reviewed:10/3/2006
Keywords:kbvs2005applies kbvs2005swept kbmacro kbAutomation kbide kbhowto KB822097 kbAudDeveloper