views:

181

answers:

1

I'm trying to log output to the Output window from one of my macros in Visual Studio. I thought that Debug.Print would do the trick like it does in Visual Basic.NET and VBA, but it doesn't do that.

I found this, and tried this, it is not simple, nor does it work in Visual Studio 2005 (see bellow):

Private Function GetMacroOutputPane() As OutputWindowPane
    Dim ow As OutputWindow = _
        DTE.Windows.Item(Constants.vsWindowKindOutput).Object()

    Dim outputPane As OutputWindowPane

    Try
        outputPane = ow.OutputWindowPanes.Item("Macros")
    Catch ex As Exception
        outputPane = ow.OutputWindowPanes.Add("Macros")
    End Try

    Return outputPane
End Function

Private Sub WriteOutput( _
ByVal s As String)

    Dim buffer As String

    buffer = buffer & Date.Now.ToLongTimeString()
    buffer = buffer & " "
    buffer = buffer & s
    buffer = buffer & vbCrLf

    Dim output As String = buffer.ToString()

    Dim outputPane As OutputWindowPane = GetMacroOutputPane()
    outputPane.OutputString(output)
End Sub

See the Output error below:

A first chance exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll
A first chance exception of type 'System.IndexOutOfRangeException' occurred in VBAssembly
The thread 0x23e4 has exited with code 0 (0x0).
The thread 0x1118 has exited with code 0 (0x0).
'vsmsvr.exe' (Managed): Loaded 'C:\Windows\assembly\GAC\EnvDTE80\8.0.0.0__b03f5f7f11d50a3a\EnvDTE80.dll', No symbols loaded.
'vsmsvr.exe' (Managed): Loaded 'C:\Windows\assembly\GAC\EnvDTE80\8.0.0.0__b03f5f7f11d50a3a\EnvDTE80.dll', No symbols loaded.
A first chance exception of type 'System.Exception' occurred in VBAssembly
A first chance exception of type 'System.Exception' occurred in VBAssembly
A first chance exception of type 'System.Exception' occurred in VBAssembly
A first chance exception of type 'System.Exception' occurred in VBAssembly
A first chance exception of type 'System.Exception' occurred in VBAssembly
A first chance exception of type 'System.Exception' occurred in VBAssembly
A first chance exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll
A first chance exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll
'vsmsvr.exe' (Managed): Loaded 'C:\Windows\assembly\GAC\EnvDTE80\8.0.0.0__b03f5f7f11d50a3a\EnvDTE80.dll', No symbols loaded.
A first chance exception of type 'System.Exception' occurred in VBAssembly
A first chance exception of type 'System.Exception' occurred in VBAssembly
A first chance exception of type 'System.Exception' occurred in VBAssembly
A first chance exception of type 'System.Exception' occurred in VBAssembly
A first chance exception of type 'System.Exception' occurred in VBAssembly
A first chance exception of type 'System.Exception' occurred in VBAssembly
A first chance exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll

Is there any simple way to do this? I'm sick of having to popup a message box (mainly because when you run the script it you have to click ok a zillion times) when I just want some simple console output that I can look at.

+1  A: 

Here is the GetOutputWindow that I use in my macros, you pass a name to it - I believe that it's part of the standard macros that come with Visual Studio, so you should be able to reference it like Utilities.GetOutputWindowPane

Private Sub WriteOutput(ByVal s As String)

    Dim buffer As String

    buffer = buffer & Date.Now.ToLongTimeString()
    buffer = buffer & " "
    buffer = buffer & s
    buffer = buffer & vbCrLf

    Dim output As String = buffer.ToString()

    Dim outputPane As OutputWindowPane = GetOutputWindowPane("Macros")
    outputPane.OutputString(output)
End Sub

Function GetOutputWindowPane(ByVal Name As String, Optional ByVal show As Boolean = True) As OutputWindowPane
    Dim window As Window
    Dim outputWindow As OutputWindow
    Dim outputWindowPane As OutputWindowPane

    window = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput)
    If show Then window.Visible = True
    outputWindow = window.Object
    Try
        outputWindowPane = outputWindow.OutputWindowPanes.Item(Name)
    Catch e As System.Exception
        outputWindowPane = outputWindow.OutputWindowPanes.Add(Name)
    End Try
    outputWindowPane.Activate()
    Return outputWindowPane
End Function
Brian Schmitt