Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2004-11-03
6
Medium Priority
?
305 Views
Last Modified: 2010-04-05
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
0
Comment
Question by:alanjbrown
  • 3
  • 3
6 Comments
 
LVL 26

Accepted Solution

by:
Russell Libby earned 1000 total points
ID: 12487367
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
 
LVL 1

Author Comment

by:alanjbrown
ID: 12487658
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
 
LVL 26

Expert Comment

by:Russell Libby
ID: 12487678

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

Russell
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Author Comment

by:alanjbrown
ID: 12487779
Thanks, I understand now.

Will get back to you soon.

Alan
0
 
LVL 1

Author Comment

by:alanjbrown
ID: 12526757
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
 
LVL 26

Expert Comment

by:Russell Libby
ID: 12528951
No problem with the delay, glad it works well for you.

Russell
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

864 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