将范围从VBA传递到C ++ [英] Pass a Range from VBA to C++

查看:41
本文介绍了将范围从VBA传递到C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将范围从VBA发送到VSTO c ++ DLL.如果我使用的是Excel 2010 64位代码,则下面的代码可以正常工作,但是将其用于excel 2016时出现错误,有人知道如何解决吗?

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;
}

和其他cpp文件

#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

defFile

Library "Teste"
EXPORTS
RangeTest

推荐答案

我不确定这是否与Excel :: RangePtr完全相同,但其目的相同,我希望能提出一些意见.

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;

}

这篇关于将范围从VBA传递到C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆