penschke
asked on
OLE-automation for Excel97
I have used OLE-automation to control Excel95 from Visual C++. Therefore I generated via the class-wizard with the Excel5-OLB-file the 4 classes Application, Workbooks, Worksheet and Range. The approach is described in Inside Visual C++ from MS-Press (Ch. 24) and worked fine. Now I want to do the same with Excel97. The previously approach did not work. Some class names differ by a prefixed underscore, some methods I used are no more available (Workbooks of Application) and I always get an exception when trying to start up Excel.
Has somebody successfully implemented an Excel97-OLE-Automation-Ser ver and can give me help??
Has somebody successfully implemented an Excel97-OLE-Automation-Ser
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Well sorry, I have found an other problem. What works is the following source-code compiled in DEBUG-mode. In RELEASE-mode the Excel does not start-up properly:
// NOTE: This example will only work with Excel8 in Office97
// Compile with cl /GX comexcel.cpp
// TO DO: Edit the #import paths
#import <mso97.dll> no_namespace rename("DocumentProperties ", "DocumentPropertiesXL")
#import <vbeext1.olb> no_namespace
#import <excel8.olb> rename("DialogBox", "DialogBoxXL") rename("RGB", "RBGXL") rename("DocumentProperties ", "DocumentPropertiesXL") no_dual_interfaces
#include <stdio.h>
#include <iostream.h>
#include <tchar.h>
void dump_com_error(_com_error &e)
{
_tprintf(_T("Oops - hit an error!\\n"));
_tprintf(_T("\\a\\tCode = %08lx\\n"), e.Error());
_tprintf(_T("\\a\\tCode meaning = %s\\n"), e.ErrorMessage());
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Descript ion());
_tprintf(_T("\\a\\tSource = %s\\n"), (LPCTSTR) bstrSource);
_tprintf(_T("\\a\\tDescrip tion = %s\\n"), (LPCTSTR) bstrDescription);
}
// If this is placed in the scope of the smart pointers, they must be
// explicitly Release(d) before CoUninitialize() is called. If any reference
// count is non-zero, a protection fault will occur.
struct StartOle {
StartOle() {
CoInitialize(NULL);
}
~StartOle(){
CoUninitialize();
}
// };
} _inst_StartOle;
void main()
{
// StartOle Excel97;
using namespace Excel;
_ApplicationPtr pXL;
try {
pXL.CreateInstance(L"Excel .Applicati on.8");
pXL->Visible = VARIANT_TRUE;
// Open sample Excel-Sheet
char excelFilename[1024] = "d:\\projects\\msvcpp5\\ex cel97_driv er\\test_f iles\\samp le_excel97 .xls";
WorkbooksPtr pBooks = pXL->Workbooks;
_WorkbookPtr pBook = pBooks->Open((char*)excelF ilename);
_WorksheetPtr pSheet = pXL->ActiveSheet;
char x;
char y[8] = ""; int iy;
char xy[9];
float fnum;
for (x='C';x<='P';x++)
{
for (iy=2; iy<=50; iy++)
{
sprintf(y, "%d", iy);
strcpy (xy ,&x);
strcat (xy, y);
fnum = pSheet->Range[xy]->Value;
// fnum = Vnum->getFloatRef();
fnum++;
// Vnum.putFloatRef(fnum);
pSheet->Range[xy]->Value = fnum;
}
}
Sleep(3000);
// Close sheet
pBook->Save();
// delete pSheet;
// delete pBook;
// delete pBooks;
pXL->Quit();
}
// catch exceptions if raised
catch(_com_error &e)
{
dump_com_error(e);
}
}
Both compile-version behave differently. Might that be a Microsoft-Bug?
// NOTE: This example will only work with Excel8 in Office97
// Compile with cl /GX comexcel.cpp
// TO DO: Edit the #import paths
#import <mso97.dll> no_namespace rename("DocumentProperties
#import <vbeext1.olb> no_namespace
#import <excel8.olb> rename("DialogBox", "DialogBoxXL") rename("RGB", "RBGXL") rename("DocumentProperties
#include <stdio.h>
#include <iostream.h>
#include <tchar.h>
void dump_com_error(_com_error &e)
{
_tprintf(_T("Oops - hit an error!\\n"));
_tprintf(_T("\\a\\tCode = %08lx\\n"), e.Error());
_tprintf(_T("\\a\\tCode meaning = %s\\n"), e.ErrorMessage());
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Descript
_tprintf(_T("\\a\\tSource = %s\\n"), (LPCTSTR) bstrSource);
_tprintf(_T("\\a\\tDescrip
}
// If this is placed in the scope of the smart pointers, they must be
// explicitly Release(d) before CoUninitialize() is called. If any reference
// count is non-zero, a protection fault will occur.
struct StartOle {
StartOle() {
CoInitialize(NULL);
}
~StartOle(){
CoUninitialize();
}
// };
} _inst_StartOle;
void main()
{
// StartOle Excel97;
using namespace Excel;
_ApplicationPtr pXL;
try {
pXL.CreateInstance(L"Excel
pXL->Visible = VARIANT_TRUE;
// Open sample Excel-Sheet
char excelFilename[1024] = "d:\\projects\\msvcpp5\\ex
WorkbooksPtr pBooks = pXL->Workbooks;
_WorkbookPtr pBook = pBooks->Open((char*)excelF
_WorksheetPtr pSheet = pXL->ActiveSheet;
char x;
char y[8] = ""; int iy;
char xy[9];
float fnum;
for (x='C';x<='P';x++)
{
for (iy=2; iy<=50; iy++)
{
sprintf(y, "%d", iy);
strcpy (xy ,&x);
strcat (xy, y);
fnum = pSheet->Range[xy]->Value;
// fnum = Vnum->getFloatRef();
fnum++;
// Vnum.putFloatRef(fnum);
pSheet->Range[xy]->Value = fnum;
}
}
Sleep(3000);
// Close sheet
pBook->Save();
// delete pSheet;
// delete pBook;
// delete pBooks;
pXL->Quit();
}
// catch exceptions if raised
catch(_com_error &e)
{
dump_com_error(e);
}
}
Both compile-version behave differently. Might that be a Microsoft-Bug?
ASKER
That was exactly what I wanted.
I have registered asa an expert as well.
Would You like to have an email-conversation?
Regards,
Walter