Determining if MS Word is running through VB code

  • Thread starter Thread starter njuneardave
  • Start date Start date
N

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.
 
njuneardave said:
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")
 
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!
 
njuneardave said:
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.
 
Back
Top