15

I'm using WinForms. I have a form that has a button.

Goal: On button click: Open up a word document. Where the file path is hard coded into the program. I don't want the users to have to locate the word document.

Problem: I receive this error message. When I wrote my code, I get a red error line under 'Application'.

enter image description here

    private void button1_Click(object sender, EventArgs e)
    { 
        this.Application.Documents.Open(@"C:\Test\NewDocument.docx", ReadOnly:true)

    }
taji01
  • 2,527
  • 8
  • 36
  • 80
  • Just a wild guess, but are you are missing a field or property called `Application`? –  Sep 28 '15 at 05:47
  • 1
    @Roy what do you mean by field? – taji01 Sep 28 '15 at 05:49
  • https://msdn.microsoft.com/en-us/library/ms173118.aspx –  Sep 28 '15 at 05:50
  • By the way, if you find that Word does not exit when your program shuts down, it may because of your codes use of _double dot_. _[Never use 2 dots with com objects.](http://stackoverflow.com/questions/158706/how-to-properly-clean-up-excel-interop-objects)_. It deals with Excel but the concept applies with Word. e.g. `Application.Documents.Open()` –  Sep 28 '15 at 05:52

4 Answers4

27

Instead of adding interop in your reference, you may also consider to use this:

System.Diagnostics.Process.Start(@"C:\Test\NewDocument.docx");
Heisenberg
  • 764
  • 6
  • 20
15

first add the dll of Microsoft.Office.Interop.Word to your references then add this:

using Microsoft.Office.Interop.Word;

and use the following code:

Application ap = new Application(); 
Document document = ap.Documents.Open(@"C:\Test\NewDocument.docx");
anhoppe
  • 4,287
  • 3
  • 46
  • 58
amit dayama
  • 3,246
  • 2
  • 17
  • 28
  • 2
    I don't know why, but I had to do make word visible using `ap.Visible = true;`. Unless, it opened the document but with a invisible window. – Daniel Bonetti Mar 07 '17 at 05:17
  • @DanielBonetti Yes, that makes sense. Opening the application only starts the process, but not the interface. It's useful if you're processing Word documents in a way that does not require user interaction (switching format, encoding, etc). If you want to open the Word UI you need to specify it. – Scopperloit May 03 '18 at 12:38
  • `The type or namespace name 'Application' does not exist in the namespace` – user2924019 May 11 '21 at 14:14
6

This Application is not this.Application it's Microsoft.Office.Interop.Word.Application.
So you can use this code:

using System;
using Microsoft.Office.Interop.Word;

class Program
{
    static void Main()
    {
    // Open a doc file.
    Application application = new Application();
    Document document = application.Documents.Open("C:\\word.doc");

    //Do whatever you want

    // Close word.
    application.Quit();
    }
}
Zhr Saghaie
  • 1,031
  • 2
  • 16
  • 41
2

There is a good answer above which is:

System.Diagnostics.Process.Start(@"C:\Test\NewDocument.docx");

This should be modified for .Net Core 2 and above to be:

    var p = new Process();
    p.StartInfo = new ProcessStartInfo(filename)
    {
       UseShellExecute = true
    };
    p.Start();