Solved

Reading Cell Values In Excel 2000 Using C++

Posted on 2007-11-15
2
277 Views
Last Modified: 2013-11-20
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
Comment
Question by:reidy_boy
2 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 200 total points
ID: 20289477
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
 
LVL 1

Author Closing Comment

by:reidy_boy
ID: 31409308
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
First Blank Cell in a range 7 34
Looking for macro to do version saving of workbooks in Excel 2016. 6 38
highlight duplicate entry 16 29
Excel macro runs twice 13 47
Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now