How to get process ID of a Word.Application object instance?

D

David Akerman

Hi,

Background
========
I've written a Web App in C# which controls a pool of Word Applications for
mailmerge purposes (not ideal using Word non-interactively I know, but I
have explored alternatives, and they all introduced various other problems,
leading me to discount them).

The main issue with this solution is that when there are problems with the
datasource has errors, as Word pops up a question, causes a hang in the
non-interactive session. I want to be able to trap this and deal with it.

My current approach is to make the web manager application have some kind of
time out facility, and allow a kill of the word process using the Process
ID.

Problem
======
How do i get the process ID when I start a Word Instance after creating a
new Word.Application using the PIA, e.g.
Word.Application wa = new Word.Application();

I have attempted to set Word.Application.Caption to something unique, then
use code as follows to scan through the Word processes, and check for a
matching MainWindowTitle:
// Set caption so we can match the instance with a process ID later
wa.Caption = "WordMerge" + i.ToString();

Process [] wordProcesses = Process.GetProcessesByName("WINWORD");
for (int i=0; i < wordProcesses.Length; i++)
{
Process p = wordProcesses;
String windowTitle = p.MainWindowTitle;
if (windowTitle.StartsWith("WordMerge"))
{
int serverNumber = p.MainWindowTitle[9];
((ManagedWordApp)_wordPool[serverNumber]).pid = p.Id;
}
}

The MainWindowTitle is currently "" for each Word Instance in the debugger.
I assumed that the Caption should set a Title for the Window, but maybe I am
misunderstanding things, or do I perhaps need to do some Interop code to
find the Window, and send a message to repaint?

Alternatively, how easy would it be to detect if Word pops up a dialog?

Any help and/or suggestions would be very much appreciated.

Regards,
David Akerman
JADe
(e-mail address removed) (remove _remove_this) in replies
please.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top