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 4 7286
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
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
.
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 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
.
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 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 .
. This discussion thread is closed Replies have been disabled for this discussion. Similar topics
1 post
views
Thread by mickeydisn |
last post: by
|
12 posts
views
Thread by Cheval |
last post: by
|
6 posts
views
Thread by Colleyville Alan |
last post: by
|
2 posts
views
Thread by kids |
last post: by
|
reply
views
Thread by mharris |
last post: by
|
2 posts
views
Thread by Radek |
last post: by
|
4 posts
views
Thread by Yohancef Chin |
last post: by
|
5 posts
views
Thread by Daniel Walzenbach |
last post: by
|
10 posts
views
Thread by cj2 |
last post: by
| | | | | | | | | | |