help with VB macro in .NET '05 IDE

hi,

i wrote a macro to break into a running application while i am debugging. i did so because in C# when i break the "Program.cs" file is opened. i don't want it to open and i always dismiss it.

here is the macro:

Sub MyBreak()
Dim i As Integer
Dim Wnd As Window
Dim FileName As String

FileName = "Program.cs"

DTE.Debugger.Break(True)

'find the window and activate it
For i = 1 To DTE.Windows.Count
Wnd = DTE.Windows.Item(i)

'MsgBox(Wnd.Document.FullName)
If (Wnd.Document.FullName.Contains(FileName)) Then
Exit For
End If
Next

Wnd.Activate()
Wnd.Close(vsSaveChanges.vsSaveChangesNo)

'now close it
'DTE.ExecuteCommand("Window.CloseDocumentWindow")
'DTE.ActiveWindow.Close(vsSaveChanges.vsSaveChangesNo)
End Sub


often when i run it i get the following error message:

"Object reference not set to an instance of an object."

i am not a VB programmer but it looks like the error message is related to an uninitialized object being used. the macro does what i want it to except it barfs up this message. does anyone know what this is and how i might prevent/suppress it

thanks,
scott


Answer this question

help with VB macro in .NET '05 IDE

  • ToddKitta

    First of all, add an exception handler to your macro and you will know the offending line.

    Guessing blindly I think that it is Wnd.Document.FullName... because Window.Document can return null (nothing) if the window does not contain a document (such as any toolwindow like the Object Browser). If you get such window before the window that contains the document that you are looking for, you get the exception.



  • EtherealSky

    Hallo Scott,

    I tried your code but "unfortunately", it works fine. I see no exception. In any case, when you break the execution of your code, the debugger opens the document that contains the last managed command executed before you break and selects it. In a windows forms application, this is usually Application.Run(new Form1()) in the Program.cs file. After this command, everything is unmanaged dispatching of messages to the window procedure. You may better want to implement some events and use breakpoints instead. Or override WndProc() if you want to get deeper. You can always use F11 to do a step by step debugging of your code.



  • joey001

    It's like this:

    Sub MyMacro

    Try

    (your code)

    Catch ex As Exception

    MessageBox.Show(ex.ToString)

    End Try

    End Sub



  • kcchesnut

    thanks to both of you. the exception only sometimes happens, lately it has not.

    i don't know how to add the exception handler to the macro. i only do VB when absolutely necessary. i'm sure i can figure it out though. thanks.

    scott.

  • help with VB macro in .NET '05 IDE