Простой вывод макроса / запись в консоль в Visual Studio 2005? - PullRequest
1 голос
/ 05 января 2010

Я пытаюсь записать вывод в окно вывода одного из моих макросов в Visual Studio. Я думал, что Debug.Print сделает то же самое, что и в Visual Basic.NET и VBA, но это не так.

Я нашел это и попробовал это , это не просто и не работает в Visual Studio 2005 (см. Ниже):

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

См. Ошибка вывода ниже:

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

Есть ли простой способ сделать это? Мне надоело открывать окно сообщения (в основном потому, что когда вы запускаете скрипт, вы должны нажимать «ОК» миллионы раз), когда мне просто нужен какой-то простой вывод консоли, на который я могу посмотреть.

1 Ответ

3 голосов
/ 20 марта 2010

Вот GetOutputWindow, которое я использую в своих макросах, вы передаете ему имя - я считаю, что это часть стандартных макросов, поставляемых с Visual Studio, поэтому вы должны иметь возможность ссылаться на него как 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
...