Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Boilerplate using Visual Studio .NET Macro

0.00/5 (No votes)
19 Apr 2005 1  
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).

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

  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".

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

Code

'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).

    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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here