2
votes

Delphi Tokyo, Office 2016. I have an existing app, been running great for a while. I went this morning to run the application, and all the sudden it is throwing an error. I open the source code and step through it. It is throwing the following error:

'Project ... raised exception class EIntfCastError with message 'Interface not supported'.

The line of code causing the issue is

oExcel := CreateOleObject('Excel.Application') as ExcelApplication;

The source code has not changed. The issue was first found in the executable. Excel appears to be working fine. There is one other fact, which may or may not be relevant... Every time I start Outlook I get a popup Error message 'Failed to load the elevation application'. This started approximately 1 month ago, and to my knowledge, I don't believe I have run my application since I started receiving this message. I cannot find any information about this, but it does not seem to effect Outlook. (I have performed both a Quick Repair as well as an Online Repair for MS Office 2016, but neither effort changes anything.) When I look at Outlook Plugins, I do NOT have any plugIn called 'Elevation'.

The Outlook issue may or may not be related to the Excel issue.

What is going on here? How do I debug this further?

1
Have you tried splitting your oExcel := CreateOleObject('Excel.Application') as ExcelApplication into two, i.e. doing the CreateOleObject and as ExcelApplication into two separate statements, so you can see which one is causing the exception? It is most likely the second, but better to be sure.MartynA
@Martyn. Can't get the syntax right..... This works/runs.. myApp:= CreateOleObject('Excel.Application'); ... if myApp is defined as OleVariant. How do I then cast it to type ExcelApplication? I have tried a couple things and the compiler complains.user1009073
The way I have done this is oExcel := IDispatch(oExcel) as ExcelApplicationMartynA
I have NO idea why, but taking the one line (which used to work just fine) and splitting it up into 2 lines solved the problem. If you have any ideas WHY, please let me know. Submit as answer and I will accept... Any idea what is causing the elevation issue? Thank you.user1009073
I've posted my previous comment as an answer. Sorry, but I have no idea aboyt the Outlook issue and anyway think you should raise that in a new q if you need an answer about that.MartynA

1 Answers

2
votes

I would split you code into two steps, one to create the Ole object and the other to extract the ExcelApplication interface. This way, you can easily identify which of the two steps is geneerating the exception. Something like this

var
  oExcel : OleVariant;

...
  oExcel := CreateOleObject('Excel.Application');
  oExcel := IDispatch(oExcel) as ExcelApplication;

To be honest, when I first saw your code, I was surprised it compiled in the first place, but I checked it and it did.

However, there is really not a lot of point in assigning the ExcelApplication interface back to an OleVariant. It would be better to assign it to an interface variable and use that to make use of the early-binding, type safety and code-completion that is available in the IDE. So I would do something like this, instead:

var
  oExcel : OleVariant;
  Excel : ExcelApplication;

...
  oExcel := CreateOleObject('Excel.Application');
  Excel := IDispatch(oExcel) as ExcelApplication;
  //  then use the Excel interface instead of oExcel.

Tbh, I am not expert enough in COM/Ole to be sure why splitting the original single step into two makes the difference between it not working and working. Perhaps a COM expert will chip in on that.