472,952 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,952 software developers and data experts.

Word Automation

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
4 7558
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: mickeydisn | last post by:
Sub: C++ Word automation Extract text hello. I want extact text form a word document using a visual c++ programme. I have see a lot of documentation. and my analysis is that I must use a...
12
by: Cheval | last post by:
Has anyone had any problems with inter-office automation between MS Word and MS Access in Office 2003? I have recently installed office 2003 in a new folder and have left the older office 2000...
6
by: Colleyville Alan | last post by:
I have an application that has an Access table that stores the locations of slides in a Powerpoint file. This used to work fine when there were about 4 files and 200 slides. The database would...
2
by: kids | last post by:
Does anybody know any reason which could cause Ms. word automation to crash? I try to call word automation to open a document and use find and replace function. For some reason it works but I...
0
by: mharris | last post by:
I need help with merging two Word documents into one through C# code. The problem isn't so much getting the documents put into one as it is maintaining the appropriate formatting, or rather...
2
by: Radek | last post by:
Hi, I have got such problem: in my directory "C:\folder" I have 3 files in MS WORD (having tables, images etc), these are: "1.doc", "2.doc", "3.doc". I want to write an application (C# of...
4
by: Yohancef Chin | last post by:
Hi, Being fairly new to .NET I am looking for a way to call MS Word from an event on a webform, and after the user is finished save that created document to an SQL Server database. Has anyone...
5
by: Daniel Walzenbach | last post by:
Hi, I need to know how I could populate a word file from within ASP.NET and stream it out to some user (I can rely on all users have at least Word XP installed). The preferable solution would be...
10
by: cj2 | last post by:
I open a word template in VB and add values to the bookmarks then save the document as as pdf. When I then go to close the document it pops up a save as dialog box. It's already saved the...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.