1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
|
HRESULT GetCell(IDispatch* pXLSheet, LCID lcid, wchar_t* pszRange, VARIANT& pVt)
{
DISPPARAMS NoArgs = {NULL,NULL,0,0}; // Now this function here - 'GetCell()', is more general purpose than my 'GetXLCell()' above. Use this
IDispatch* pXLRange = NULL; // one when you aren't sure what type of value is contained within a cell, or, stated even more precisely,
VARIANT vArgArray[1]; // how Excel itself is internally handling the value contained within a cell. For example, if you type
VARIANT vResult; // 123456 in a cell, Excel the miserable but intelligent beaste will recognize that as a number, and when
DISPPARAMS DispParams; // the cell contents is returned in a VARIANT, the VARIANT::vt member will likely be VT_I4 or VT_R8.
HRESULT hr; // However, if the user or creator of the Excel SpreadSheet's data explicitely typed the cell as a 'Text'
// cell, (Right Click On A Cell And Choose 'Format Cells') a VT_BSTR will be returned in the VARIANT. So
VariantInit(&vResult); // in other words, you can't just look at the data in an Excel spreadsheet and determine what the data
vArgArray[0].vt = VT_BSTR, // type of the data is. You had better reread that last sentence and think long and hard about it. But
vArgArray[0].bstrVal = SysAllocString(pszRange); // this function begins exactly like the one above, but when it gets the IDispatch* to the Excel Range,
DispParams.rgvarg = vArgArray; // it simply makes the IDispatch::Invoke() call to obtain the contents of the cell, and returns that
DispParams.rgdispidNamedArgs = 0; // contents to the caller in the [out] parameter VARIANT& pVt. The caller then has the responsibility
DispParams.cArgs = 1; // for determining what the VARIANT contains, e.g, a number, a string, a date, an interface pointer, etc.,
DispParams.cNamedArgs = 0; // and for freeing that memory if necessary.
hr=pXLSheet->Invoke(0xC5,IID_NULL,lcid,DISPATCH_PROPERTYGET,&DispParams,&vResult,NULL,NULL);
if(FAILED(hr))VariantClear(&vResult);
return hr;
pXLRange=vResult.pdispVal;
//Member Get Value <6> () As Variant
VariantClear(&vArgArray[0]);
VariantClear(&pVt);
hr=pXLRange->Invoke(6,IID_NULL,lcid,DISPATCH_PROPERTYGET,&NoArgs,&pVt,NULL,NULL);
pXLRange->Release();
return hr;
}
IDispatch* SelectWorkSheet(IDispatch* pXLWorksheets, LCID& lcid, wchar_t* pszSheet)
{
VARIANT vResult;
HRESULT hr;
VARIANT vArgArray[1];
DISPPARAMS DispParams;
DISPID dispidNamed;
IDispatch* pXLWorksheet = NULL;
// Member Get Item <170> (In Index As Variant<0>) As IDispatch >> Gets pXLWorksheet
// [id(0x000000aa), propget, helpcontext(0x000100aa)] IDispatch* Item([in] VARIANT Index);
VariantInit(&vResult);
vArgArray[0].vt = VT_BSTR;
vArgArray[0].bstrVal = SysAllocString(pszSheet);
DispParams.rgvarg = vArgArray;
DispParams.rgdispidNamedArgs = &dispidNamed;
DispParams.cArgs = 1;
DispParams.cNamedArgs = 0;
hr=pXLWorksheets->Invoke(0xAA,IID_NULL,lcid,DISPATCH_PROPERTYGET,&DispParams,&vResult,NULL,NULL);
if(FAILED(hr))
return NULL;
pXLWorksheet=vResult.pdispVal;
SysFreeString(vArgArray[0].bstrVal);
// Worksheet::Select()
VariantInit(&vResult);
VARIANT varReplace;
varReplace.vt = VT_BOOL;
varReplace.boolVal = VARIANT_TRUE;
dispidNamed = 0;
DispParams.rgvarg = &varReplace;
DispParams.rgdispidNamedArgs = &dispidNamed;
DispParams.cArgs = 1;
DispParams.cNamedArgs = 1;
hr=pXLWorksheet->Invoke(0xEB,IID_NULL,lcid,DISPATCH_METHOD,&DispParams,&vResult,NULL,NULL);
return pXLWorksheet;
}
IDispatch* OpenXLWorkBook(IDispatch* pXLWorkbooks, LCID& lcid, wchar_t* pszWorkBookPath)
{
VARIANT vResult;
VARIANT vArgArray[1];
DISPPARAMS DispParams;
DISPID dispidNamed;
HRESULT hr;
VariantInit(&vResult); // Call Workbooks::Open() - 682 >> Gets pXLWorkbook
vArgArray[0].vt = VT_BSTR;
vArgArray[0].bstrVal = SysAllocString(pszWorkBookPath);
DispParams.rgvarg = vArgArray;
DispParams.rgdispidNamedArgs = &dispidNamed;
DispParams.cArgs = 1;
DispParams.cNamedArgs = 0;
hr=pXLWorkbooks->Invoke(682,IID_NULL,lcid,DISPATCH_METHOD,&DispParams,&vResult,NULL,NULL);
SysFreeString(vArgArray[0].bstrVal);
if(FAILED(hr))
return NULL;
else
return vResult.pdispVal;
}
IDispatch* GetDispatchObject(IDispatch* pCallerObject, DISPID dispid, WORD wFlags, LCID lcid)
{
DISPPARAMS NoArgs = {NULL,NULL,0,0};
VARIANT vResult;
HRESULT hr;
VariantInit(&vResult);
hr=pCallerObject->Invoke(dispid,IID_NULL,lcid,wFlags,&NoArgs,&vResult,NULL,NULL);
if(FAILED(hr))
return NULL;
else
return vResult.pdispVal;
}
|