Thanks and regards , Mark
Untested/compiled Win32 pseudo-code using only ole2.h, omitting error
checks for brevity:
IDispatch *excelApp = 0;
CoCreateInstance(uuidof(Excel.Application), 0, CLSCTX_SERVER,
IID_IDispatch, (void**)&excelApp);
VARIANT result;
VariantInit(&result);
VARIANT args[8]; // up to eight parameters
DISPPARAMS params;
params.cArgs = 0;
params.cNamedArgs = 0;
params.rgdispidNamedArgs = 0;
params.rgvarg = args;
DISPID dispid;
OLECHAR *api = L"Workbooks";
excelApp->GetIDOfNames(IID_NULL, &api, 1, LOCALE_USER_DEFAULT, &dispid);
excelApp->Invoke(dispid, IID_NULL, LOCALE_USER_DEFAULT,
DISPATCH_PROPERTYGET, ¶ms, &result, 0, 0);
IDispatch *excelWorkbooks = result.pdispVal;
OLECHAR *api = L"Add";
excelWorkbooks->GetIDOfNames(IID_NULL, &api, 1, LOCALE_USER_DEFAULT,
&dispid);
excelWorkbooks->Invoke(dispid, IID_NULL, LOCALE_USER_DEFAULT,
DISPATCH_METHOD, ¶ms, &result, 0, 0);
excelWorkbooks->Release();
IDispatch *excelWorkbook = result.pdispVal;
OLECHAR *api = L"Range";
args[0].vt = VT_BSTR;
args[0].bstrVal = SysAllocStringLen(L"A1", 2);
args[1].vt = VT_BSTR;
args[1].bstrVal = SysAllocStringLen(L"C3", 2);
params.cArgs = 2;
excelWorkbook->GetIDOfNames(IID_NULL, &api, 1, LOCALE_USER_DEFAULT,
&dispid);
excelWorkbook->Invoke(dispid, IID_NULL, LOCALE_USER_DEFAULT,
DISPATCH_PROPERTYGET, ¶ms, &result, 0, 0);
SysFreeString(args[0].bstrVal);
SysFreeString(args[1].bstrVal);
excelWorkbook->Release();
IDispatch *excelRange = result.pdispVal;
Now you have a Range object, and you can call the "Value" property with
code simliar to the stuff above.
Obviously it will be easier to use a decent C++ framework that
encapsulates all that parameters stuff for you, e.g. ActiveQt
http://doc.trolltech.com/3.3/activeqt.html
When done, call Quit on Excel.Application and release the interface to
shut down Excel again.
Volker
It should look something like this:
IDispatch *pXlBook;
{
VARIANT result;
VariantInit(&result);
AutoWrap(DISPATCH_PROPERTYGET, &result, pXlBooks,
L"Open(c:\\test.xls");
pXlBook = result.pdispVal;
}
but it just ddoesnt execute (it does compile, though). Hope you can
help me once again :)
Regards, Mark
The only thing I can tell you for sure is that the problem is in the
AutoWrap function :)
Now I guess the code for that is a bit longer than useful here, but note
that "Open" is not a property getter (so call as DISPATCH_METHOD rather
than DISPATCH_PROPERTYGET). Then I assume your AutoWrap function simply
parses the string, calls IDispatch::GetIdOfNames() for the part before the
opening parenthesis, turns every parameters into a VARIANT of type VT_BSTR
(not caring about syntax errors, your string literal is missing the
closing parenthesis), and calls IDispatch::Invoke() with the results. This
should work :)
Volker
PS: You should also initialize your pointer to 0.
"Mark O" <onr...@wanadoo.nl> wrote in message
news:19f1f902.04051...@posting.google.com...
"Mark O" <onr...@wanadoo.nl> wrote in message
news:19f1f902.04050...@posting.google.com...
// Call Workbooks.Add() to get a new workbook...
IDispatch *pXlBook;
{
VARIANT result;
VariantInit(&result);
AutoWrap(DISPATCH_METHOD, &result, pXlBooks,
L"Open(c:\\test.xls)",0);
pXlBook = result.pdispVal;
}
Still it doesnt understand the open method. Indeed, it seems to parse
the autowrap function call. What I need is to find the right syntax to
open the xls book in this manner. And it seems infindeable on the
internet. I'll keep looking for it, cause I can imagine it'd make lots
of people happy to find out about this - especially dummies like me :)
Errormessage I get is:
IDispatch::GetIdSOfNames("Open(c:\test.xls)"failed)w/err 0x80020006
Any help greatly appreciated :)
Regards Mark
// Call Workbooks.Add() to get a new workbook...
IDispatch *pXlBook;
{
VARIANT result;
VariantInit(&result);
VARIANT x;
x.vt = VT_BSTR;
x.bstrVal = ::SysAllocString(L"C:\\test.xls");
AutoWrap(DISPATCH_METHOD, &result, pXlBooks, L"open",1, x);
pXlBook = result.pdispVal;
SysFreeString(x.bstrVal);
}
Thanks for your help! Now I can continue reading ranges from my
workbook into an array.
Regards,
Mark
Me again!
I'm trying to:
Read values from an existing Excel sheet, and put them in an array.
Now, I assume I have to put something into these lines (where the << are:
IDispatch *pXlRange;
{
VARIANT parm;
parm.vt = VT_BSTR;
parm.bstrVal = ::SysAllocString(L"A1:C8");
VARIANT result;
VariantInit(&result);
AutoWrap(DISPATCH_PROPERTYGET, &result, pXlSheet, L"Range", 1, parm);
VariantClear(&parm);
for(int i=1; i<=15; i++) {
for(int j=1; j<=15; j++) {
VARIANT tmp;
tmp.vt = VT_I4;
tmp.lVal = i*j; <<<<<<<<<< here must the values of the range be put in..
// Add to safearray...
long indices[] = {i,j};
SafeArrayPutElement(arr.parray, indices, (void *)&tmp);
pXlRange = result.pdispVal;
}
but I just cant figure out what that would be. Can you help me?
Regards,
Mark
Regards,
Mark
--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: agnic...@mvps.org
MVP VC FAQ: http://www.mvps.org/vcfaq
=====================================
"Mark O" <onr...@wanadoo.nl> wrote in message
news:19f1f902.04060...@posting.google.com...
Thanks much!
--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: agnic...@mvps.org
MVP VC FAQ: http://www.mvps.org/vcfaq
=====================================
"Mark O" <onr...@wanadoo.nl> wrote in message
news:19f1f902.04060...@posting.google.com...
Right now I did this (don't laugh :)):
for(int i=1; i<=15; i++) {
for(int j=1; j<=15; j++) {
VARIANT tmp;
tmp.vt = VT_I4;
tmp.lVal = DISPID_VALUE;
// Add to safearray...
long indices[] = {i,j};
This at least doesnt give me the errors I got before. But now, it
writes an array (safearray) with all 0's to the sheet. My intention is
for it to read the existing values from the sheet into the array and
write them back to the sheet again. This still is not what happens.
Hope you have another suggestion for my struggle ..
Greetings, Mark
microsoft.public.office.developer.office.sdks
microsoft.public.excel.programming
--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: agnic...@mvps.org
MVP VC FAQ: http://www.mvps.org/vcfaq
=====================================
"Mark O" <onr...@wanadoo.nl> wrote in message
news:19f1f902.04060...@posting.google.com...
Regards,
Mark