Does anyone know why ws->CreateDispatch("Excel.Sheet") doesn't work?
Here is the source.
#include <stdio.h>
#include <ole_Nat.h>
#include <xl5en32.h>
// this code is adapted from the XLCLIENT code sample...
// this macro allocates a cell using the Range object
#define ALLOCRANGE(cell) \
VariantClear(&v); \
V_VT(&v) = VT_BSTR; \
V_BSTR(&v) = SysAllocString(cell); \
vRet = ws->Range(v,v); \
SysFreeString(V_BSTR(&v));
// this macro stuffs a string into a cell
#define PUTSTR(cell,str) \
ALLOCRANGE(cell) \
V_VT(&v) = VT_BSTR; \
V_BSTR(&v) = SysAllocString(OLESTR(str)); \
ptr = new Range(V_DISPATCH(&vRet)); \
ptr->SetValue(v); \
SysFreeString(V_BSTR(&v)); \
delete ptr;
// this macro stuffs a float into a cell
#define PUTFLT(cell,flt) \
ALLOCRANGE(cell) \
V_VT(&v) = VT_R8; \
V_R8(&v) = flt; \
ptr = new Range(V_DISPATCH(&vRet)); \
ptr->SetValue(v); \
delete ptr;
// this macro stuffs an integer into a cell
#define PUTINT(cell, ival) \
ALLOCRANGE(cell) \
V_VT(&v) = VT_I4; \
V_I4(&v) = (int)ival; \
ptr = new Range(V_DISPATCH(&vRet)); \
ptr->SetValue(v); \
delete ptr;
JNIEXPORT void JNICALL Java_ole_Nat_meto
(JNIEnv *, jobject)
{
printf("Inicio OLE\n");
VARIANT v, vRet;
Range *ptr;
// part 1: Create Excel sheet object to work with
Worksheet *ws = new Worksheet();
if (!ws->CreateDispatch("Excel.Sheet"))
{
printf("Error Worksheet\n");
return;
}
// part 2: Assign headings the array to cells
PUTSTR(OLESTR("A1"),"Threads");
PUTSTR(OLESTR("B1"),"Bias");
PUTSTR(OLESTR("C1"),"Elapsed time");
PUTSTR(OLESTR("D1"),"Response");
PUTSTR(OLESTR("A2"),"Concurrent");
PUTSTR(OLESTR("B2"),"I/O bound");
printf("Fin\n");
}
Couldn't you describe where the error occurs? If it is the call to
CreateDispatch: The function enables a second parameter with the chance to
retrieve an error number (COleException::m_sc). My we can help you knowing
the error code
Mirko
Mauricio CASTRO ESPINOSA wrote in message ...
>Hello,
>
>Does anyone know why ws->CreateDispatch("Excel.Sheet") doesn't work?
>
>Here is the source.
>...
Worksheet ws; //Excel Object
VARIANT v, vRet;
Range *ptr;
try{
VariantClear(&v);
V_VT(&v) = VT_BSTR;
V_BSTR(&v) = SysAllocString(OLESTR("A1"));
vRet = ws->Range(v,v); //Here it s throws the exception...
SysFreeString(V_BSTR(&v));
V_VT(&v) = VT_BSTR;
V_BSTR(&v) = SysAllocString(OLESTR("Threads"));
ptr = new Range(V_DISPATCH(&vRet));
ptr->SetValue(v);
SysFreeString(V_BSTR(&v));
delete ptr;
}
catch (COleException * e){
e->ReportError();
}
Mirko Fuchs wrote in message ...