Click here to Skip to main content
15,892,674 members
Articles / Programming Languages / Visual Basic

Boilerplate using Visual Studio .NET Macro

Rate me:
Please Sign up or sign in to vote.
2.71/5 (5 votes)
19 Apr 2005CPOL1 min read 33K   12  
Inserting boiler plate templates using VS.NET macro.

Introduction

A boilerplate is a kind of a template which can be used over and over again. Some kinds of boilerplates are copy right statements, mission statements, safety warnings etc.. Here a boilerplate is used to describe the file, the author, created date, purpose etc..

We can use VS.NET macros to add the boilerplate template to each page. To access the VS.NET IDE we need to reference DTE object. The DTE object represents the Visual Studio .NET IDE. It is located in a namespace called EnvDTE. The .NET assembly name for this namespace is envdte and is contained in a file called envdte.dll.

Steps to create the macro

  1. Open Visual Studio .NET.
  2. Open the Macro Explorer. Tools > Macros > Macro Explorer (or by pressing Alt+F8).

    Image 1

  3. Right-click on your MyMacros icon and select "New Module".

    Image 2

  4. Name the new module as BoilerPlate and save it.
  5. In the Macro Explorer, right-click on the new module and select "New Macro".

    Image 3

  6. Replace all the code with the code in the "Code" section below.
  7. Save the macro file.

Code

VB
'EnvDTE namespace contains the object DTE which represents the VS.NET IDE
Imports EnvDTE
'used to import StringBuilder class
Imports System.Text
Public Module BoilerPlate
    'Method for C# code
    Sub InsertBiolerPlateForCSharp()
        'Get the reference of the current active document
        Dim firstLine As TextSelection = DTE.ActiveDocument.Selection
        Dim boilerPlateText As StringBuilder = New StringBuilder
        boilerPlateText.Append("//********************" & vbNewLine)
        boilerPlateText.Append("// Module Name: " & vbNewLine)
        boilerPlateText.Append("// File Name: " & vbNewLine)
        boilerPlateText.Append("// Author: " & vbNewLine)
        boilerPlateText.Append("// Created Date: " & vbNewLine)
        boilerPlateText.Append("// Description: " & vbNewLine)
        boilerPlateText.Append("// Related pages: " & vbNewLine)
        boilerPlateText.Append("// Displayed: true/false " & vbNewLine)
        boilerPlateText.Append("// Flow when the page is submitted: " & _
                                                               vbNewLine)
        boilerPlateText.Append("// Modification History: " & vbNewLine)
        boilerPlateText.Append("// Author: " & vbNewLine)
        boilerPlateText.Append("// Modified Date: " & vbNewLine)
        boilerPlateText.Append("// Description: " & vbNewLine)
        boilerPlateText.Append("//***********************" & vbNewLine)
        'move to the begining of the file
        firstLine.StartOfDocument()
        'insert the boiler plate
        firstLine.Insert(boilerPlateText.ToString())
        'inserting a new line after the boiler plate
        firstLine.NewLine()
    End Sub
    
    'Method for VB.Net Code
    Sub InsertBiolerPlateForVB()
        Dim firstLine As TextSelection = DTE.ActiveDocument.Selection
        Dim boilerPlateText As StringBuilder = New StringBuilder
        boilerPlateText.Append("'-----------------------" & vbNewLine)
        boilerPlateText.Append("' Module Name: " & vbNewLine)
        boilerPlateText.Append("' File Name: " & vbNewLine)
        boilerPlateText.Append("' Author: " & vbNewLine)
        boilerPlateText.Append("' Created Date: " & vbNewLine)
        boilerPlateText.Append("' Description: " & vbNewLine)
        boilerPlateText.Append("' Related pages: " & vbNewLine)
        boilerPlateText.Append("' Displayed: true/false " & vbNewLine)
        boilerPlateText.Append("' Flow when the page is submitted:" & _
                                                             vbNewLine)
        boilerPlateText.Append("' Modification History: " & vbNewLine)
        boilerPlateText.Append("' Author: " & vbNewLine)
        boilerPlateText.Append("' Modified Date: " & vbNewLine)
        boilerPlateText.Append("' Description: " & vbNewLine)
        boilerPlateText.Append("'-----------------------" & vbNewLine)
        firstLine.StartOfDocument()
        firstLine.Insert(boilerPlateText.ToString())
        firstLine.NewLine()
    End Sub
End Module

How to run a macro

  1. Open a project that is already created.
  2. Open a C# file.
  3. Open the Macro Explorer (if not already opened).

    Image 4

    Now in the Macro Explorer you can see the two functions under the BoilerPlate module which is under MyMacros section.

  4. Double click or right-click and choose Run to run the macro.

Conclusion

Macros ease the life of a developer. Macros are a useful way of speeding up a developer's coding efforts. VS.NET comes with a powerful recording feature for recording macros from key strokes.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --