Solved

VC++ excel  Variant Range

Posted on 2001-07-19
4
815 Views
Last Modified: 2012-06-27
How can i use the Variant data type in C++
i am selecting a range of values from the excel sheet
storing them in a variant data type
want to send this as a parameter to a C++ function
do some calculations on the same.

 

 


0
Comment
Question by:null_pointer
4 Comments
 
LVL 1

Accepted Solution

by:
senohp earned 50 total points
ID: 6300952
You can import the Excel ActiveX.dll in your VC++ for loading and saving the data, and do the complex calculation in C++.

The excel objects shown in VC++ will just like the VBA objects

Here are some codes :

// in excelh.cpp

#include "excelh.h"
#import "C:\Program Files\Microsoft Office\Office\MSO9.DLL"     implementation_only
#import "C:\Program Files\Common Files\Microsoft Shared\VBA\VBA6\VBE6EXT.OLB" implementation_only
#import "C:\Program Files\Microsoft Office\Office\excel9.olb" rename("DialogBox", "excelDialogBox") rename("RGB", "excelRGB") implementation_only

// in excelh.h
#include <atlbase.h>

#import "C:\Program Files\Microsoft Office\Office\MSO9.DLL"     no_implementation
#import "C:\Program Files\Common Files\Microsoft Shared\VBA\VBA6\VBE6EXT.OLB" no_implementation
#import "C:\Program Files\Microsoft Office\Office\excel9.olb" rename("DialogBox", "excelDialogBox") rename("RGB", "excelRGB") no_implementation


// the code that access to the activex
#include "excelh.h"

void PrintComError(_com_error &e) ;

int main()
{
   CoInitialize(NULL) ;

   try {
      Excel::_ApplicationPtr apps(__uuidof(Excel::Application)) ;
      Excel::_WorkbookPtr book ;
      Excel::_WorksheetPtr sheet ;

     
      book = apps->Workbooks->Add() ;
      sheet = book->ActiveSheet ;

      for (long i=1; i<=100; i++) {
         TCHAR szBuffer[100] ;
         wsprintf (szBuffer, "%d", i) ;
         sheet->Cells->Item[i][1L] = szBuffer ;
      }

      _variant_t vnull, vformat, vconflict ;

      vformat = (long)Excel::xlWorkbookNormal ;
      vconflict = (long)Excel::xlLocalSessionChanges ;
      book->SaveAs("c:\\temp\\test.xls",
                   vformat,                        // format
                   vnull,                          // password
                   vnull,                          // write-res password
                   vnull,                          // read-only recommended
                   vnull,                          // create backup ?
                   Excel::xlNoChange,              // access mode
                   vconflict,                      // conflict resolution
                   vnull,                          // add to MRU
                   vnull,vnull) ;                  // not used

      book->Close() ;
      apps->Quit () ;
   }
   catch(_com_error &e)
   {
      PrintComError(e) ;
   }
   
   CoUninitialize() ;

   return 0 ;
}

void PrintComError(_com_error &e)
{
    _bstr_t bstrSource(e.Source());
    _bstr_t bstrDescription(e.Description());
       
    // Print Com errors.  
    ATLTRACE("Error\n");
    ATLTRACE("\tCode = %08lx\n", e.Error());
    ATLTRACE("\tCode meaning = %s\n", e.ErrorMessage());
    ATLTRACE("\tSource = %s\n", (LPCSTR) bstrSource);
    ATLTRACE("\tDescription = %s\n", (LPCSTR) bstrDescription);
}

0
 
LVL 3

Author Comment

by:null_pointer
ID: 6411515
How can i use the Variant data type in C++
i am selecting a range of values from the excel sheet
storing them in a variant data type
want to send this as a parameter to a C++ function
do some calculations on the same.






0
 
LVL 11

Expert Comment

by:griessh
ID: 6832995
I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. Unless there is objection or further activity,  I will suggest to accept "senohp" comment(s) as an answer.

If you think your question was not answered at all, you can post a request in Community support (please include this link) to refund your points.
The link to the Community Support area is: http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0
 
LVL 5

Expert Comment

by:Netminder
ID: 6853058
Per recommendation comment force/accepted by

Netminder
Community Support Moderator
Experts Exchange
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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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 pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

864 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

21 Experts available now in Live!

Get 1:1 Help Now