Articles → .NET → Create Your First Visual Studio Add-In In C#
Create Your First Visual Studio Add-In In C#
Create A New Project
- Go to file – ‘New project’ - ‘Visual Studio Add-in’
Click to Enlarge
- The following window appears
Click to Enlarge
- In the next window, select ‘Create an Add-in using Visual C#’ and click on ‘Next’
Click to Enlarge
- In the following window click on ‘Next’
Click to Enlarge
- In the following window, you can enter the name and description of the add-in. Once you edit the information, click on ‘Next’ button.
Click to Enlarge
- 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’.
Click to Enlarge
- In the following window, you can add ‘About’ information about the add-in. Once done click on ‘Next’
Click to Enlarge
- Finally, click on ‘Finish’ in the following window
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