Open Word document error

A

Alan T

In my C# application, I want to open a Word document using the code from the
Help:
using Microsoft.Office.Interop.Word;

object readOnly = true;
object fileName = @"C:\Test\NewDocument.doc";

this.Application.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 missing, ref missing, ref missing, ref missing, ref missing);I got a
compilation error:'WindowsApplication2.Form1' does not contain a definition
for 'Application'
 
G

Guest

You need to declare an instance of Word:

Microsoft.Office.Interop.Word.Application app = new
Microsoft.Office.Interop.Word.ApplicationClass();
object readOnly = true;
object fileName = @"C:\Test\NewDocument.doc";
app.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 missing, ref missing, ref missing, ref missing, ref missing);

HTH

Ciaran O'Donnell
 
A

Alan T

Hi,

What should I add to my Reference ?

Ciaran O''Donnell said:
You need to declare an instance of Word:

Microsoft.Office.Interop.Word.Application app = new
Microsoft.Office.Interop.Word.ApplicationClass();
object readOnly = true;
object fileName = @"C:\Test\NewDocument.doc";
app.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 missing, ref missing, ref missing, ref missing, ref missing);

HTH

Ciaran O'Donnell
 

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