473,403 Members | 2,323 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,403 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 2406
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.