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

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
LVL 1
alanjbrownAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Russell LibbySoftware Engineer, Advisory 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;

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
alanjbrownAuthor 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
0
Russell LibbySoftware Engineer, Advisory Commented:

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

Russell
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

alanjbrownAuthor Commented:
Thanks, I understand now.

Will get back to you soon.

Alan
0
alanjbrownAuthor 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
0
Russell LibbySoftware Engineer, Advisory Commented:
No problem with the delay, glad it works well for you.

Russell
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.