none
Word Automation closes existing instance of Word. RRS feed

  • Question

  • I have an opened instance of Word (that was opened manually) containing an unsaved document. When I run

    _officeApp = new Word.Application();

    my instance is closing the GUI and it disappears from my open apps list in Task Manager (I am using Windows 8). The document is lost because the automation is using that instance instead of creating a new one. 

    Is there a way to force new Word.Application(); to create a new instance and not reuse an existing one?

    Thank you,
    Cosima Radu

    Wednesday, March 26, 2014 8:54 PM

Answers

  • Hi Cosima,

    According to my investigation with Windows 8.1 and Word 2013, using Word.Application would create a new Word process.

    Here is my test code from a windows console application:

    static void Main(string[] args)
            {
    		openWord();
            }
    static void openWord()
            {
                Word.Application app = new Word.Application();
                Word.Document doc = app.Documents.Open("D:/2.docx");
                app.Visible = true;
            }
    

    After opening a Word document manually and running the code, I would find a new Word process in task manager:

    You could try my code to check whether there is a new Word process in task manager.

    Which version of Office are you using?

    In addition, if you didn’t set the visible of Word application with true, the Word process would stay in background processes list.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, March 27, 2014 10:50 AM
    Moderator
  • An alternative approach is to check whether Word is already running before trying to create the new instance. Only create it if Word isn't running and only close it afterwards if Word is not already running. A single Boolean is all you need. The VBA code would be like:

    Dim wdApp As Object, bStrt As Boolean
    ' Test whether Word is already running.
    On Error Resume Next
    bStrt = False ' Flag to record if we start Word, so we can close it later.
    Set wdApp = GetObject(, "Word.Application")
    'Start Word if it isn't running
    If wdApp Is Nothing Then
      Set wdApp = CreateObject("Word.Application")
      If wdApp Is Nothing Then
        MsgBox "Can't start Word.", vbExclamation
        Exit Sub
      End If
      ' Record that we've started Word.
      bStrt = True
    End If
    On Error GoTo 0
    With wdApp
      'Do all processing here

      'Quit Word if we started it
      If bStrt = True Then .Quit
    End With
    ' Release Word object memory
    Set wdApp = Nothing


    Cheers
    Paul Edstein
    [MS MVP - Word]


    Friday, March 28, 2014 1:25 AM

All replies

  • Hello Stefan,

    By default, an existing Word process is used for opening documents. However, you can specify a command line parameter (/n) for starting a new instance/process of Word instead of using an existing one. See How to use startup command line switches to start Word 2010, Word 2007, Word 2003, Word 2002, and Word 2000 for more information.

    The Word application ctor doesn't allow to specify command line parameters. You need to start a new Word process instead (see Process.Start). Then you can find the running Word instance and connect to it. See How to use Visual C# to automate a running instance of an Office program for more infromation.

    Thursday, March 27, 2014 8:54 AM
  • Hi Cosima,

    According to my investigation with Windows 8.1 and Word 2013, using Word.Application would create a new Word process.

    Here is my test code from a windows console application:

    static void Main(string[] args)
            {
    		openWord();
            }
    static void openWord()
            {
                Word.Application app = new Word.Application();
                Word.Document doc = app.Documents.Open("D:/2.docx");
                app.Visible = true;
            }
    

    After opening a Word document manually and running the code, I would find a new Word process in task manager:

    You could try my code to check whether there is a new Word process in task manager.

    Which version of Office are you using?

    In addition, if you didn’t set the visible of Word application with true, the Word process would stay in background processes list.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, March 27, 2014 10:50 AM
    Moderator
  • An alternative approach is to check whether Word is already running before trying to create the new instance. Only create it if Word isn't running and only close it afterwards if Word is not already running. A single Boolean is all you need. The VBA code would be like:

    Dim wdApp As Object, bStrt As Boolean
    ' Test whether Word is already running.
    On Error Resume Next
    bStrt = False ' Flag to record if we start Word, so we can close it later.
    Set wdApp = GetObject(, "Word.Application")
    'Start Word if it isn't running
    If wdApp Is Nothing Then
      Set wdApp = CreateObject("Word.Application")
      If wdApp Is Nothing Then
        MsgBox "Can't start Word.", vbExclamation
        Exit Sub
      End If
      ' Record that we've started Word.
      bStrt = True
    End If
    On Error GoTo 0
    With wdApp
      'Do all processing here

      'Quit Word if we started it
      If bStrt = True Then .Quit
    End With
    ' Release Word object memory
    Set wdApp = Nothing


    Cheers
    Paul Edstein
    [MS MVP - Word]


    Friday, March 28, 2014 1:25 AM