Solved

VC++ excel  Variant Range

Posted on 2001-07-19
4
812 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Per recommendation comment force/accepted by

Netminder
Community Support Moderator
Experts Exchange
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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.

771 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

14 Experts available now in Live!

Get 1:1 Help Now