How to read/write excel checkbox status in c++ -
i working on issue requires me read/write status of checkbox in given cell in excel.
i know can access activex controls can accessed using com/ole. however, have not been able find solution problem. in fact, not sure if can access checkbox using row-column. studied properties of activex checkbox. found top , left properties, not row-column
i want ask if: 1. there direct way this. 2. if not, indirect way, getting top/left column somehow , getting row/column positions , comparing two. 3. there way same form controls?
thanks replies. found useful on msdn. have edited bit , sharing you.. support everyone
(forgive formatting)
autowrap() function msdn unedited:
hresult autowrap(int autotype, variant *pvresult, idispatch *pdisp, lpolestr ptname, int cargs...) { // begin variable-argument list... va_list marker; va_start(marker, cargs); if(!pdisp) { messagebox(null, "null idispatch passed autowrap()", "error", 0x10010); _exit(0); } // variables used... dispparams dp = { null, null, 0, 0 }; dispid dispidnamed = dispid_propertyput; dispid dispid; hresult hr; char buf[200]; char szname[200]; // convert down ansi widechartomultibyte(cp_acp, 0, ptname, -1, szname, 256, null, null); // dispid name passed... hr = pdisp->getidsofnames(iid_null, &ptname, 1, locale_user_default, &dispid); if(failed(hr)) { sprintf(buf, "idispatch::getidsofnames(\"%s\") failed w/err 0x%08lx", szname, hr); messagebox(null, buf, "autowrap()", 0x10010); _exit(0); return hr; } // allocate memory arguments... variant *pargs = new variant[cargs+1]; // extract arguments... for(int i=0; i<cargs; i++) { pargs[i] = va_arg(marker, variant); } // build dispparams dp.cargs = cargs; dp.rgvarg = pargs; // handle special-case property-puts! if(autotype & dispatch_propertyput) { dp.cnamedargs = 1; dp.rgdispidnamedargs = &dispidnamed; } // make call! hr = pdisp->invoke(dispid, iid_null, locale_system_default, autotype, &dp, pvresult, null, null); if(failed(hr)) { sprintf(buf, "idispatch::invoke(\"%s\"=%08lx) failed w/err 0x%08lx", szname, dispid, hr); messagebox(null, buf, "autowrap()", 0x10010); _exit(0); return hr; } // end variable-argument section... va_end(marker); delete [] pargs; return hr; }
here edited main() function:
int main() { // initialize com thread... coinitialize(null); // clsid our server... clsid clsid; hresult hr = clsidfromprogid(l"excel.application", &clsid); if(failed(hr)) { ::messagebox(null, "clsidfromprogid() failed", "error", 0x10010); return -1; } // start server , idispatch... idispatch *pxlapp; hr = cocreateinstance(clsid, null, clsctx_local_server, iid_idispatch, (void **)&pxlapp); if(failed(hr)) { ::messagebox(null, "excel not registered properly", "error", 0x10010); return -2; } // make visible (i.e. app.visible = 1) { variant x; x.vt = vt_i4; x.lval = 1; autowrap(dispatch_propertyput, null, pxlapp, l"visible", 1, x); } // make visible (i.e. app.visible = 1) { variant x; x.vt = vt_bstr; x.bstrval = ::sysallocstring (l"d:\\"); autowrap(dispatch_propertyput, null, pxlapp, l"defaultfilepath", 1, x); } // workbooks collection idispatch *pxlbooks; { variant result; variantinit(&result); autowrap(dispatch_propertyget, &result, pxlapp, l"workbooks", 0); pxlbooks = result.pdispval; } // call workbooks.add() new workbook... idispatch *pxlbook; { variant parm; parm.vt = vt_bstr; parm.bstrval = ::sysallocstring(l"a.xlsx"); variant result; variantinit(&result); autowrap(dispatch_method, &result, pxlbooks, l"open", 1,parm); pxlbook = result.pdispval; } // activesheet object idispatch *pxlsheet; { enter code here variant result; variantinit(&result); autowrap(dispatch_propertyget, &result, pxlbook, l"activesheet",0); if(result.pdispval != null) pxlsheet = result.pdispval; } //get shapes collection.. idispatch *pshapes; { variant result; variantinit(&result); autowrap(dispatch_propertyget, &result, pxlsheet, l"shapes",0); pshapes = result.pdispval; } int ncontrols; { variant result; variantinit(&result); autowrap(dispatch_propertyget, &result, pshapes, l"count",0); if(result.vt == vt_i4) ncontrols = result.lval; } for(int = 1; <= ncontrols; ++i) { idispatch * pshape; { variant x; x.vt = vt_i4; x.lval = i; variant result; variantinit(&result); autowrap(dispatch_method, &result, pshapes, l"item",1, x); pshape = result.pdispval; } idispatch *pcontrolformat = null; { variant result; variantinit(&result); autowrap(dispatch_propertyget, &result, pshape, l"type",0); // msoformcontrol if(result.lval == 8) { variant result0; variantinit(&result0); autowrap(dispatch_propertyget, &result0, pshape, l"formcontroltype",0); // xlcheckbox if(result0.lval == 1) { // range containing cell tested variant result2; variantinit(&result2); idispatch * range; { variant param1; param1.vt= vt_bstr; param1.bstrval = ::sysallocstring(l"f5"); variant result3; variantinit(&result3); autowrap(dispatch_propertyget, &result3, pxlsheet,l"range",1,param1); range = result3.pdispval; } // top, left, bottom, right of cell double top, left, bottom, right; { variant result4; variantinit(&result4); autowrap(dispatch_propertyget,&result4, range,l"top",0); top = result4.dblval; autowrap(dispatch_propertyget,&result4, range,l"left",0); left = result4.dblval; autowrap(dispatch_propertyget,&result4, range,l"height",0); bottom = top + result4.dblval; autowrap(dispatch_propertyget,&result4, range,l"width",0); right = left + result4.dblval; } range->release(); // top, left of checkbox float shapetop, shapeleft; { variant result5; variantinit(&result5); autowrap(dispatch_propertyget,&result5, pshape,l"top",0); shapetop = result5.fltval; autowrap(dispatch_propertyget,&result5, pshape,l"left",0); shapeleft = result5.fltval; } // hold of control format variant result1; variantinit(&result1); autowrap(dispatch_propertyget, &result1, pshape, l"controlformat",0); pcontrolformat = result1.pdispval; // check if checkbox if within range boundary. if yes, check it, else dont if((top <= shapetop) && (bottom >= shapetop) && (left <= shapeleft) && (right >= shapeleft)) { // perpare parameter variant parm; variant_bool t = variant_true;; parm.vt = vt_bool; parm.pboolval = &t; autowrap(dispatch_propertyput, null, pcontrolformat, l"value", 1, parm); } } } } if(pcontrolformat != null) pcontrolformat->release(); pshape->release(); } // save when tell excel quit... { variant x; x.vt = vt_i4; x.lval = 1; autowrap(dispatch_method, null, pxlbook, l"save", 0); } // wait user... ::messagebox(null, "all done.", "notice", 0x10000); // tell excel quit (i.e. app.quit) autowrap(dispatch_method, null, pxlapp, l"quit", 0); // release references... pshapes->release(); pxlsheet->release(); pxlbook->release(); pxlbooks->release(); pxlapp->release(); // uninitialize com thread... couninitialize(); return 0; }
for further details:
http://support.microsoft.com/kb/216686
http://msdn.microsoft.com/en-us/library/office/bb149081%28v=office.12%29.aspx
Comments
Post a Comment