Solved

VC++ excel  Variant Range

Posted on 2001-07-19
4
822 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

860 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