By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 959 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

Word Automation

P: n/a
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

Nov 16 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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]
- mv*@spam.guard.caspershouse.com

"Daniel" <an*******@discussions.microsoft.com> wrote in message
news:0a****************************@phx.gbl...
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

Nov 16 '05 #2

P: n/a
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 ondoesn't support these properties?

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

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

For example, when you get the Documents property, you are not releasingthe object. When you end up releasing the application object (if at all),then the Documents object is still around, and holds a reference to theApplication, and the program will be running in the background.
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Daniel" <an*******@discussions.microsoft.com> wrote in messagenews:0a****************************@phx.gbl...
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

.

Nov 16 '05 #3

P: n/a
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.GetTypeFromPr ogID("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]
- mv*@spam.guard.caspershouse.com

<an*******@discussions.microsoft.com> wrote in message
news:0a****************************@phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Daniel" <an*******@discussions.microsoft.com> wrote in

message
news:0a****************************@phx.gbl...
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

.

Nov 16 '05 #4

P: n/a
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 tothe static ReleaseComObject method on the Marshal class for EACH object youexposed. SO, when you get the Documents property, thats one object you haveto 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 aninstance of type object, and then just make calls like this:
// The application object.
Dim application As Object =
System.Activator.CreateInstance(Type.GetTypeFromP rogID ("Word.Application"));
// Get the documents collection.
Dim documents As Object = application.Documents;

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

If you use an early bound call, you are going to bind to the RuntimeCallable Wrapper, which your assembly will have no problem loading (becauseyou have to distribute it). If Word is not on the machine, then when youtry and create the instance, you will end up getting an exception when youtry and create the object.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<an*******@discussions.microsoft.com> wrote in message
news:0a****************************@phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Daniel" <an*******@discussions.microsoft.com> wrote in

message
news:0a****************************@phx.gbl.. .
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

.

.

Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.