• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 306
  • Last Modified:

Reading Cell Values In Excel 2000 Using C++

I have an Excel 2000 spreadsheet that I am trying to read data from using C++.  I have used the code in the following link to get me started and this works well.

http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_20642013.html

I am now trying to tweak the code so rather than create a new spreadsheet, I open an existing one (which I have figured out how to do), and then read the data in a given cell (which I can't do).  I have found numerous examples of Excel automation on the internet, but have yet to find one that shows how to do this.  Any help would be greatly appreciated.
0
reidy_boy
Asked:
reidy_boy
1 Solution
 
jkrCommented:
Take a look at http://www.codeproject.com/useritems/HOOK_COM.asp ("Get Cell Text of Excel"):


COleVariant VOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
 
    _Application objApp;
    Window objWin;
 
    Range objRange;
    VARIANT ret;
    
    CLSID clsid ;
    if (CLSIDFromProgID(OLESTR("Excel.Application.9"), &clsid) == NOERROR)
    {
     // Recherche du server Excel s'il est lance
     LPUNKNOWN lpUnk ;
     LPDISPATCH lpDispatch ;
     if (GetActiveObject(clsid, NULL, &lpUnk) == S_OK)
     {
      HRESULT hr = lpUnk->QueryInterface(IID_IDispatch, (LPVOID*)&lpDispatch) ;
      lpUnk->Release() ;
      if (hr == NOERROR)
      {
       objApp.AttachDispatch(lpDispatch, TRUE) ;
      }
     }
    }
 
    objWin = objApp.GetActiveWindow();
    objRange = objWin.GetActiveCell();
    ret = objRange.GetValue();
 
    BSTR txt;
    txt = ret.bstrVal;
    char *p=_com_util::ConvertBSTRToString(txt); 

Open in new window

0
 
reidy_boyAuthor Commented:
Took a bit of playing around with, but got there in the end.  I realised other examples weren't working because I had the wrong .olb version installed for Excel 2000, but this solution included the code that I sould have been using.  Excellent, thanks.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now