views:

152

answers:

2

I am trying to compile the following code and i am getting the error:

Cannot create instance of abstract class . Please help

m_objExcel = new Excel.Application();
m_objBooks = (Excel.Workbooks)m_objExcel.Workbooks;
m_objBook = (Excel._Workbook)(m_objBooks.Add(m_objOpt));
m_objSheets = (Excel.Sheets)m_objBook.Worksheets;
m_objSheet = (Excel._Worksheet)(m_objSheets.get_Item(1));

// Create an array for the headers and add it to cells A1:C1.
object[] objHeaders = {"Order ID", "Amount", "Tax"};
m_objRange = m_objSheet.get_Range("A1", "C1");
m_objRange.Value = objHeaders;
m_objFont = m_objRange.Font;
m_objFont.Bold=true;

// Create an array with 3 columns and 100 rows and add it to
// the worksheet starting at cell A2.
object[,] objData = new Object[100,3];
Random rdm = new Random((int)DateTime.Now.Ticks);
double nOrderAmt, nTax;
for(int r=0;r<100;r++)
{
    objData[r,0] = "ORD" + r.ToString("0000");
    nOrderAmt = rdm.Next(1000);
    objData[r,1] = nOrderAmt.ToString("c");
    nTax = nOrderAmt*0.07;
    objData[r,2] = nTax.ToString("c");
}
m_objRange = m_objSheet.get_Range("A2", m_objOpt);
m_objRange = m_objRange.get_Resize(100,3);
m_objRange.Value = objData;

// Save the Workbook and quit Excel.
m_objBook.SaveAs(m_strSampleFolder + "Book2.xls", m_objOpt, m_objOpt,
    m_objOpt, m_objOpt, m_objOpt, Excel.XlSaveAsAccessMode.xlNoChange,
    m_objOpt, m_objOpt, m_objOpt, m_objOpt);
m_objBook.Close(false, m_objOpt, m_objOpt);
m_objExcel.Quit();
+5  A: 

I would suggest you determine the line where it is breaking...

Abstract classes can not be created directly, instead you need to create an instance via a derived class, that is why you are getting the exception.

If you don't wrap the code with a try/catch or turn on the "Common Language Runtime Exceptions" (default shortcut is Ctrl+E in VS2008) then you should be able to find the line causing the problem.

I think it might even be your first line, normally you use something like:

new ApplicationClass();
Ian
could you post the edited code?
You need to check that that is the problem. If that line is the problem then it could be as simple as m_objExcel = new ApplicationClass();
Ian
A: 

Abstract classes cannot be instanciated with a 'new'. Look at the error and and see what line its point at. Then check the code to see what it is doing. Post the line if you cannot determin the answer.

just try Excel.Application instead of new Excel.Application()

Preet Sangha
m_objExcel = new Excel.Application();