View Full Version : Using Activator.GetObject with MS Word


Evan Stone
02-14-2006, 12:41 AM
Hi,

What I'd like to do is attach to an instance of Word (if it's running), and
if it's not running then create a new instance of the application. To that
end, I was wondering if the following was a valid use of the
Activator.GetObject() method:

public Word.ApplicationClass CreateWordApplicationInstance()
{

Type wordType = Type.GetTypeFromProgID("Word.Application");
Word.ApplicationClass app = null;

try
{
app = Activator.GetObject(wordType, "");
}
catch(Exception rex)
{
app = new Word.ApplicationClass();
}

return app;

}

Thanks!

evan k. stone | software engineer
----------------------------------------
santa rosa, ca, usa

Evan Stone
02-14-2006, 08:15 PM
OK... How about this question: If I was going to try to get a running
instance of MS Word with Activator.GetObject, what should go into the URL
parameter of the GetObject method?

> Type wordType = Type.GetTypeFromProgID("Word.Application");
> Word.ApplicationClass app = null;
> app = Activator.GetObject(wordType, "[what goes here?]");

Thanks!

evan k. stone | software engineer
----------------------------------------
santa rosa, ca, usa

Cindy M -WordMVP-
02-15-2006, 05:23 PM
Hi Evan,

I've never encountered "Activator". But I have successfully used
Word.Application wdApp = (Word.Application)
System.Runtime:InteropServices.Marshal.GetActiveObject("Word.Application");

> OK... How about this question: If I was going to try to get a running
> instance of MS Word with Activator.GetObject, what should go into the URL
> parameter of the GetObject method?
>
> > Type wordType = Type.GetTypeFromProgID("Word.Application");
> > Word.ApplicationClass app = null;
> > app = Activator.GetObject(wordType, "[what goes here?]");
>

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :-)