cn-deng
cn-deng

Reputation: 51

How to open a word file with interop.word

When I open a .docx file through interop.word, I can edit it by another editor tool.

After editing it, the Interop.word find the file has been modified and will attempt to update the content edited.

My code is below:

Word.Application app = new Microsoft.Office.Interop.Word.Application();
            Word.Document doc = null;
            object unknow = Type.Missing;
            app.Visible = true;
            string str = @"E:\1.doc";
            object file = str;
            doc = app.Documents.Open(ref file,
                ref unknow, ref unknow, ref unknow, ref unknow,
                ref unknow, ref unknow, ref unknow, ref unknow,
                ref unknow, ref unknow, ref unknow, ref unknow,
                ref unknow, ref unknow, ref unknow);

The error I get is telling me that the file is opened.

Upvotes: 2

Views: 11036

Answers (1)

Hexie
Hexie

Reputation: 4221

You dont need all the parameters if you not using them - this should do the same job:

Create new instance of the application:

Word.WordApplication = CreateObject("Word.Application")

or

Word.Application app = new Microsoft.Office.Interop.Word.Application();

Add a new document:

Word.WordApplication.Documents.Add()

Then open it:

Word.WordApplication.Documents.Open(documentPath)

If you need to open the document in ReadOnly set to true then use this:

Word.WordApplication.Documents.Open(documentPath, ReadOnly:=True)

Upvotes: 3

Related Questions