Articles → .NET → Create your first visual studio add-in in C#

Create your first visual studio add-in in C#






Create a new project




Picture showing selecting the Visual Studio Add-in project type in visual studio

Click to Enlarge





Picture showing the first screen of the add-in wizard

Click to Enlarge





Picture showing the screen to select the programming language used for creating the visual studio add-ins

Click to Enlarge



Picture showing the screen for selecting the windows host for add-ins

Click to Enlarge



Picture showing the screen for adding the name and description of add-ins

Click to Enlarge



Picture showing the wizard screen for adding the add-ins in the tool

Click to Enlarge



Picture showing the wizard screen for adding the about information about the add-ins

Click to Enlarge



Picture showing the last screen of the wizard

Click to Enlarge


Add a reference of Microsoft.Office.Interop.Excel




Picture showing adding the reference of Microsoft.Office.Interop.Excel in the project

Click to Enlarge


Adding code to export error list to excel




Picture showing implement the export error list code inside the Exec method of connect.cs file

Click to Enlarge



public void GetErrorList(DTE2 dte) {
  ErrorList myErrors;
  int count;
  ArrayList aString = new ArrayList();
  List < ErrorObject > errorList = new List < ErrorObject > ();
  _applicationObject.ExecuteCommand("View.ErrorList", " ");
  myErrors = _applicationObject.ToolWindows.ErrorList;
  count = myErrors.ErrorItems.Count;
  if (count != 0) {
    for (int i = 1; i <= count; i++) {
      errorList.Add(new ErrorObject() {
        Description = myErrors.ErrorItems.Item(i).Description,
        File = myErrors.ErrorItems.Item(i).FileName,
        Line = myErrors.ErrorItems.Item(i).Line,
        Column = myErrors.ErrorItems.Item(i).Column,
        Project = myErrors.ErrorItems.Item(i).Project
      });
    }
    ExportDataToExcel(errorList);
  }

}
private void ExportDataToExcel(List < ErrorObject > errorList) {
  int i = 0;
  Microsoft.Office.Interop.Excel.Application xlApp;
  Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
  Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
  object misValue = System.Reflection.Missing.Value;

  xlApp = new Microsoft.Office.Interop.Excel.Application();
  xlWorkBook = xlApp.Workbooks.Add(misValue);
  xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet) xlWorkBook.Worksheets.get_Item(1);

  xlWorkSheet.Cells[1, 1] = "Description";
  xlWorkSheet.Cells[1, 2] = "File";
  xlWorkSheet.Cells[1, 3] = "Line";
  xlWorkSheet.Cells[1, 4] = "Column";
  xlWorkSheet.Cells[1, 5] = "Project";
  for (i = 0; i < errorList.Count; i++) {
    xlWorkSheet.Cells[i + 2, 1] = errorList[i].Description;
    xlWorkSheet.Cells[i + 2, 2] = errorList[i].File;
    xlWorkSheet.Cells[i + 2, 3] = errorList[i].Line;
    xlWorkSheet.Cells[i + 2, 4] = errorList[i].Column;
    xlWorkSheet.Cells[i + 2, 5] = errorList[i].Project;
  }

  xlWorkBook.SaveAs("gyansangrah.xls", Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
  xlWorkBook.Close(true, misValue, misValue);
  xlApp.Quit();

  releaseObject(xlWorkSheet);
  releaseObject(xlWorkBook);
  releaseObject(xlApp);

}
private void releaseObject(object obj) {
  try {
    System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
    obj = null;
  }
  catch(Exception) {
    obj = null;
  }
  finally {
    GC.Collect();
  }
}




public class ErrorObject {
  public string Description {
    get;
    set;
  }
  public string File {
    get;
    set;
  }
  public int Line {
    get;
    set;
  }
  public int Column {
    get;
    set;
  }
  public string Project {
    get;
    set;
  }
}



Output




Picture showing the .net code with error

Click to Enlarge



Picture showing the ExportEWI submenu inside the toolmenu for exporting the error messages to excel

Click to Enlarge



Picture showing the error messages excel file exported from visual studio

Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Friday, August 14, 2015

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250