D
Dave Calkins
I'm attempting to use automation to interface with Excel. I used the VS2003
wizard to import MFC classes from the type library. I'm able to
successfully connect up with Excel, query for the version, create a new
workbook, and save it. The problem comes in when I try and write some
simple text to the document.
I've included a code snippet I put together to do some initial testing. The
code works except for the section between "BROKEN-START" and "BROKEN-STOP".
With that code un-commented I get a dialog indicating, "Does not support a
collection". In the code below, the Excel namespace is something I created
which includes all the generated classes from the type library (I just
declared the namespace around all the header inclusions).
Note that I get the same dialog if I replace the code between the -START
and -STOP with code which uses get_Worksheets() and then attempts to inquire
as to the size of the worksheets collection.
I'm sure there's something really basic I'm missing here. Any ideas?
===
#define VAR_STR(s) COleVariant(_T(s))
#define VAR_CSTR(s) COleVariant(s)
#define VAR_ENUM(e) COleVariant((long)e)
#define VAR_LONG(l) COleVariant((long)l)
#define VAR_EMPTY COleVariant()
#define VAR_PARAMNOTFOUND COleVariant((long)DISP_E_PARAMNOTFOUND,VT_ERROR)
void TestExcel()
{
COleException err;
LPDISPATCH pDispatch;
long lcid = GetUserDefaultLCID();
// automation objects
Excel::CApplication excelApp;
Excel::CWorkbooks workbooks;
Excel::CWorkbook workbook;
Excel::CWorksheet worksheet;
Excel::CRange range;
// get Excel application
if (excelApp.CreateDispatch(_T("Excel.Application"), &err))
{
// check version
CString ver = excelApp.get_Version();
if (_tcstod(ver,NULL) >= 11.0)
{
// create a new workbook with a single worksheet
workbooks = excelApp.get_Workbooks();
workbook = workbooks.Add(VAR_PARAMNOTFOUND);
////////////////////////////////////////////////////////////////
// BROKEN-START
////////////////////////////////////////////////////////////////
// the below causes a dialog to pop up which says,
// "Does not support a collection" ???
////////////////////////////////////////////////////////////////
// write some text into the document
worksheet = excelApp.get_ActiveSheet();
range = worksheet.get_Range(VAR_STR("A1"),VAR_STR("A1"));
range.put_Value2(lcid,VAR_STR("Hello World"));
////////////////////////////////////////////////////////////////
// BROKEN-STOP
////////////////////////////////////////////////////////////////
// save the new workbook
CFileDialog fd(FALSE,_T(".xls"),_T("myreport.xls"),0,
_T("Worksheet Files (*.xls)|*.xls|All Files (*.*)|*.*||"));
if (fd.DoModal() == IDOK)
{
workbook.SaveAs(VAR_CSTR(fd.GetPathName()),VAR_ENUM(Excel::xlWorkbookNormal),
VAR_EMPTY,VAR_EMPTY,VAR_EMPTY,VAR_EMPTY,(long)Excel::xlNoChange,
VAR_EMPTY,VAR_EMPTY,VAR_EMPTY,VAR_EMPTY,VAR_EMPTY);
}
AfxMessageBox(_T("done"));
}
else
{
AfxMessageBox(_T("Office 2003 (v11.0) or later must be installed."));
}
}
else
{
AfxMessageBox(_T("Office 2003 (v11.0) or later must be installed."));
}
// close the workbook
workbook.Close(VAR_LONG(0),VAR_EMPTY,VAR_EMPTY);
// ensure automation objects are all released
range.ReleaseDispatch();
worksheet.ReleaseDispatch();
workbook.ReleaseDispatch();
workbooks.ReleaseDispatch();
excelApp.ReleaseDispatch();
}
wizard to import MFC classes from the type library. I'm able to
successfully connect up with Excel, query for the version, create a new
workbook, and save it. The problem comes in when I try and write some
simple text to the document.
I've included a code snippet I put together to do some initial testing. The
code works except for the section between "BROKEN-START" and "BROKEN-STOP".
With that code un-commented I get a dialog indicating, "Does not support a
collection". In the code below, the Excel namespace is something I created
which includes all the generated classes from the type library (I just
declared the namespace around all the header inclusions).
Note that I get the same dialog if I replace the code between the -START
and -STOP with code which uses get_Worksheets() and then attempts to inquire
as to the size of the worksheets collection.
I'm sure there's something really basic I'm missing here. Any ideas?
===
#define VAR_STR(s) COleVariant(_T(s))
#define VAR_CSTR(s) COleVariant(s)
#define VAR_ENUM(e) COleVariant((long)e)
#define VAR_LONG(l) COleVariant((long)l)
#define VAR_EMPTY COleVariant()
#define VAR_PARAMNOTFOUND COleVariant((long)DISP_E_PARAMNOTFOUND,VT_ERROR)
void TestExcel()
{
COleException err;
LPDISPATCH pDispatch;
long lcid = GetUserDefaultLCID();
// automation objects
Excel::CApplication excelApp;
Excel::CWorkbooks workbooks;
Excel::CWorkbook workbook;
Excel::CWorksheet worksheet;
Excel::CRange range;
// get Excel application
if (excelApp.CreateDispatch(_T("Excel.Application"), &err))
{
// check version
CString ver = excelApp.get_Version();
if (_tcstod(ver,NULL) >= 11.0)
{
// create a new workbook with a single worksheet
workbooks = excelApp.get_Workbooks();
workbook = workbooks.Add(VAR_PARAMNOTFOUND);
////////////////////////////////////////////////////////////////
// BROKEN-START
////////////////////////////////////////////////////////////////
// the below causes a dialog to pop up which says,
// "Does not support a collection" ???
////////////////////////////////////////////////////////////////
// write some text into the document
worksheet = excelApp.get_ActiveSheet();
range = worksheet.get_Range(VAR_STR("A1"),VAR_STR("A1"));
range.put_Value2(lcid,VAR_STR("Hello World"));
////////////////////////////////////////////////////////////////
// BROKEN-STOP
////////////////////////////////////////////////////////////////
// save the new workbook
CFileDialog fd(FALSE,_T(".xls"),_T("myreport.xls"),0,
_T("Worksheet Files (*.xls)|*.xls|All Files (*.*)|*.*||"));
if (fd.DoModal() == IDOK)
{
workbook.SaveAs(VAR_CSTR(fd.GetPathName()),VAR_ENUM(Excel::xlWorkbookNormal),
VAR_EMPTY,VAR_EMPTY,VAR_EMPTY,VAR_EMPTY,(long)Excel::xlNoChange,
VAR_EMPTY,VAR_EMPTY,VAR_EMPTY,VAR_EMPTY,VAR_EMPTY);
}
AfxMessageBox(_T("done"));
}
else
{
AfxMessageBox(_T("Office 2003 (v11.0) or later must be installed."));
}
}
else
{
AfxMessageBox(_T("Office 2003 (v11.0) or later must be installed."));
}
// close the workbook
workbook.Close(VAR_LONG(0),VAR_EMPTY,VAR_EMPTY);
// ensure automation objects are all released
range.ReleaseDispatch();
worksheet.ReleaseDispatch();
workbook.ReleaseDispatch();
workbooks.ReleaseDispatch();
excelApp.ReleaseDispatch();
}