Click here to Skip to main content
Licence CPOL
First Posted 15 Jul 2004
Views 29,010
Bookmarked 20 times

Creation and Update Comment Stamps Macro

By | 15 Jul 2004 | Article
This is a Visual Studio .NET 2003 macro, it can automatically insert comment blocks to your source files about copyright, creation date, author and update, etc., basically anything you want for your source files’ header.

Introduction

This is a Visual Studio .NET 2003 macro, it can automatically insert comment blocks to your source files about copyright, creation date, author and update, etc., basically anything you want for your source files’ header.

Here are the codes:

Imports EnvDTE
Imports System.Diagnostics
Public Module Stamps
    'DESCRIPTION: MQZ. Creates comment block 
    Dim g_strAuthor As String = "Modesty Zhang"
    Dim g_strAuthorEmail As String = "<a href="mailto:ModestyZ@hotmail.com">ModestyZ@hotmail.com</a>"
    Dim g_strCompany As String = _
       "COMPANY, Inc. Copyright © 2004-2006, All rights reserved."
    Dim g_strTeam As String = "Modesty Team"
    Dim g_stampMark As String = _
       "/////////////////////////////////////////" & _
       "////////////////////////////////////"
    Dim g_createdBy As String = "// Created By: "
    Dim g_updatedOn As String = "// Updated On: "
 
 
    Sub StampHeaderCreation()
        'DESCRIPTION: Creates a creation comment block 
        'for the active Source files
        If (StampCheckDoc() = False) Then
            Exit Sub
        End If
 
        Dim doc As Document = DTE.ActiveDocument
        Dim ts As TextSelection = DTE.ActiveWindow.Selection
 
        ts.SelectAll()
        ts.StartOfDocument(True)
 
        If Not ts.FindText(g_createdBy) Then
            PrintText(g_stampMark, True)
            PrintText("// " & g_strCompany, True)
            PrintText("// ", True)
            PrintText("// " & doc.Name & " - " & g_strTeam, True)
            PrintText("//", True)
            PrintText("// Description:", True)
            PrintText("//      [TODO: Write the purpose of "_ 
                                      & doc.Name & ".]", True)
            PrintText("//", True)
            PrintText("// Created On: " & CDate(Now) & "", True)
            PrintText(g_createdBy & g_strAuthor & _
                 " <mailto:" & g_strAuthorEmail & "> ", True)
            PrintText(g_stampMark, True)
        Else
            StampHeaderUpdate()
        End If
 
    End Sub
    Sub StampHeaderUpdate()
        'DESCRIPTION: Creates a update comment block 
        'for the active Source files
        If (StampCheckDoc() = False) Then
            Exit Sub
        End If
 
        Dim doc As Document = DTE.ActiveDocument
        Dim ts As TextSelection = DTE.ActiveWindow.Selection
        Dim foundMark As Boolean = False
 
        ts.SelectAll()
        ts.StartOfDocument(True)
 
        While ts.FindText(g_createdBy)
            ts.FindText(g_stampMark)
            foundMark = True
        End While
 
        While ts.FindText(g_updatedOn)
            ts.FindText(g_stampMark)
            foundMark = True
        End While
 
        If foundMark Then
            'ts.LineDown(False, 1)
            ts.EndOfLine()
            ts.NewLine()
        End If
        PrintText("// Updated On: " & CDate(Now) & ". By: " & _
            g_strAuthor & " <mailto:" & g_strAuthorEmail & _
            ">", True)
        PrintText("//      [TODO: Write the purpose of update on " _
                                           & CDate(Now) & ".]", True)
        PrintText(g_stampMark, True)
 
    End Sub
 
    Function StampCheckDoc() As Boolean
        'DESCRIPTION: Creates a update comment block 
        'for the active Source files
        Dim doc As Document = DTE.ActiveDocument
        Dim badFile As Boolean = True
        Dim name As String
 
        If doc Is Nothing Then
            MsgBox("Please run when a text editor window is active.")
            Return False
        End If
 
        name = doc.Name.ToLower
        If name.EndsWith(".h") Or name.EndsWith(".cpp") _
                              Or name.EndsWith(".js") Then
            badFile = False
        End If
 
        If badFile Then
            MsgBox("Please run with a c/c++ or JavaScript file.")
            Return False
        End If
 
        Return True
    End Function
 
    Function PrintText(ByVal s As String, ByVal newline As Boolean)
        Dim ts As TextSelection = DTE.ActiveWindow.Selection
        ts.Text = s
        If newline Then
            ts.NewLine()
        End If
    End Function
 
End Module

Before you use it, you may want to customize the strings in the beginning, like g_strAuthor, g_strAuthorEmail, g_strCompany, g_strTeam, etc. The rest of the strings are some markers that the code use to align inserted content.

If you create a file and it’s currently active in VS.NET 2003 IDE, running StampHeaderCreation() will create a header about copyright, creation date, and author info block at the beginning of your file. If you run StampHeaderCreation() again, it will automatically insert “update” comment block by calling StampHeaderUpdate().

Of course, StampHeaderUpdate() should be used when you edit a file created by somebody else, it will not add creation block with author info. Each time it’s invoked, a new “update” comment block is inserted.

The above code has two helper functions: StampCheckDoc() will make sure the IDE has a document open and the document type is correct, you can certainly expand it to include .cs or any other type of files you see fit. Another helper function is PrintText(), it doesn’t need any explanation though…

If you have any questions, please send me an email to ModestyZ@hotmail.com.

License

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

About the Author

Modesty Zhang

Software Developer (Senior)

United States United States

Member

Specialties:
RIA / Ajax, HTML5, CSS3, jQuery, JavaScript / HTML, CSS, Cross browser and cross platform applications /
Flex / Flash / Silverlight / Software Architecting / Front End Design and Development /Windows /.NET /C# /WPF /XAML

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralIt is OK. Pinmembernwlf20:59 16 Nov '06  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 16 Jul 2004
Article Copyright 2004 by Modesty Zhang
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid