?
Solved

OLE Application Excel Intercept (and deal with) Window messages

Posted on 2004-04-06
5
Medium Priority
?
406 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 300 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses
Course of the Month8 days, 19 hours left to enroll

764 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