Solved

OLE Application Excel Intercept (and deal with) Window messages

Posted on 2004-04-06
5
402 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 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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

707 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