How can I determine if an application has errors from an add-in? Read the items in the Error List.
Sometimes, in an add-in, you need to know if an application in the IDE has compiler errors. For example, if your add-in is using the CodeModel to explore and manipulate code, you have to realize that the CodeModel may not return code that will not compile. Basically, the CodeModel may not "see" code that does not compile. For that reason the your add-in has to know if there are errors in the application.
At least one of the methods for determining if there are errors in an application is to read the Error List Tool Window. The code for this article will show you how to not only determine if there are errors, it will be able to tell you the project file that is in error. In an add-in, which I have developed, I am using the FileCodeModel to retrieve all of the code from the active code window. If the active code window has errors in it, I need to warn the developer of the possible code loss if they proceed with the use of the add-in to load and then save the code back to the window. Obviously, if I don't retrieve all of the code and then overwrite the code window with the code that I retrieved, then I will have lost code that would not compile. That could be as little as one line, in the case of a variable, or many lines if a whole method will not compile. For example, if there is an error in the definition of a function, then the FileCodeModel will not show that function at all.
First, I will show you the code for the method for determining if there are errors in the application.
| Private Function IsCompilerErrors(ByVal oVS2 As EnvDTE.DTE) As String Dim ide As DTE2 ide = CType(oVS2, DTE2) Dim myErrors As ErrorList ide.ExecuteCommand("View.ErrorList", " ") myErrors = ide.ToolWindows.ErrorList Dim count As Integer = count = myErrors.ErrorItems.Count Dim currentFile As String = ide.ActiveDocument.Name.ToLower For k As Integer = 1 To count Try If myErrors.ErrorItems.Item(k).ErrorLevel = _ vsBuildErrorLevel.vsBuildErrorLevelHigh Then Dim fn As String = _ IO.Path.GetFileName(myErrors.ErrorItems.Item(k).FileName).ToLower If fn.Equals(currentFile) Then ide.ExecuteCommand("View.ViewCode") Return currentFile End If End If Catch End Try Next ide.ExecuteCommand("View.ViewCode") Return String.Empty End Function |
| Dim fileName As String = IsCompilerErrors(oVB2) If fileName.Length > 0 Then If MsgBox("Your application has compile errors. " & _ "In particular, the current file: " & fileName _ & " has errors. Saving a file with errors from " & _ "Class Organzier may lose code. Do you want to proceed?", _ MsgBoxStyle.Question Or _ MsgBoxStyle.YesNo Or _ MsgBoxStyle.DefaultButton2, _ "Confirm Proceeding With Errors") = MsgBoxResult.No _ Then Exit Sub End If |
| Ask a Question, or give your feedback on my articles or products by going to the KnowDotNet Forum or by clicking on My Blog. | ![]() |