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

Create your first visual studio add-in in C#






Create a new project





Click to Enlarge






Click to Enlarge






Click to Enlarge




Click to Enlarge




Click to Enlarge




Click to Enlarge




Click to Enlarge




Click to Enlarge


Add a reference of Microsoft.Office.Interop.Excel





Click to Enlarge


Adding code to export error list to excel





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





Click to Enlarge




Click to Enlarge




Click to Enlarge


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

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250