Imports EnvDTE
Imports System

Public Module DocumentMacros

    Function GetDocument(ByVal caption As String) As Document
        ' Description: Retrieves the Document object associated with
        '              the specified window, or creates a text file in
        '              a new window and returns its Document object

        Dim win As Window

        Try
            win = DTE.Windows.Item(caption)
        Catch ex As System.Exception
            win = DTE.ItemOperations.NewFile("Allgemein\Textdatei", caption)
        End Try

        Return win.Document
    End Function


    Function GetTextDocument(ByVal caption As String) As TextDocument
        ' Description: Retrieves the TextDocument object associated with
        '              the specified window, or creates a text file in
        '              a new window and returns its TextDocument object

        Dim doc As Document = GetDocument(caption)

        Return doc.Object("TextDocument")
    End Function

    Sub CreateNewTextFile()
        ' Description: Shows how to use the ItemOperations.NewFile method
        '              to create a new text file

        Dim Item As String = "Allgemein\Textdatei"
        Dim Name As String = "MyTextFile"
        Dim ViewKind As String = Constants.vsViewKindPrimary
        Dim win As Window

        win = DTE.ItemOperations.NewFile(Item, Name, ViewKind)
    End Sub

End Module
