Boilerplate using Visual Studio .NET Macro
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
- Open Visual Studio .NET.
- Open the Macro Explorer. Tools > Macros > Macro Explorer (or by pressing Alt+F8).
- Right-click on your MyMacros icon and select "New Module".
- Name the new module as
BoilerPlate
and save it. - In the Macro Explorer, right-click on the new module and select "New Macro".
- Replace all the code with the code in the "Code" section below.
- 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
- Open a project that is already created.
- Open a C# file.
- 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. - 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.