将范围从VBA传递到C ++
我想从VBA发送范围到VSTO c + + DLL。 下面的代码工作正常,如果我使用Excel 2010 64位,但我得到一个错误,当我使用它的Excel 2016,任何人都知道如何解决它?
#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
你有没有尝试改变
RangePtr
至
范围
我不确定这是否与Excel :: RangePtr完全一样,但它的作用相同,我很欣赏一些评论。
#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;
}