Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel 2010 automation constants not working in Delphi XE7

I am trying to convert a program from Delphi 2010 to Delphi XE7 (32 bit/windows VCL). Code that used to work in D2010 that automated Excel via late binding OLE now gives the exception "Unable to set the Window state property of the application class" in Delphi XE7, when the app is maximized or minimized.

I am getting the constants xlmaximized and xlminimized from the ExcelXP unit that has these constants: xlMaximized = $FFFFEFD7; xlMinimized = $FFFFEFD4;

However, if I use the simple constant values -4137 and -4140 the program does work OK. I realize I must be doing something simple that is wrong.

Below is some sample code that illustrates the problem. I tested this and it works in Delphi 2010, but not in Delphi XE7. I suppose it must be something to do with how constants are handled in newer versions(?) Can someone point me in the right direction? Thanks in advance!

//XLA is a global variable of type OLEVariant;
//Program uses ComObj and ExcelXP unit

//This proc just runs or connects to Excel

procedure TForm3.RunExcelClick(Sender: TObject);

begin
  try
    xla := GetActiveOLEObject('Excel.Application');
  except
    try
      xla := CreateOleObject('Excel.Application');
    except
      on E: Exception do
        begin
          ShowMessage(E.Message);
        end;
    end;
    xla.Visible := true;
  end;
end;


procedure TForm3.MaxExcelClick(Sender: TObject);
begin
   //This is the code that gives the exception
   xla.windowstate := xlmaximized;  //-4137;  Works OK if use this number
end;

procedure TForm3.MinExcelClick(Sender: TObject);
begin
   //Or this.  I also get exceptions 
  xla.windowstate := xlminimized ; //-4140; Works OK if use this number
end;
like image 366
CHEAPS Avatar asked Dec 06 '14 03:12

CHEAPS


1 Answers

It's probably a data type issue. $FFFFEFD7 as a signed 32 bit integer is -4137 (and is what Excel expects). According to a quick goolge search Delphi Longint is signed 32 bit int, so maybe there is some type casting going on...

According to OP's own research, setting

System.Variants.DispatchUnsignedAsSigned := True;

solves it.

like image 110
chris neilsen Avatar answered Nov 15 '22 23:11

chris neilsen