Solved

OLE Application Excel Intercept (and deal with) Window messages

Posted on 2004-04-06
5
395 Views
Last Modified: 2010-04-06
Using OleApplication in Delphi to use Excel.

Have to append data as it appears, to existing sheets if they exist, without operator intervention.  

For new workbook all is fine, but if the workbook is already there, Excel generates a windows message which the Delphi program should deal with - such as 'This file already exists - Do you want to save?'  appears.  

How would you trap these messages to make an automated system?
0
Comment
Question by:mornao
  • 2
5 Comments
 

Expert Comment

by:TomGrills
ID: 10764649
try setting the DisplayAlerts property to FALSE

    XLApplication.DisplayAlerts:=false;
0
 
LVL 1

Author Comment

by:mornao
ID: 10764683
While that turns off all mesages, would like to trap then and decide based on what the message says.
0
 
LVL 1

Author Comment

by:mornao
ID: 11481588
Still no answer to this one - and as a result had serious Americanisation issues.
0
 
LVL 3

Accepted Solution

by:
SuperUt earned 100 total points
ID: 11489671
I have solved a similar problem as follows:
I create a helper thread that performs the desired action on the COM object that might pop up a window.
While that's going on, I look for a pop-up dialog and I let my program press the OK button to close the dialog.

Here is the code that I got to work after some trial and error:

Parameter s is the caption of the pop up window I look for,
TOms is the max. time I wait for the pop up to appear since the pop up window doesn't always show up.

procedure PressOKButton(Sender: TObject; s: AnsiString; TOms: integer);
var
   h1, h2: thandle;
   tag: tagInput;
   TimeOut: TDateTime;
begin
   //Enum windows and press OK button
   TimeOut:= now + TOms/ MSecsPerDay;
   Repeat
      h1:= FindWindowEx( 0, 0, Nil, pAnsiChar( s));
      if h1 = 0 then
         sleep( 50);
   until ( h1 <> 0) OR( now > TimeOut);

   if h1 <> 0 then begin
      h2:= FindWindowEx( h1, 0, Nil, 'OK' );

      if h2 <> 0 then begin
         SetActiveWindow ( h1);
         Windows.SetFocus( h1);
         Windows.SetFocus( h2);  //OK button
sleep(20);
         tag.Itype:= INPUT_KEYBOARD;
         tag.ki.wVk:= 0;
         SendInput( 1, tag, sizeof( tag));
sleep(20);
         SendMessage( h2, BM_CLICK, 0, 0);
sleep(20);
      end;
   end;
end;





Here is a sample of the main thread

procedure BO_Refresh;
begin
   FHelperThread:= Nil;
   try
         FHelperThread:= tHelperThread.Create( True );
         FHelperThread.FreeOnTerminate:= False;
         FHelperThread.Resume;
         PressOKButton( Nil, 'Select Values', 20000);

         FHelperThread.WaitFor;
   finally
      FHelperThread.Free;
   end;
end;



This is the only code of the helper thread:
//------------ Helper thread
procedure tHelperThread.Execute;
begin
   BusObjDoc.Refresh;
   Terminate;
end;
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi - replicating a form 8 81
Downloading email attachments 2 79
creating threads in delphi 1 130
How to insert .png images to TClientDataSet .cds file? 2 29
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question