2010-01-05 134 views
1

我想從Visual Studio中的一個宏將輸出記錄到輸出窗口。我認爲Debug.Print會像在Visual Basic.NET和VBA中那樣執行這個技巧,但它並沒有這樣做。Visual Studio 2005中的簡單宏輸出/控制檯日誌?

我發現this,並試圖this,它不是簡單的,也不在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 

有沒有簡單的方法來做到這一點?我厭倦了不得不彈出一個消息框(主要是因爲當你運行這個腳本時,你不得不單擊ok幾十次),當我只想要一些簡單的控制檯輸出時,我可以看看。

+0

在這一點上,我會寫出來寫出一個日誌文件... – leeand00 2010-01-05 19:39:34

+0

我對此也感興趣,你有沒有找到辦法做到這一點? – levesque 2010-01-21 19:20:36

+0

@JC不,還沒... – leeand00 2010-01-21 19:24:00

回答

3

這裏是我在我的宏使用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 
相關問題