インクルードガードをマクロで

VBは補完が効いて簡単ですね。C#もそうなんだろうな。

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics

Public Module IncludeGuard

    Sub IncludeGuard()
        Dim id As String
        id = InputBox("Include Guardに用いる文字列")
        If String.IsNullOrEmpty(id) = False Then
            Dim file As EnvDTE.TextDocument = DTE.ActiveDocument().Object("TextDocument")
            If file Is Nothing Then
                Return
            End If

            Dim head As EditPoint = file.StartPoint().CreateEditPoint()

            head.Insert( _
                "#ifndef" + vbTab + id + vbNewLine + _
                "#define" + vbTab + id + vbNewLine + vbNewLine)

            Dim tail As EditPoint = file.EndPoint().CreateEditPoint()

            If file.EndPoint().LineCharOffset <> 1 Then
                tail.Insert(vbNewLine)
            End If
            tail.Insert(vbNewLine + "#endif" + vbTab + "//" + id + vbNewLine)
        End If

    End Sub

End Module

[追記 20110207]ファイルが空の場合の挙動がおかしかったのを修正[/追記]

[追記 20120705]
新しい版
使用後にフォーカスが外れてしまう問題を修正
Visual Studio 2008で確認

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics

Public Module IncludeGuard

    Sub IncludeGuard()
        Dim id As String
        id = InputBox("Include Guardに用いる文字列")
        If String.IsNullOrEmpty(id) = False Then
            Dim file As EnvDTE.TextDocument = DTE.ActiveDocument().Object("TextDocument")
            Dim pane As EnvDTE.TextPane = DTE.ActiveWindow.Object.ActivePane
            If file Is Nothing Then
                Return
            End If

            Dim sel As TextSelection = CType(file.Selection, TextSelection)
            Dim head As EditPoint = file.StartPoint().CreateEditPoint()

            Dim orig_line As Integer = file.Selection.ActivePoint.Line
            Dim orig_offs As Integer = file.Selection.ActivePoint.LineCharOffset

            head.Insert( _
                "#ifndef" + vbTab + id + vbNewLine + _
                "#define" + vbTab + id + vbNewLine + vbNewLine)

            Dim tail As EditPoint = file.EndPoint().CreateEditPoint()

            If file.EndPoint().LineCharOffset <> 1 Then
                tail.Insert(vbNewLine)
            End If

            tail.Insert(vbNewLine + "#endif" + vbTab + "//" + id + vbNewLine)

            sel.MoveToLineAndOffset(orig_line + 3, orig_offs)
            pane.Activate()
        End If

    End Sub

End Module

[/追記]