title: Word Application hangs when executing Documents.Open(ref filename) method and handling Application.DocumentOpen event
body: <p>I've created a user control (windows form application) to get an instance of Word (either active or new), and provide a button to open documents into that instance using a file dialog picker.</p>
  
  <p>The form contains 2 buttons, 1 for getting the word instance and another for opening a document. It also contains a list box for displaying the open documents, and an openfiledialog control to provide the means for selecting documents to open.</p>
  
  <p>I am handling the Application.DocumentOpen event in order to populate the listbox...</p>
  
  <pre><code>m_wordApp.DocumentOpen += new msoWord.ApplicationEvents4_DocumentOpenEventHandler(m_wordApp_DocumentOpen);
  </code></pre>
  
  <p>I am determining when i need to reinvoke my method that populates the listbox to ensure that access to the control is on the same thread that created it....</p>
  
  <pre><code>private void AddDocument(string name)
  {
      try
      {
          if (m_documentsListBox.InvokeRequired &amp;&amp; m_documentsListBox.IsHandleCreated&amp;&amp;!m_documentsListBox.IsDisposed)
          {
              this.Invoke(m_AddDocument, new object[] { name });
              return;
          }
  
          if (!m_documentsListBox.Items.Contains(name))
              m_documentsListBox.Items.Add(name);
      }
      catch (Exception ex)
      {
      }
  }
  </code></pre>
  
  <p>Im not using 2 dots, and i believe i am releasing any COM objects correctly.</p>
  
  <p>Why does the application hang on either the line of code that opens the document ...</p>
  
  <pre><code>WordDoc = m_wordDocs.Open(ref fileName);
  </code></pre>
  
  <p>or the line that reinvokes the AddDocument() method...</p>
  
  <pre><code>this.Invoke(m_AddDocument, new object[] { name });
  </code></pre>
  
  <p>somewhere along the line i think i must be having a thread issue, because the hang only happens if i choose to open a document using the button, rather than from within the Word application directly.</p>
  
  <p>full code below...</p>
  
  <hr>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Windows.Forms;
  
  using System.Runtime.InteropServices;
  
  //  use these for the core office &amp; word references
  using msoWord = Microsoft.Office.Interop.Word;
  
  
  namespace MCDevNET.Office.Word.TestUIControls
  {
      public partial class OpenWordDocument : Form
      {
          public OpenWordDocument()
          {
              InitializeComponent();
  
              m_openWordButton.Click += new EventHandler(buttonOpenWordApp_Click);
              m_openDocumentButton.Click += new EventHandler(buttonOpenDocument_Click);
  
              m_AddDocument = new UpdateListControl(AddDocument);
          }
  
          #region Form: control eventHandlers
  
          void buttonOpenWordApp_Click(object sender, EventArgs e)
          {
              try
              {
                  if (!IsValid(m_wordApp))
                      WordApp = GetInstance();
                  AddAllDocuments();
              }
              catch (Exception ex)
              {
              }
          }
  
          void buttonOpenDocument_Click(object sender, EventArgs e)
          {
              OpenWordDoc();
          }
  
          public delegate void UpdateListControl(string name);
          private UpdateListControl m_AddDocument;
  
      private void AddDocument(string name)
      {
          try
          {
              if (m_documentsListBox.InvokeRequired &amp;&amp; m_documentsListBox.IsHandleCreated&amp;&amp;!m_documentsListBox.IsDisposed)
              {
                  this.Invoke(m_AddDocument, new object[] { name });
                  return;
              }
  
              if (!m_documentsListBox.Items.Contains(name))
                  m_documentsListBox.Items.Add(name);
          }
          catch (Exception ex)
          {
          }
      }
  
          private void AddAllDocuments()
          {
              try
              {
                  m_documentsListBox.Items.Clear();
                  if (m_wordDocs != null)
                  {
                      for (int i = 1; i &lt;= m_wordDocs.Count; i++)
                          AddDocument(m_wordDocs[i].Name);
                  }
              }
              catch (Exception ex)
              {
              }
          }
  
          #endregion
  
  
          #region Word: properties &amp; eventhandlers
  
          private msoWord.Document m_wordDoc;
          public msoWord.Document WordDoc
          {
              get { return m_wordDoc; }
              private set
              {
                  try
                  {
                      if (m_wordDoc != value)
                      {
                          ReleaseCOMObject(m_wordDoc);
                          m_wordDoc = value;
                      }
                  }
                  catch (Exception ex)
                  {
                  }
              }
          }
  
          private msoWord.Documents m_wordDocs;
          public msoWord.Documents WordDocs
          {
              get { return m_wordDocs; }
              private set
              {
                  try
                  {
                      if (m_wordDocs != value)
                      {
                          ReleaseCOMObject(m_wordDocs);
                          m_wordDocs = value;
                      }
                  }
                  catch (Exception ex)
                  {
                  }
              }
          }
  
          private msoWord.Application m_wordApp;
          public msoWord.Application WordApp
          {
              get { return m_wordApp; }
              set
              {
                  try
                  {
                      if (m_wordApp != value)
                      {
                          if (m_wordApp != null)
                          {
                              m_wordApp.DocumentOpen -= new msoWord.ApplicationEvents4_DocumentOpenEventHandler(m_wordApp_DocumentOpen);
                              ReleaseCOMObject(m_wordApp);
                          }
  
                          m_wordApp = value;
  
                          if (IsValid(m_wordApp))
                          {
      m_wordApp.DocumentOpen += new msoWord.ApplicationEvents4_DocumentOpenEventHandler(m_wordApp_DocumentOpen);
                              WordDocs = m_wordApp.Documents;
                          }
                      }
                  }
                  catch (Exception ex)
                  {
                  }
              }
          }
  
          void m_wordApp_DocumentOpen(msoWord.Document doc)
          {
              try
              {
                  string name = doc.Name;
                  AddDocument(name);
              }
              catch (Exception ex)
              {
              }
              finally
              {
                  ReleaseCOMObject(doc);
                  doc = null;
              }
          }
  
          private msoWord.Application GetInstance()
          {
              msoWord.Application app = null;
              try
              {
                  app = (msoWord.Application)Marshal.GetActiveObject("Word.Application");
              }
              catch (Exception ex)
              {
                  if (app == null)
                      app = new msoWord.Application();
              }
              finally
              {
                  if (IsValid(app))
                  {
                      app.Visible = true;
                      app.Activate();
                  }
              }
              return app;
          }
  
          private void OpenWordDoc()
          {
              try
              {
                  m_openFileDialog.AddExtension = true;
                  m_openFileDialog.Filter = "All Word (*.docx; *.docm; *.doc; *.dotx; *.dotm; *.dot)|*.docx;*.docm;*.doc;*.dotx;*.dotm;*.dot|Word Documents (*.docx)|*.docx|Word Macro-Enabled Documents (*.docm)|*.docm|Word 97-2003 Documents (*.doc)|*.doc|All Word Templates (*.dotx; *.dotm; *.dot)|*.dotx;*.dotm;*.dot|Word Templates (*.dotx)|*.dotx|Word Macro-Enabled Templates (*.dotm)|*.dotm)";
                  m_openFileDialog.FilterIndex = 1;
                  m_openFileDialog.Multiselect = false;
                  m_openFileDialog.Title = "Open Word Document";
  
                  if (m_openFileDialog.ShowDialog() == DialogResult.OK)
                  {
                      object fileName = m_openFileDialog.FileName;
                      WordDoc = m_wordDocs.Open(ref fileName);
                  }
              }
              catch (Exception ex)
              {
              }
          }
  
          private bool IsValid(msoWord.Application app)
          {
              try
              {
                  if (app != null)
                  {
                      string name = app.Caption;
                      return true;
                  }
              }
              catch (Exception ex)
              {
              }
              return false;
          }
          #endregion
  
          private void ReleaseCOMObject(object comObject)
          {
              try
              {
                  GC.Collect();
                  GC.WaitForPendingFinalizers();
                  GC.Collect();
                  GC.WaitForPendingFinalizers();
  
                  if (comObject != null &amp;&amp; Marshal.IsComObject(comObject))
                      Marshal.ReleaseComObject(comObject);
  
              }
              catch (Exception ex)
              {
              }
          }
      }
  }
  namespace MCDevNET.Office.Word.TestUIControls
  {
      partial class OpenWordDocument
      {
          /// &lt;summary&gt;
          /// Required designer variable.
          /// &lt;/summary&gt;
          private System.ComponentModel.IContainer components = null;
  
          /// &lt;summary&gt;
          /// Clean up any resources being used.
          /// &lt;/summary&gt;
          /// &lt;param name="disposing"&gt;true if managed resources should be disposed; otherwise, false.&lt;/param&gt;
          protected override void Dispose(bool disposing)
          {
              if (disposing &amp;&amp; (components != null))
              {
                  components.Dispose();
              }
              base.Dispose(disposing);
          }
  
          #region Windows Form Designer generated code
  
          /// &lt;summary&gt;
          /// Required method for Designer support - do not modify
          /// the contents of this method with the code editor.
          /// &lt;/summary&gt;
          private void InitializeComponent()
          {
              this.m_documentsListBox = new System.Windows.Forms.ListBox();
              this.m_openDocumentButton = new System.Windows.Forms.Button();
              this.m_openWordButton = new System.Windows.Forms.Button();
              this.m_openFileDialog = new System.Windows.Forms.OpenFileDialog();
              this.SuspendLayout();
              // 
              // lb_Documents
              // 
              this.m_documentsListBox.FormattingEnabled = true;
              this.m_documentsListBox.Location = new System.Drawing.Point(12, 41);
              this.m_documentsListBox.Name = "lb_Documents";
              this.m_documentsListBox.Size = new System.Drawing.Size(156, 134);
              this.m_documentsListBox.TabIndex = 8;
              // 
              // m_openDocumentButton
              // 
              this.m_openDocumentButton.Location = new System.Drawing.Point(93, 12);
              this.m_openDocumentButton.Name = "m_openDocumentButton";
              this.m_openDocumentButton.Size = new System.Drawing.Size(75, 23);
              this.m_openDocumentButton.TabIndex = 7;
              this.m_openDocumentButton.Text = "Doc";
              this.m_openDocumentButton.UseVisualStyleBackColor = true;
              // 
              // m_openWordButton
              // 
              this.m_openWordButton.Location = new System.Drawing.Point(12, 12);
              this.m_openWordButton.Name = "m_openWordButton";
              this.m_openWordButton.Size = new System.Drawing.Size(75, 23);
              this.m_openWordButton.TabIndex = 6;
              this.m_openWordButton.Text = "Word";
              this.m_openWordButton.UseVisualStyleBackColor = true;
              // 
              // m_openFileDialog
              // 
              this.m_openFileDialog.FileName = "openFileDialog1";
              // 
              // OpenWordDocument
              // 
              this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
              this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
              this.ClientSize = new System.Drawing.Size(182, 184);
              this.Controls.Add(this.m_documentsListBox);
              this.Controls.Add(this.m_openDocumentButton);
              this.Controls.Add(this.m_openWordButton);
              this.Name = "OpenWordDocument";
              this.Text = "OpenWordDocument";
              this.ResumeLayout(false);
  
          }
  
          #endregion
  
          private System.Windows.Forms.ListBox m_documentsListBox;
          private System.Windows.Forms.Button m_openDocumentButton;
          private System.Windows.Forms.Button m_openWordButton;
          private System.Windows.Forms.OpenFileDialog m_openFileDialog;
      }
  }
  </code></pre>