473,320 Members | 1,856 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,320 software developers and data experts.

Automatiting office from .NET (Invalid CAST)

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
5 2399
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
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
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
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

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

Similar topics

5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
3
by: John Howard | last post by:
Making the following call to a local MSAccess database works fine: Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs) Dim intRows As Integer Dim strSQL As String Dim ds As New...
1
by: Hifni Shahzard | last post by:
Hi, I got a stored procedure, where it returns a value. But if I execute it. It gives an error as "Invalid cast from System.Int32 to System.Byte.". To make clear how do I execute this, below I'm...
0
by: Ken Ingram | last post by:
I have been reading through all the MSDN docs on using the new Visual Studio Tools for Microsoft Office Systems and have alot of things working in my project. One thing I am stuggling with is how...
6
by: ©pEIO | last post by:
I have installed the PIA of Microsoft Office 2003 and the installation was succefully. If I add the reference of Microsoft Excel 11.0, I see that the property CopyLocal is False and the assembly...
0
by: Tom Rahav | last post by:
Dear All! I use Visual Basic .NET 2003 and want to produce graphs by using Office Chart 11.0. I would like to provide this chart with data from an Office Spreadsheet 11.0 and I use the...
5
by: Steve Drake | last post by:
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...
25
by: dennijr | last post by:
ok, shell always used to be easy for me, now its starting to get annoying cause i dont know wats wrong heres the simplist code possible: Private Sub IExplorer_Click() a = Shell("C:\Program...
1
vinci
by: vinci | last post by:
Please help me out... i only have two weeks to do it... here is my code: Dim ex As New Object Dim wb As Excel.Workbook Dim sheet As Excel.Worksheet
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.