Home All Groups Group Topic Archive Search About

Determining if MS Word is running through VB code

Author
11 Sep 2006 2:44 PM
njuneardave
Hi,  I have an application that will run a method depending on whether
or not there is an instance of MS Word currently open.

How do I determine whether or not an executable from another program is
running?  I am new to VB, but I suspect that this should definitely be
5 lines or less.

Author
11 Sep 2006 2:57 PM
Tom Shelton
njuneardave wrote:
> Hi,  I have an application that will run a method depending on whether
> or not there is an instance of MS Word currently open.
>
> How do I determine whether or not an executable from another program is
> running?  I am new to VB, but I suspect that this should definitely be
> 5 lines or less.

Well...  One way with VB.NET would be to use the GetObject method.
Word is an automation server - and if it's running GetObject will
return a valid reference to it.  If word isn't running then GetObject
will throw an exception.  So, you can actually do this in one line of
code:

Dim wordObject As Object = GetObject (Class:="Word.Application")

--
Tom Shelton
Author
11 Sep 2006 3:08 PM
njuneardave
Cool, Tom.  Thanks!!

I guess I should have used a little less abstraction in this case,
though.  Using MS Word was a prelim test.  I actually want to test for
the existence of an executable that I made myself called,
"Spectrum.exe".    I first opened the application using:

System.Diagnostics.Process.Start("Spectrum.exe")


Now, later in the code, I need to test if my Spectrum.exe is currently
running.  Sorry about the abstraction.... I didn't think that it would
play a major role =P  Anyway, any new tips?


Thanks so much for the help!



Tom Shelton wrote:
Show quoteHide quote
> njuneardave wrote:
> > Hi,  I have an application that will run a method depending on whether
> > or not there is an instance of MS Word currently open.
> >
> > How do I determine whether or not an executable from another program is
> > running?  I am new to VB, but I suspect that this should definitely be
> > 5 lines or less.
>
> Well...  One way with VB.NET would be to use the GetObject method.
> Word is an automation server - and if it's running GetObject will
> return a valid reference to it.  If word isn't running then GetObject
> will throw an exception.  So, you can actually do this in one line of
> code:
>
> Dim wordObject As Object = GetObject (Class:="Word.Application")
>
> --
> Tom Shelton
Author
11 Sep 2006 3:37 PM
Tom Shelton
njuneardave wrote:
Show quoteHide quote
> Cool, Tom.  Thanks!!
>
> I guess I should have used a little less abstraction in this case,
> though.  Using MS Word was a prelim test.  I actually want to test for
> the existence of an executable that I made myself called,
> "Spectrum.exe".    I first opened the application using:
>
> System.Diagnostics.Process.Start("Spectrum.exe")
>
>
> Now, later in the code, I need to test if my Spectrum.exe is currently
> running.  Sorry about the abstraction.... I didn't think that it would
> play a major role =P  Anyway, any new tips?
>
>
> Thanks so much for the help!

Sure, save the reference to the process that is returned by
Process.Start.  Then you can test and see if it is running at
anytime...  Look at the process classes HasExited property.  It is also
possible to hook an event handler to the process so that your
application get's notified when the process exits.

--
Tom Shelton
Author
11 Sep 2006 5:32 PM
njuneardave
Oh, I like that way better, Tom.  Thanks for the advice!  I appreciate
you.



Tom Shelton wrote:
Show quoteHide quote
> njuneardave wrote:
> > Cool, Tom.  Thanks!!
> >
> > I guess I should have used a little less abstraction in this case,
> > though.  Using MS Word was a prelim test.  I actually want to test for
> > the existence of an executable that I made myself called,
> > "Spectrum.exe".    I first opened the application using:
> >
> > System.Diagnostics.Process.Start("Spectrum.exe")
> >
> >
> > Now, later in the code, I need to test if my Spectrum.exe is currently
> > running.  Sorry about the abstraction.... I didn't think that it would
> > play a major role =P  Anyway, any new tips?
> >
> >
> > Thanks so much for the help!
>
> Sure, save the reference to the process that is returned by
> Process.Start.  Then you can test and see if it is running at
> anytime...  Look at the process classes HasExited property.  It is also
> possible to hook an event handler to the process so that your
> application get's notified when the process exits.
>
> --
> Tom Shelton