Articles → .NET → Create Your First Visual Studio Add-In In C#

Create Your First Visual Studio Add-In In C#






Create A New Project


  1. Go to file – ‘New project’ - ‘Visual Studio Add-in’
  2. Picture showing selecting the Visual Studio Add-in project type in visual studio
    Click to Enlarge



  3. The following window appears
  4. Picture showing the first screen of the add-in wizard
    Click to Enlarge



  5. In the next window, select ‘Create an Add-in using Visual C#’ and click on ‘Next’
  6. Picture showing the screen to select the programming language used for creating the visual studio add-ins
    Click to Enlarge

  7. In the following window click on ‘Next’
  8. Picture showing the screen for selecting the windows host for add-ins
    Click to Enlarge

  9. In the following window, you can enter the name and description of the add-in. Once you edit the information, click on ‘Next’ button.
  10. Picture showing the screen for adding the name and description of add-ins
    Click to Enlarge

  11. In the following figure, select the first check box. It means an option for add-in is added in ‘Tools’. After that click on ‘Next’.
  12. Picture showing the wizard screen for adding the add-ins in the tool
    Click to Enlarge

  13. In the following window, you can add ‘About’ information about the add-in. Once done click on ‘Next’
  14. Picture showing the wizard screen for adding the about information about the add-ins
    Click to Enlarge

  15. Finally, click on ‘Finish’ in the following window
  16. 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