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

Automatiting office from .NET (Invalid CAST)

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

Jul 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Hi Steve,

There is a knowledge Base article that can show you how to get and set
office document properties.

http://support.microsoft.com/?kbid=303296

Thanks,
Paul Stubbs
VSTO Program Manager

--
Please post questions to the newsgroup - everyone benefits.
This post is provided "AS IS" with no warranties, and confers no rights
Sample code subject to http://www.microsoft.com/info/cpyright.htm

Jul 21 '05 #2

P: n/a
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
"Paul Stubbs [MS]" <ps*****@NoSpam.Microsoft.com> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
Hi Steve,

There is a knowledge Base article that can show you how to get and set
office document properties.

http://support.microsoft.com/?kbid=303296

Thanks,
Paul Stubbs
VSTO Program Manager

--
Please post questions to the newsgroup - everyone benefits.
This post is provided "AS IS" with no warranties, and confers no rights
Sample code subject to http://www.microsoft.com/info/cpyright.htm

Jul 21 '05 #3

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


"Steve Drake" <Steve@_NOSPAM_Drakey.co.uk> wrote in message
news:uQ****************@TK2MSFTNGP12.phx.gbl...
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
"Paul Stubbs [MS]" <ps*****@NoSpam.Microsoft.com> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
Hi Steve,

There is a knowledge Base article that can show you how to get and set
office document properties.

http://support.microsoft.com/?kbid=303296

Thanks,
Paul Stubbs
VSTO Program Manager

--
Please post questions to the newsgroup - everyone benefits.
This post is provided "AS IS" with no warranties, and confers no rights
Sample code subject to http://www.microsoft.com/info/cpyright.htm



Jul 21 '05 #4

P: n/a
I have used that in the past, but... it doesent do bookmarks :(

Thanks

Steve

"Jonathan West" <jw***@mvps.org> wrote in message
news:OM**************@TK2MSFTNGP10.phx.gbl...
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


"Steve Drake" <Steve@_NOSPAM_Drakey.co.uk> wrote in message
news:uQ****************@TK2MSFTNGP12.phx.gbl...
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
"Paul Stubbs [MS]" <ps*****@NoSpam.Microsoft.com> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
Hi Steve,

There is a knowledge Base article that can show you how to get and set
office document properties.

http://support.microsoft.com/?kbid=303296

Thanks,
Paul Stubbs
VSTO Program Manager

--
Please post questions to the newsgroup - everyone benefits.
This post is provided "AS IS" with no warranties, and confers no rights Sample code subject to http://www.microsoft.com/info/cpyright.htm


Jul 21 '05 #5

P: n/a

"Steve Drake" <Steve@_NOSPAM_Drakey.co.uk> wrote in message
news:eL**************@TK2MSFTNGP11.phx.gbl...
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.

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

Jul 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.