I am trying to use the code below to open an .xlsx file from C++Builder in RAD Studio XE7:
#include "ComObj.hpp"
Variant Excel = CreateOleObject("Excel.Application");
Variant Books = Excel.OlePropertyGet("Workbooks");
Excel.OlePropertySet("Visible", true);
// An escape character is missing but the problem remains
Books.OleProcedure("Open", L"D:\1.xlsx"); // exception here
But the last line causes exception with message:
Project2.exe raised exception class EOleException with message 'Unfortunately, we were unable to find the file TRUE.xlsx. It may have been moved, renamed or deleted?'.
Screen with place where the source breaks
The code in Delphi seems to work fine:
uses ComObj;
var
Excel, Books: Variant;
begin
Excel := CreateOleObject('Excel.Application');
Books := Excel.Workbooks;
Excel.Visible := True;
Books.Open('D:\1.xlsx'); // code passes
end;
Does anyone know the solution?
Update1: The following code in VB also works fine:
Sub Button1_Click()
Dim xlApp As Excel.Application
Dim xlBooks As Excel.Workbooks
Set xlApp = CreateObject("Excel.Application")
Set xlBooks = xlApp.Workbooks
xlApp.Visible = True
xlBooks.Open ("D:\1.xlsx")
End Sub
Update2: Sending a raw string literal causes the same exception.
Books.OleProcedure("Open", uR"(D:\1.xlsx)");
It also doesn't seem to be an environment problem. I tested the example at several computers with no effect.
In C++ the backslash character is the escape character in string literals and so needs itself to be escaped. Instead of
you need to write
The error message is strange though. It is almost as though some conversion in the COM dispatch code interprets the
1
as a truth value and converts it to text. You could try passing the filename as aSystem::WideString
which might side-step the issue.What you are reporting seems almost too weird to be true though! I have to confess I'm having some trouble believing it because it is so outlandish.