Automation to Excel in C++ using Direct-To-COM

Posted on 2000-03-06
Last Modified: 2008-02-20
I need to export some tabular data to Excel.8.
So, I am writing :

#import <D:\Program Files\Microsoft Office\Office\EXCEL8.OLB>
boolean MyClass::menuExportTableToExcel()

    using namespace Excel;
   _ApplicationPtr pXL;
            pXL->Visible = VARIANT_TRUE;
            WorkbooksPtr pBooks = pXL->Workbooks;
               _WorkbookPtr pBook  = pBooks->Add((long)xlWorksheet);

            _WorksheetPtr pSheet = pXL->ActiveSheet;
            pSheet->Name = "Selling Sheet";

            for (int col_i = firstCol; col_i < listt->count(); col_i++)
(*)              pSheet->Cells(1,col_i-firstCol+1)->Value = //something;


Compiler issues 4 errors on line (*).
      - term does not evaluate to a function
      - left of '->Value' must point to class/struct/union

I saw in *.tli, tlh files but unfortunatly it did not help

In 2 words, I need in loop put data in EXCEL through _WorkbookPtr. I need something
like that in VB :

For rwIndex = 1 to 4
    For colIndex = 1 to 10
        With Worksheets("Sheet1").Cells(rwIndex, colIndex)
             .Value = "shalom"
        End With
    Next colIndex
Next rwIndex


Question by:arikka
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2

Expert Comment

ID: 2589557
Without getting into too much detail, it's nothing to do with COM.

pSheet->Cells(1,col_i-firstCol+1)->Value = //something

It seems to think that you're calling
a function Cells()

Cells(1, col_i-firstCol+1)

you probably want square brackets (I'm assuming that Cells is actually a multidimensional array)

Cells[1, col_1-firstCol+1]

RE: - left of '->Value' must point to class/struct/union

This is pointing to the fact that you aren't getting what you want before ->Value

If the idea is to get something back from a function (e.g. Cells() actually returns a cell) you'll have to do

Foo* pCell = Cells(1,col_1-firstCol+1);

and then pSheet->pCell->Value

or whatever.


Author Comment

ID: 2590989
Adjusted points to 100

Author Comment

ID: 2590990
I've tried it before, it did not help...
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.


Author Comment

ID: 2591304
Please do not ask me, I already found the soultion !!!

    Excel::RangePtr pRange;
      CString csRange;
      for (int i = 0; i < nLines; i++) // send each line to Excel
        { // one value at at time (till speed becomes an issue
        for (int j = 0; j < sizeof(cBreaks) / sizeof(cBreaks[0]);j++)
          csRange.Format("%c%d", 'A'+j, i+1); // "A1, A2... B1, B2," etc
          pRange = pSheet->Range[(LPCTSTR)csRange];
          int idxChar = i*nLineLength+cBreaks[j]+1; // idx 1 past prev. NUL
          if (idxChar > nLength) // avoid overruns
          _variant_t tVar(&pC[idxChar]); // point to NUL-terminated string

Expert Comment

ID: 2676280
Community Support has reduced points from 100 to 0

Accepted Solution

ianB earned 0 total points
ID: 2676281
Closing question and keeping answer.

Community Support @ Experts Exchange

Featured Post

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

615 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