How to ensure MsWord dialog boxes come to front when using Word via Automation

alanjbrown
alanjbrown used Ask the Experts™
on
I am performing mail merges from Word XP controlled from Delphi using TwordApplication. Some of the word templates have dialog boxes to prompt the user for additional information during the merge. I have set wd1.visible := true, however on some occasions the word dialog goes behind the Delphi application, the user is not aware that word is waiting for his input and after a long wait the user closes the application in task manager.

How do I ensure that the dialog always is on top?

Thanks in anticipation.

Alan
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Software Engineer, Advisory
Top Expert 2005
Commented:
Alan,

You could try something like the following to make sure that while the word app is running, it remains above your mainform. As a side note, its too bad that MS did not expose the hwnd directly (as they have done for Access, and later Excel versions) as this would have made things much simpler.

Hope this helps,
Russell

--------------------

type
  TForm1            = class(TForm)
     Button1:       TButton;
     oleWord:       TWordApplication;
     procedure      Button1Click(Sender: TObject);
     procedure      FormCreate(Sender: TObject);
  private
     // Private declarations
     FWordHwnd:     HWND;
  protected
     // Protected declarations
     procedure      WMActivate(var Message: TWMActivate); message WM_ACTIVATE;
  public
     // Public declarations
     procedure      WordBinding(Bind: Boolean);
  end;

var
  Form1: TForm1;

implementation
{$R *.DFM}

procedure TForm1.WMActivate(var Message: TWMActivate);
begin

  // Check word binding
  if IsWindow(FWordHwnd) and IsWindowVisible(FWordHwnd) then
     SetForegroundWindow(FWordHwnd)
  else
     inherited;

end;

procedure TForm1.WordBinding(Bind: Boolean);
begin

  // Check for (un)binding
  if Bind then
     // Get the window handle
     FWordHwnd:=FindWindow('OpusApp', nil)
  else
     // Clear the window handle
     FWordHwnd:=0;

  // If handle is valid then bring to front
  if IsWindow(FWordHwnd) and IsWindowVisible(FWordHwnd) then SetForegroundWindow(FWordHwnd);

end;

procedure TForm1.Button1Click(Sender: TObject);
begin

  // Connect
  oleWord.Connect;
  oleWord.Visible:=True;
 
  // Get the word application handle to keep it on top
  WordBinding(True);

end;

procedure TForm1.FormCreate(Sender: TObject);
begin

  // Set default handle
  FWordHwnd:=0;

end;

Author

Commented:
Thanks for the response.
I will not be able to try it untill tommorow as I am away from the office and don't have Delphi on my home PC. However what does OpusApp in FindWindow refer to?

Thanks
Alan
Russell LibbySoftware Engineer, Advisory
Top Expert 2005

Commented:

OpusApp refers the the window class that Microsoft for MS Word. (eg, the return value of GetClassName(hwnd, ...)).

Russell
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

Author

Commented:
Thanks, I understand now.

Will get back to you soon.

Alan

Author

Commented:
Russell

It appears to have done the trick. It has not gone wrong once during testing!

Thank you very much for the help. Sorry for the delay in responding.

Alan
Russell LibbySoftware Engineer, Advisory
Top Expert 2005

Commented:
No problem with the delay, glad it works well for you.

Russell

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial