C# late binding for accessing WORD object model

  • Thread starter Thread starter Mystery Man
  • Start date Start date
M

Mystery Man

I am attempting to use late binding to interface with word (versions
97, 2000 and 2002). I have a few questions:

1. How do I detect if there is an instance of word that I can attach
to rather than starting a new instance.
2. How can I determine which version of Word is running. (I assume I
need this as there may be instances where the arguments to methods
change from version to version).
3. Does the MailMerge facility exist in Office 97.
4. Does anyone know of any C# samples that run on Word 97, 2000 and
2002.
5. Where can I get acces to the word object model for all of these
versions of word.
6. Below is sample code that creates an instance of word and then
opens a document. To access the Open method on the Documents, I have
to somehow get a Type from an object. I achieve this by calling
Type.GetHandle and then call Type.GetTypeFromHandle. My instinct was a
GetTypeFromObject method but none exists. Is this the best way to do
this as it seems very messy?


Type t = Type.GetTypeFromProgID("Word.Application");
object w = Activator.CreateInstance(t);
object docs = t.InvokeMember("Documents", BindingFlags.GetProperty,
null, w, null);

// Get a type from an object???????
RuntimeTypeHandle handle = Type.GetTypeHandle(docs);
Type tDocs = Type.GetTypeFromHandle(handle);

object doc = tDocs.InvokeMember("Open", BindingFlags.InvokeMethod,
null, docs, new Object[] {@"C:\logfile.txt"});
 
Back
Top