Tuesday, July 28, 2009

BstrVals in C++... help!!?

I need to read multiple lines of data out of an EXCEL file.





_Application objApp;


_Workbook objBook;


Workbooks objBooks;


Worksheets objSheets;


_Worksheet objSheet;


Range objRange, range;


VARIANT ret;





objSheets = objBook. GetWorksheets();


objSheet = objSheets.GetItem (COleVariant((short)1));


objRange = objSheet.GetRange (COleVariant("B1"), COleVariant ("B1500"));


ret = objRange.GetValue();


long col = 2;


long row = thisManyTimes;





objRange = objSheet.GetRange (COleVariant("A1"), COleVariant ("AZ200"));


ret = objRange.GetValue();





COleSafeArray sa(ret);





long lNumRows;


long lNumCols;


sa.GetUBound (1, %26amp;lNumRows);


sa.GetUBound (2, %26amp;lNumCols);





long index[2];


VARIANT val;


int r, c;





index[0] = 2;


index[1] = 2;


sa.GetElement (index, %26amp;val);


partName = val.bstrVal;





When I come to this last line, however, I'm told that I have an "uncaught exception" (even though it's set up inside a try/catch block.





I'm very new to variants and bstrVals. Any tips???

BstrVals in C++... help!!?
Here's a good code example for what you're trying to do:





http://support.microsoft.com/kb/186122/E...





It looks like your for loop (in your follow up notes) has a problem Did you really want to initialize the counter to 2?
Reply:first of all.....girl programmer sounds so cute.......





but cutie this is not a place to do c++...i mean not such a big code.....no one is gonna read it and help you like that.........





i think go to some c++ forum like there are many of them on www.orkut.com....
Reply:i'm not really familiar with bstr, but it's ugly and horrible (which is not your fault...)





how do you know that "val" holds a string? Maybe it holds a different type, depending on the cell value or cell format.





you can check the type of "val" with val.vt





if (val.vt==VT_BSTR) --%26gt; now it's a string





if it's not a string, i guess you shouldn't use val.bstrVal.
Reply:Visit the following link may be it would be help full:





http://www.mstipsandtricks.com/





Cheers!


No comments:

Post a Comment