简体   繁体   English

使用C ++构建器打开Excel文档

[英]Open an Excel document with C++ builder

I am trying to open an existing Excel document using C++ builder. 我正在尝试使用C ++构建器打开现有的Excel文档。 but I am having an exception : 但我有一个例外:

Project project1.exe raised exception class EOlSysError with message "incorrect type of variable" 项目project1.exe引发异常类EOlSysError,并显示消息“变量类型不正确”

Here is the code I'm using : 这是我正在使用的代码:

try {
    vMSExcel = Variant::GetActiveObject("Excel.Application");
}
catch (...) {
    vMSExcel = Variant::CreateObject("Excel.Application");
}

vMSExcel.OlePropertySet("Visible", true);

Variant vFileName = "C:\\Users\mi\\Desktop\\classeur1.xls";

vXLWorkbooks = vMSExcel.OlePropertyGet("Workbooks");
vXLWorkbook = vXLWorkbooks.OleFunction("Open", vFileName);

The exception is thrown in this line : 这行抛出异常:

vXLWorkbook = vXLWorkbooks.OleFunction("Open", vFileName);

Any help will be appreciated. 任何帮助将不胜感激。 Thank you 谢谢

PROBLEM FIXED : 解决的问题:

I changed the type of vFileName from Vaiant to String and added c_str() to OleFucntion as following 我将vFileName的类型从Vaiant更改为String,并将c_str()添加到OleFucntion,如下所示

vXLWorkbook = vXLWorkbooks.OleFunction( "Open",vFileName.c_str()); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM