Error with open word doc.

S

sudha

Hi,
To open a word doc from c#, i use the following code :

Word.ApplicationClass WordApp = new Word.ApplicationClass
();

// give any file name of your
choice.
object fileName = "C:\\test.doc";
object readOnly = false;
object isVisible = true;

// the way to handle parameters
you don't care about in .NET
object missing =
System.Reflection.Missing.Value;

// Make word visible, so you can
see what's happening
//WordApp.Visible = true;
// Open the document that was
chosen by the dialog
Word.Document aDoc =
WordApp.Documents.Open(ref fileName,
ref missing,ref readOnly,
ref missing,
ref missing, ref missing,
ref missing,
ref missing, ref missing,
ref missing,
ref missing, ref
isVisible);


While executing, Wordapp.Documents.Open..it stops with the
following message :

A first chance exception of
type 'System.InvalidCastException' occurred in AdoDemo1.exe

Additional information: QueryInterface for interface
Word._Application failed.

Can anyone help to overcome the above error message.

Thanks
sudha
 
A

Aravind C

Hi Sudha,

Two questions that will help us troubleshoot the problem better
Which version of Word are you presently using ?
For interop, are you presently using the
(a) Office XP PIAs or
(b) Did you generate the interop wrapper classes manually via TLBIMP
(Add reference in VS.NET)

If you are using Word 2000, then you would need to generate the
interop wrappers via (b).
There are some issues with using Office XP PIAs with Word 2000.

Regards,
Aravind C
 
S

Sudha

Aravind
Thanks for your reply. i am using (a)
I tried (b) but got build errors so i installed office XP
PIAs. How do i go about it. Can you please advise.

Many thanks
sudha
 
A

Aravind C

Sudha,

Can you please post the build errors that you're getting.
The current version of Word you're currently using is Word 2003(Office XP).
Correct ?

Regards,
Aravind C
 
S

sudha

Aravind
Before i put in the office XP PIA , i tried to open word
document by adding reference. It said 'Application-
ambigous reference..' .After i installed the PIA's i dont
get any build errors. but i get this invalidcast operation
run-time exception.
How do i undo this Office XP PIA ? Pls advise.

Thanks
sudha
 
S

sudha

Aravind,
i undid office XP PIA with
gacutil /u Microsoft.Office.Interop.Word
and created a new windows application and just add a COM
reference .
i added same set of code, but got the same error as
given below :

A first chance exception of
type 'System.InvalidCastException' occurred in
AdoDemo1.exe
Additional information: QueryInterface for
interface Word._Application failed.

Please help.
Many thanks
sudha
 
A

Aravind C

HI Sudha,

There's a possibiloity of a version mismatch between the version of
the word you have installed in your machine and the actual type library
you are binding to from th Add Reference.

Can you please try late-binding to see if it makes any diffrence:

Object wordApp = null;
wordApp =
Activator.CreateInstance(Type.GetTypeFromProgID("Word.Application", true) );
Word.Application app = (Word.Application) wordApp;

Then, call app.Documents.Open() on this instance that you've onbtained via
late binding.

Regards,
Aravind C
 
S

sudha

Hi Aravind,
Thanks for your reply. I have office 2k and the ref.is
MSWORD9.OLB.
when i tried the solution you have given :

I got this error :


A first chance exception of
type 'System.InvalidCastException' occurred in AdoDemo1.exe

Additional information: Specified cast is not valid.

when it exec : Word.Application app = (Word.Application)
wordApp;

Please advise.
Thanks again
sudha
 

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