473,396 Members | 2,036 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 7621
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.