Solved

OLE Application Excel Intercept (and deal with) Window messages

Posted on 2004-04-06
5
396 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
How to renew a Delphi rad-studio licence? 5 77
PHP preg_replace code convert to Delphi 14 80
Delphi: making a BW image transparent 10 91
IP without any Dots 1 54
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

749 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