• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 839
  • Last Modified:

VC++ excel Variant Range

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
null_pointer
Asked:
null_pointer
1 Solution
 
senohpCommented:
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
 
null_pointerAuthor Commented:
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
 
griesshCommented:
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
 
NetminderCommented:
Per recommendation comment force/accepted by

Netminder
Community Support Moderator
Experts Exchange
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now