简体   繁体   中英

Open an Excel document with C++ builder

I am trying to open an existing Excel document using C++ builder. but I am having an exception :

Project project1.exe raised exception class EOlSysError with message "incorrect type of variable"

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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