Tulio Carnelossi
Tulio Carnelossi

Reputation: 151

Pass a Range from VBA to C++

I'm trying to send a Range from a VBA to a VSTO c++ DLL. The code below works fine if I'm using Excel 2010 64 bits but I get an error when I use it for excel 2016, any one knows how to solve it?

#include <fstream>
#include <iostream>
#include "ExcelImports.cpp"
#include <vector>

using namespace std;
ofstream outputFile("outputFile.txt");

double __stdcall RangeTest(Excel::RangePtr &pRange, long N1, long N2 )
{
long sum = 0;

for (int i = 1; i <= N1; i++)
{
    for (int j = 1; j <= N2; j++)
    {

        outputFile << (((Excel::RangePtr)pRange->Item[i][j])->Value).dblVal << " ";


    }

    outputFile << endl;

}
 return 0;
}

and the Other cpp file

#ifndef ExcelImports_cpp
#define ExcelImports_cpp


#import "C:\Program Files\Common Files\Microsoft Shared\OFFICE14\MSO.dll" rename ("DocumentProperties","DocumentsPropertiesXL") rename("RGB","RGBXL")
#import "C:\Program Files\Common Files\Microsoft Shared\VBA\VBE6EXT.OLB" 
#import "C:\Program Files\Microsoft Office\Office14\EXCEL.EXE" rename ("DialogBox","DialogBoxXL") rename("RGB","RGBXL") rename ("DocumentProperties","DocumentsPropertiesXL") rename ("ReplaceText","ReplaceTextXL") rename ("CopyFile","CopyFileXL") no_dual_interfaces


#endif

the defFile

Library "Teste"
EXPORTS
RangeTest

Upvotes: 1

Views: 860

Answers (2)

Tulio Carnelossi
Tulio Carnelossi

Reputation: 151

I'm not sure if this is exactly the same as the Excel::RangePtr but it works the same purposes, I'de appreciate some comments.

#include <OAIdl.h> // for VARIANT, BSTR etc

double __stdcall RangeTest(VARIANT * x)
{

int cols = 0.0;
int rows = 0.0;

rows = x->parray[0].rgsabound[1].cElements;
cols=x->parray[0].rgsabound[0].cElements;

ofstream outputFile("outputFile.txt");
long c= 0;

for (int i = 0; i < rows; i++)
    {

     for (int j = 0; j < cols ;j++)
       {

         outputFile << ((((tagVARIANT *)(*(x->parray)).pvData))[i + j*rows]).dblVal << " ";

       }

        outputFile << endl;

     }

return 0;

}

Upvotes: 2

IR_IR
IR_IR

Reputation: 184

Did you try change

RangePtr

to

Range

Upvotes: 0

Related Questions