Word Automation

D

Daniel

Hello,

i have a problem with the word automation from c#. First,
i want to mention, that i don't have any dependencies from
word in my c#-project, i want to use the system.reflection
model to handle the automation.
So, i'm using the following code to create a new word
document:

---Code---
object objApplication;
object objDocuments;
object objDocument;

objApplication = System.Activator.CreateInstance(
Type.GetTypeFromProgID("Word.Application"));

objDocuments = objApplication.GetType().InvokeMember
("Documents", BindingFlags.Default |
BindingFlags.GetProperty, null, objApplication , null);

objDocument = objDocuments.GetType().InvokeMember("Add",
BindingFlags.Default | BindingFlags.InvokeMethod, null,
objDocuments, null);
---End of Code---

This works fine. But several properties cannot be accessed
like that. For example, when i try to get the
properties "BuiltInDocumentProperties"
or "CustomDocumentProperties" like the "Documents"-
property in the example above, i get an exception with the
text "Unknown name.".

Am i doing anything wrong? Is it possible to get all
properties with the system.reflection model?

Thanks in advance for your help.

Regards,
Daniel
 
N

Nicholas Paldino [.NET/C# MVP]

Daniel,

Is it possible that the version of word that you are running this on
doesn't support these properties?

For this kind of work, I would recommend using a separate assembly in
VB, and use late-bound calls (they are much easier to use in VB as opposed
to reflection).

Also, you should be aware of all the references that you might be
leaking when using automation like this.

For example, when you get the Documents property, you are not releasing
the object. When you end up releasing the application object (if at all),
then the Documents object is still around, and holds a reference to the
Application, and the program will be running in the background.

Hope this helps.
 
G

Guest

I'm using Office XP. In the Word-VB-Editor, i have free
access to the properties that produce the exception in
C#.NET.

How can i release the references i created? Do i have to
call the Dispose()-method?

I'm using the reflection-model, because my assembly should
also work on a machine, where no office is installed
(because its providing other functions, too).
If i use early binding in this case (add the word
reference to my c#-project), the assembly would not load,
if the word references are missing on the clients
computer, would it?

Thanks,
Daniel
-----Original Message-----
Daniel,

Is it possible that the version of word that you are running this on
doesn't support these properties?

For this kind of work, I would recommend using a separate assembly in
VB, and use late-bound calls (they are much easier to use in VB as opposed
to reflection).

Also, you should be aware of all the references that you might be
leaking when using automation like this.

For example, when you get the Documents property, you are not releasing
the object. When you end up releasing the application object (if at all),
then the Documents object is still around, and holds a reference to the
Application, and the program will be running in the background.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hello,

i have a problem with the word automation from c#. First,
i want to mention, that i don't have any dependencies from
word in my c#-project, i want to use the system.reflection
model to handle the automation.
So, i'm using the following code to create a new word
document:

---Code---
object objApplication;
object objDocuments;
object objDocument;

objApplication = System.Activator.CreateInstance(
Type.GetTypeFromProgID("Word.Application"));

objDocuments = objApplication.GetType().InvokeMember
("Documents", BindingFlags.Default |
BindingFlags.GetProperty, null, objApplication , null);

objDocument = objDocuments.GetType().InvokeMember("Add",
BindingFlags.Default | BindingFlags.InvokeMethod, null,
objDocuments, null);
---End of Code---

This works fine. But several properties cannot be accessed
like that. For example, when i try to get the
properties "BuiltInDocumentProperties"
or "CustomDocumentProperties" like the "Documents"-
property in the example above, i get an exception with the
text "Unknown name.".

Am i doing anything wrong? Is it possible to get all
properties with the system.reflection model?

Thanks in advance for your help.

Regards,
Daniel


.
 
N

Nicholas Paldino [.NET/C# MVP]

Daniel,

In order to release the references, you would have to pass the object to
the static ReleaseComObject method on the Marshal class for EACH object you
exposed. SO, when you get the Documents property, thats one object you have
to release, and then any property you get from that which returns an object
(collections included) is another, and so on, and so on.

The reason I recommended VB for these calls is that you can declare an
instance of type object, and then just make calls like this:

// The application object.
Dim application As Object =
System.Activator.CreateInstance(Type.GetTypeFromProgID("Word.Application"));

// Get the documents collection.
Dim documents As Object = application.Documents;

Granted, you won't get Intellisense, but it sure as hell beats making
all the calls through the Reflection API yourself.

If you use an early bound call, you are going to bind to the Runtime
Callable Wrapper, which your assembly will have no problem loading (because
you have to distribute it). If Word is not on the machine, then when you
try and create the instance, you will end up getting an exception when you
try and create the object.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I'm using Office XP. In the Word-VB-Editor, i have free
access to the properties that produce the exception in
C#.NET.

How can i release the references i created? Do i have to
call the Dispose()-method?

I'm using the reflection-model, because my assembly should
also work on a machine, where no office is installed
(because its providing other functions, too).
If i use early binding in this case (add the word
reference to my c#-project), the assembly would not load,
if the word references are missing on the clients
computer, would it?

Thanks,
Daniel
-----Original Message-----
Daniel,

Is it possible that the version of word that you are running this on
doesn't support these properties?

For this kind of work, I would recommend using a separate assembly in
VB, and use late-bound calls (they are much easier to use in VB as opposed
to reflection).

Also, you should be aware of all the references that you might be
leaking when using automation like this.

For example, when you get the Documents property, you are not releasing
the object. When you end up releasing the application object (if at all),
then the Documents object is still around, and holds a reference to the
Application, and the program will be running in the background.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hello,

i have a problem with the word automation from c#. First,
i want to mention, that i don't have any dependencies from
word in my c#-project, i want to use the system.reflection
model to handle the automation.
So, i'm using the following code to create a new word
document:

---Code---
object objApplication;
object objDocuments;
object objDocument;

objApplication = System.Activator.CreateInstance(
Type.GetTypeFromProgID("Word.Application"));

objDocuments = objApplication.GetType().InvokeMember
("Documents", BindingFlags.Default |
BindingFlags.GetProperty, null, objApplication , null);

objDocument = objDocuments.GetType().InvokeMember("Add",
BindingFlags.Default | BindingFlags.InvokeMethod, null,
objDocuments, null);
---End of Code---

This works fine. But several properties cannot be accessed
like that. For example, when i try to get the
properties "BuiltInDocumentProperties"
or "CustomDocumentProperties" like the "Documents"-
property in the example above, i get an exception with the
text "Unknown name.".

Am i doing anything wrong? Is it possible to get all
properties with the system.reflection model?

Thanks in advance for your help.

Regards,
Daniel


.
 
D

Daniel

Ok, thank you. You're right, its quite laborious to call
the automation through reflection in c#. I will try your
suggestions.

Regards,
Daniel
-----Original Message-----
Daniel,

In order to release the references, you would have to pass the object to
the static ReleaseComObject method on the Marshal class for EACH object you
exposed. SO, when you get the Documents property, thats one object you have
to release, and then any property you get from that which returns an object
(collections included) is another, and so on, and so on.

The reason I recommended VB for these calls is that you can declare an
instance of type object, and then just make calls like this:

// The application object.
Dim application As Object =
System.Activator.CreateInstance(Type.GetTypeFromProgID ("Word.Application"));

// Get the documents collection.
Dim documents As Object = application.Documents;

Granted, you won't get Intellisense, but it sure as hell beats making
all the calls through the Reflection API yourself.

If you use an early bound call, you are going to bind to the Runtime
Callable Wrapper, which your assembly will have no problem loading (because
you have to distribute it). If Word is not on the machine, then when you
try and create the instance, you will end up getting an exception when you
try and create the object.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I'm using Office XP. In the Word-VB-Editor, i have free
access to the properties that produce the exception in
C#.NET.

How can i release the references i created? Do i have to
call the Dispose()-method?

I'm using the reflection-model, because my assembly should
also work on a machine, where no office is installed
(because its providing other functions, too).
If i use early binding in this case (add the word
reference to my c#-project), the assembly would not load,
if the word references are missing on the clients
computer, would it?

Thanks,
Daniel
-----Original Message-----
Daniel,

Is it possible that the version of word that you are running this on
doesn't support these properties?

For this kind of work, I would recommend using a separate assembly in
VB, and use late-bound calls (they are much easier to
use
in VB as opposed
to reflection).

Also, you should be aware of all the references that you might be
leaking when using automation like this.

For example, when you get the Documents property,
you
are not releasing
the object. When you end up releasing the application object (if at all),
then the Documents object is still around, and holds a reference to the
Application, and the program will be running in the background.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hello,

i have a problem with the word automation from c#. First,
i want to mention, that i don't have any dependencies from
word in my c#-project, i want to use the system.reflection
model to handle the automation.
So, i'm using the following code to create a new word
document:

---Code---
object objApplication;
object objDocuments;
object objDocument;

objApplication = System.Activator.CreateInstance(
Type.GetTypeFromProgID("Word.Application"));

objDocuments = objApplication.GetType().InvokeMember
("Documents", BindingFlags.Default |
BindingFlags.GetProperty, null, objApplication , null);

objDocument = objDocuments.GetType().InvokeMember ("Add",
BindingFlags.Default | BindingFlags.InvokeMethod, null,
objDocuments, null);
---End of Code---

This works fine. But several properties cannot be accessed
like that. For example, when i try to get the
properties "BuiltInDocumentProperties"
or "CustomDocumentProperties" like the "Documents"-
property in the example above, i get an exception with the
text "Unknown name.".

Am i doing anything wrong? Is it possible to get all
properties with the system.reflection model?

Thanks in advance for your help.

Regards,
Daniel



.


.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top