If there's a better newsgroup to post this in, please let me know. I'm
trying to start MS Word and get notification when it exits. The basic case
works fine, but if Word or Outlook is already running - or is started while
my instance of Word is still open, I get problems. Here's my basic code:
ProcessStartInfo psi = new ProcessStartInfo( );
psi.FileName = "<path to executable>\winword.exe";
psi.UseShellExecute = false;
psi.Arguments = String.Format( "\"{0}\"", fileName );
Process process = new Process( );
process.EnableRaisingEvents = true;
process.Exited += new EventHandler(process_Exited);
process.SynchronizingObject = myForm;
process.StartInfo = psi;
process.Start( );
with psi.UseShellExecute = false:
1) If Outlook/Word is already running, Process.Exited fires immediately. It
appears it changes to the existing WINWORD process.
2) If this runs and Outlook/Word is started after I've run this (the
launched instance of Word is still open), it appears it hijacks my instance
of Word and I don't get notification until Outlook/Word is closed
with psi.UseShellExecute = true:
1) If Outlook/Word is already running, Process.Exited never fires
2) If this runs and Outlook/Word is started after running this, same as #2
above
How can I work around this? I'm working on an application that pulls
documents from a SQL Server database and displays them. If changes are made
to the document, I want to be able to update it after it has been closed or,
if they close my form that launched Word, warn them that any changes made
won't be saved (similar to the way Outlook works when you open a document in
a message). Can I force my instance of Word to launch in a separate process?
--Brian
Brian Kavanaugh - 20 Jul 2006 13:35 GMT
Forgot to include I'm using the 1.1 Framework, and this is Office 2003.
> If there's a better newsgroup to post this in, please let me know. I'm
> trying to start MS Word and get notification when it exits. The basic case
[quoted text clipped - 36 lines]
>
> --Brian
Pritcham - 20 Jul 2006 19:37 GMT
Hi
Is it important that there's only one instance of Word (or whatever)
running? The reason I ask is that I'm in the middle of experimenting
with word automation etc and the following code seems (so far) to work
fine regardless of whether there's an instance of word running already
(granted, it always creates a new instance but in my case that's
absolutely no problem)
Imports Microsoft.Office.Interop.Word
Public Class Form1
Dim WithEvents wrd As New Microsoft.Office.Interop.Word.Application
Dim WithEvents doc As New Microsoft.Office.Interop.Word.Document
Private Sub btnOpenWord_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles btnOpenWord.Click
wrd.Documents.Open("Path\MyFileName.doc")
doc = wrd.Documents.Item(wrd.Documents.Count)
wrd.Visible = True
Private Sub wrd_Quit() Handles wrd.Quit
MsgBox("Word quit")
End Sub
Private Sub doc_Close() Handles doc.Close
MsgBox("Doc closed")
End Sub
I know it's a bit basic but it seems to do the job for me (unless I'm
missing a trick somewhere!!)
Martin
> Forgot to include I'm using the 1.1 Framework, and this is Office 2003.
>
[quoted text clipped - 38 lines]
> >
> > --Brian
Brian Kavanaugh - 20 Jul 2006 20:12 GMT
That only works if the client has MS Word 2003 installed, correct? Ours is a
remoted application where I cannot guarantee that our end users have Word
2003 (or any version) installed. I didn't include the code that does it, but
we search the Registry for the program associated with the document's
extension and launch that program. That's what psi.FileName is actually set
to.
> Hi
>
[quoted text clipped - 79 lines]
>> >
>> > --Brian
Pritcham - 20 Jul 2006 21:14 GMT
Ah - I see, sorry I couldn't help (thought my 'solution' may be a bit
simplistic but worth a try hey!)
Hopefully someone else will pipe up with a more relevant/useful answer!
Cheers
Martin
> That only works if the client has MS Word 2003 installed, correct? Ours is a
> remoted application where I cannot guarantee that our end users have Word
[quoted text clipped - 86 lines]
> >> >
> >> > --Brian