Office Automation

G

Guest

Hi
first of all, sorry for my bad english
anyway, i am trying to access a word document in c# but my code doesnt work!
i have search through the net about Office Automation and i have found man
articles and forums speaking about it but no answer to my prob

IMPORTANT: I am using
- W2K
- Word2000
- Visual studio .Net 200
- Microsoft Office 9.0 Object Librar
- Microsoft Word 9.0 Object Librar

my code is

object oMissing = System.Reflection.Missing.Value

Word.ApplicationClass oWord
Word.Document oDoc
oWord = new Word.ApplicationClass()
oWord.Visible = true
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing
ref oMissing, ref Missing)

..... etc ..

At runtime i got the following error

An unhandled exception of type 'System.InvalidCastException' occurred i
WindowsApplication2.ex
Additional information: QueryInterface for interface Word._Applicatio
failed

The problem is that 'new Word.ApplicationClass();' is not able to inizialize the variable 'oWord' and I dont know why

Microsoft Office 9.0 Object Library, Microsoft Word 9.0 Object Library should be the right ones for Word2000

I know there are new Pias for Word2002 or 2003, but i need to open, create etc. a Word2000 document

Any idea

Thank

federic
 
W

Wiktor Zychla

At runtime i got the following error:
An unhandled exception of type 'System.InvalidCastException' occurred in
WindowsApplication2.exe
Additional information: QueryInterface for interface Word._Application
failed.

are you sure that the object libraries are from the correct version of
Office?

from my practical experiences, I will give you an advice: do not use static
libraries for automation. instead use dynamic linking through reflection. it
can be done in C# but it is much simpler in VB.NET because it uses dynamic
method binding. the pros are obvious: the code you write is in general
independent of the Office version and is really simple.

you can organize your app as follows: put all automation code into vb.net
module and all the logic into C# modules.

here's an example of vb.net code:

Sub Automation
Dim o as object, w as object

o = CreateObject( "Word.Application" )
o.Visible = true
w = o.Documents.Add
w.Range().Text = "inserted text"

End Sub
 

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