Jump to content
Valter

Undeclared identifier but the code compiles

Recommended Posts

Hello everybody.

I wrote a management software with Delphi 10 Seattle and recently I was asked to automate the updating of an Excel file after issuing an invoice.

I access the file, find the first blank line and fill in the fields.

Since this is the only operation required, I don't use any add-ons.

This is part of the cleaned up code:

ExcelApplication := CreateOleObject('Excel.Application');
ExcelWorkbook := ExcelApplication.Workbooks.Open(ExcelFileName);
ExcelWorksheet := ExcelWorkbook.WorkSheets[1];
ExcelWorksheet.Select;
ExcelWorksheet.Cells[i,2] := cod;
ExcelWorksheet.Cells[i+1,2].Select; // Undeclared identifier 'Select'
ExcelWorkbook.Save;

The customer requests that the cell following the one inserted is selected when opening the Excel file.

Despite the undeclared identifier error, the code compiles and the application works correctly.

I specify that in typing this code I have no self-completion by the IDE.

Is it possible that some units are missing? I searched the net but couldn't find any suggestions.

 

Share this post


Link to post

What is the exact variable type of ExcelApplication and ExcelWorkbook?

I would guess Variant or OleVariant, and in that case, a technique called "late binding" is used. It both explains why you don't have code-completion, and that the code compiles even without the compiler knowing about the specific available properties and methods. Behind the scenes, the operations you do (Cells, Select and Save in this case) are resolved at runtime on the 'living' objects using hidden 'IDispatch' interfaces that list the available properties and methods by name. It was invented by Microsoft to make dynamic scripting languages work (like VBScript, and actually also something called "JScript" because they weren't allowed to call it JavaScript, but that's another story). And Delphi has excellent support for this (but a downside is it works so good you don't see anything of it and can't access it, need to get the sequence of calls exactly right and need to debug on live objects.)

Edited by stijnsanders
  • Thanks 1

Share this post


Link to post

Yes, they are Variants.
Ok, thank you for the clarification.

It is boring to have this error message but I can live with it 🙂

Thanks again.

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×