Solved

VC++ excel  Variant Range

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

831 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