How to check any existing ActiveDocument?

For office developers, you might want to check is there any existing active document is opened before you open a document template.

What you can do is, check is there any open by using  (Globals.ThisAddIn.Documents.Count > 0). Then close the activeDocument using ActiveDocument.Close(ref object, ref object, ref object) .

Here is the snippet code that for your reference:

object miss = Type.Missing;
object readOnly = false;

if (Globals.ThisAddIn.Documents.Count > 0)
{
      Globals.ThisAddIn.Application.ActiveDocument.Close(ref miss, ref miss, miss);
}

Word.Document docNew = Globals.ThisAddIn.Application.Documents.Open(ref filePath, ref miss, ref readOnly, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);

docNew = Globals.ThisAddIn.Application.ActiveDocument;

Comments