Automatiting office from .NET (Invalid CAST)

S

Steve Drake

All,

I am trying to open a word document and get all the properties using .NET
(c#), if I create the wordapp, call the correct methods I get invalid cast
(see code examples), but if I create a WORD PROJECT (from C#) and use the
Application (thisApplication) the word project creates, it works fine, but
this is far from a acceptable work around as doing it this way means I have
to trigger the code from word.

This does not work (Watch for the word wrap):


void TestUsing_WORDAPP()
{
object missing = System.Reflection.Missing.Value;
object filename = @"C:\1.doc";

Word.ApplicationClass wordApp = new
Microsoft.Office.Interop.Word.ApplicationClass(); // I HAVE TRIED USING THE
INTERFACE HERE
Word.Document wordDoc = wordApp.Documents.Open(ref filename,ref
missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref
missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref
missing,ref missing,ref missing);

// Get the props
Microsoft.Office.Core.DocumentProperties docprops
=(Microsoft.Office.Core.DocumentProperties)
wordDoc.BuiltInDocumentProperties; // INVALID CAST HERE
System.Diagnostics.Trace.WriteLine(docprops.Count);
}

And this does work.

void TestUsing_This()
{
object missing = System.Reflection.Missing.Value;
object filename = @"C:\1.doc";

Word.Document wordDoc = thisApplication.Documents.Open(ref filename,ref
missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref
missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref
missing,ref missing,ref missing);

// Get the props
Microsoft.Office.Core.DocumentProperties docprops
=(Microsoft.Office.Core.DocumentProperties)
wordDoc.BuiltInDocumentProperties;
System.Diagnostics.Trace.WriteLine(docprops.Count); // THIS WORKS, output
is 30
}
 
S

Steve Drake

Thanks, I ended up doing it this way and it was very long winded, I wrote a
class that converts a COM collection to a .NET hashtable, but you would have
thought the office.word lib would work with the office core lib like it does
when you use the word application object that's is passed to the startup
method when you load the .NET assembly from the word document.

Steve
 
J

Jonathan West

Hi Steve,

Rather than automating Word for this, you might want instead to use an
ActiveX DLL which is available from Microsoft on a free download, which just
reads & writes document properties, and does nothing else. It is a good deal
faster than automatic Word for this purpose, particularly if you don't
already have a copy of Word open.

The accompanying sample code is VB5/6, but it shouldn't be that hard to
control it from C#. Take a look here

Dsofile.exe Lets You Edit Office Document Properties from Visual Basic and
ASP
http://support.microsoft.com/?kbid=224351

--
Regards
Jonathan West - Word MVP
http://www.multilinker.com
Please reply to the newsgroup
 
J

Jonathan West

Steve Drake said:
I have used that in the past, but... it doesent do bookmarks :(

If you want the contents of the properties to be reflected wothin the
document, simply ensure that DOCPROPERTY fields are inserted where you
currently have bookmarks.
 

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