Solved

OLE Application Excel Intercept (and deal with) Window messages

Posted on 2004-04-06
5
385 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
Comment Utility
try setting the DisplayAlerts property to FALSE

    XLApplication.DisplayAlerts:=false;
0
 
LVL 1

Author Comment

by:mornao
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

763 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now