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

Error with open word doc.

P: n/a
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
Nov 15 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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
"sudha" <su*******@hotmail.com> wrote in message
news:05****************************@phx.gbl...
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

Nov 15 '05 #2

P: n/a
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
-----Original Message-----
Hi Sudha,

Two questions that will help us troubleshoot the problem betterWhich 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 theinterop wrappers via (b).
There are some issues with using Office XP PIAs with Word 2000.
Regards,
Aravind C
"sudha" <su*******@hotmail.com> wrote in message
news:05****************************@phx.gbl...
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

.

Nov 15 '05 #3

P: n/a
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
"Sudha" <an*******@discussions.microsoft.com> wrote in message
news:05****************************@phx.gbl...
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
-----Original Message-----
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
"sudha" <su*******@hotmail.com> wrote in message
news:05****************************@phx.gbl...
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

.

Nov 15 '05 #4

P: n/a
I meant Word 2002. Sorry. Sleepy after my lunch :-)

"Aravind C" <ar***********@nospam.hotmail.com> wrote in message
news:OM**************@TK2MSFTNGP09.phx.gbl...
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
"Sudha" <an*******@discussions.microsoft.com> wrote in message
news:05****************************@phx.gbl...
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
-----Original Message-----
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
"sudha" <su*******@hotmail.com> wrote in message
news:05****************************@phx.gbl...
> 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
>
>
.


Nov 15 '05 #5

P: n/a
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
-----Original Message-----
I meant Word 2002. Sorry. Sleepy after my lunch :-)

"Aravind C" <ar***********@nospam.hotmail.com> wrote in messagenews:OM**************@TK2MSFTNGP09.phx.gbl...
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
"Sudha" <an*******@discussions.microsoft.com> wrote in

message news:05****************************@phx.gbl...
> 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
>
> >-----Original Message-----
> >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
> >
> >
> >"sudha" <su*******@hotmail.com> wrote in message
> >news:05****************************@phx.gbl...
> >> 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
> >>
> >>
> >
> >
> >.
> >


.

Nov 15 '05 #6

P: n/a
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
-----Original Message-----
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 operationrun-time exception.
How do i undo this Office XP PIA ? Pls advise.

Thanks
sudha
-----Original Message-----
I meant Word 2002. Sorry. Sleepy after my lunch :-)

"Aravind C" <ar***********@nospam.hotmail.com> wrote inmessage
news:OM**************@TK2MSFTNGP09.phx.gbl...
Sudha,

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

Regards,
Aravind C
"Sudha" <an*******@discussions.microsoft.com> wrote in

message news:05****************************@phx.gbl...
> Aravind
> Thanks for your reply. i am using (a)
> I tried (b) but got build errors so i installedoffice XP > PIAs. How do i go about it. Can you please advise.
>
> Many thanks
> sudha
>
> >-----Original Message-----
> >Hi Sudha,
> >
> >Two questions that will help us troubleshoot theproblem > 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 classesmanually > 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
withWord > 2000.
> >
> >Regards,
> >Aravind C
> >
> >
> >"sudha" <su*******@hotmail.com> wrote in message
> >news:05****************************@phx.gbl...
> >> Hi,
> >> To open a word doc from c#, i use the followingcode : > >>
> >> 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 stopswith > the
> >> following message :
> >>
> >> A first chance exception of
> >> type 'System.InvalidCastException' occurred in
> AdoDemo1.exe
> >>
> >> Additional information: QueryInterface forinterface > >> Word._Application failed.
> >>
> >> Can anyone help to overcome the above errormessage. > >>
> >> Thanks
> >> sudha
> >>
> >>
> >
> >
> >.
> >

.

.

Nov 15 '05 #7

P: n/a
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("W ord.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

"sudha" <an*******@discussions.microsoft.com> wrote in message
news:04****************************@phx.gbl...
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
-----Original Message-----
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
-----Original Message-----
I meant Word 2002. Sorry. Sleepy after my lunch :-)

"Aravind C" <ar***********@nospam.hotmail.com> wrote in

message
news:OM**************@TK2MSFTNGP09.phx.gbl...
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
"Sudha" <an*******@discussions.microsoft.com> wrote in

message
news:05****************************@phx.gbl...
> 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
>
> >-----Original Message-----
> >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
> >
> >
> >"sudha" <su*******@hotmail.com> wrote in message
> >news:05****************************@phx.gbl...
> >> 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
> >>
> >>
> >
> >
> >.
> >


.

.

Nov 15 '05 #8

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

-----Original Message-----
HI Sudha,

There's a possibiloity of a version mismatch between the version ofthe word you have installed in your machine and the actual type libraryyou 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 vialate binding.

Regards,
Aravind C

"sudha" <an*******@discussions.microsoft.com> wrote in messagenews:04****************************@phx.gbl...
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
>-----Original Message-----
>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
>>-----Original Message-----
>>I meant Word 2002. Sorry. Sleepy after my lunch :-)
>>
>>"Aravind C" <ar***********@nospam.hotmail.com> wrote in >message
>>news:OM**************@TK2MSFTNGP09.phx.gbl...
>>> 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
>>>
>>>
>>> "Sudha" <an*******@discussions.microsoft.com> wrote in >message
>>> news:05****************************@phx.gbl...
>>> > 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
>>> >
>>> > >-----Original Message-----
>>> > >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
>>> > >
>>> > >
>>> > >"sudha" <su*******@hotmail.com> wrote in message
>>> > >news:05****************************@phx.gbl...
>>> > >> 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
>>> > >>
>>> > >>
>>> > >
>>> > >
>>> > >.
>>> > >
>>>
>>>
>>
>>
>>.
>>
>.
>

.

Nov 15 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.