public void TestOpenDocument()
        {
            using (ExcelApplicationController applicationController = new ExcelApplicationController(true))
            {
                applicationController.CreateHostApplication(2000);
                object officeDocument = applicationController.OpenDocument(m_testPath + "test.xls", false);
                Assert.IsNotNull(officeDocument);
                try
                {
                    Assert.IsInstanceOf(typeof(Excel.Workbook), officeDocument);
                }
                finally
                {
                    if (null != officeDocument)
                        Marshal.ReleaseComObject(officeDocument);

                    officeDocument = null;
                }
            }
        }
        public void TestScreenUpdate()
        {
            using (ExcelApplicationController applicationController = new ExcelApplicationController(true))
            {
                applicationController.CreateHostApplication(2000);
                object officeDocument = null;
                try
                {
                    Excel._Application application = (Excel._Application)applicationController.HostApplication;
                    Assert.IsTrue(application.ScreenUpdating);

                    officeDocument = applicationController.OpenDocument(m_testPath + "test.xls", false);
                    Assert.IsNotNull(officeDocument);

                    Assert.IsFalse(application.ScreenUpdating);

                    applicationController.CloseDocument(officeDocument, false);

                    Assert.IsTrue(application.ScreenUpdating);
                }
                finally
                {
                    if (null != officeDocument)
                        Marshal.ReleaseComObject(officeDocument);

                    officeDocument = null;
                }
            }
        }
        public void TestRecoverInvalidHostApplication()
        {
            using (ExcelApplicationController applicationController = new ExcelApplicationController(true))
            {
                applicationController.CreateHostApplication(2000);
                Excel._Application application = (Excel._Application)applicationController.HostApplication;
                Assert.IsNotNull(application);
                Assert.IsTrue(applicationController.IsHostApplicationValid);

                // User kills all excel processes
                foreach (Process process in Process.GetProcessesByName("excel"))
                {
                    process.Kill();
                }

                Assert.IsFalse(applicationController.IsHostApplicationValid);

                object officeDocument = applicationController.OpenDocument(m_testPath + "test.xls", false);
                Assert.IsNotNull(officeDocument);
                Assert.IsTrue(applicationController.IsHostApplicationValid);

                application = (Excel._Application)applicationController.HostApplication;
                try
                {
                    Assert.AreEqual(1, application.Workbooks.Count);
                    Assert.IsInstanceOf(typeof(Excel.Workbook), officeDocument);
                }
                finally
                {
                    if (null != officeDocument)
                        Marshal.ReleaseComObject(officeDocument);

                    officeDocument = null;
                }
            }
        }
        public void TestCloseDocumentOnAlreadyClosedDocument()
        {
            using (ExcelApplicationController applicationController = new ExcelApplicationController(true))
            {
                applicationController.CreateHostApplication(2000);
                Excel.Workbook workbook = (Excel.Workbook)applicationController.OpenDocument(m_testPath + "test.xls", false);
                Assert.IsNotNull(workbook);

                try
                {
                    object vtMissing = Missing.Value;
                    workbook.Close(vtMissing, vtMissing, vtMissing);

                    applicationController.CloseDocument(workbook, false);
                }
                finally
                {
                    if (null != workbook)
                        Marshal.ReleaseComObject(workbook);

                    workbook = null;
                }
            }
        }
        public void TestEventsDisabledForAutoMacros()
        {
            using (ExcelApplicationController applicationController = new ExcelApplicationController(true))
            {
                applicationController.CreateHostApplication(2000);

                Excel.Workbook workbook = null;

                try
                {
                    workbook = (Excel.Workbook)applicationController.OpenDocument(m_testPath + "McKinzie bounce-test2.xls", false);
                    Assert.IsNotNull(workbook);
                    Assert.IsTrue(workbook.Saved);
                    Excel._Application application = (Excel._Application)applicationController.HostApplication;
                    Assert.IsNotNull(application);
                    Assert.IsFalse(application.EnableEvents);
                    applicationController.CloseDocument(workbook, false);
                }
                finally
                {
                    if (null != workbook)
                        Marshal.ReleaseComObject(workbook);

                    workbook = null;
                }
            }
        }
        public void TestCloseDocument()
        {
            using (ExcelApplicationController applicationController = new ExcelApplicationController(true))
            {
                applicationController.CreateHostApplication(2000);
                object officeDocument = applicationController.OpenDocument(m_testPath + "test.xls", false);
                Assert.IsNotNull(officeDocument);

                Excel.Workbooks workbooks = null;
                try
                {
                    Assert.IsInstanceOf(typeof(Excel.Workbook), officeDocument);
                    Excel._Application application = (Excel._Application)applicationController.HostApplication;
                    workbooks = (Excel.Workbooks)application.Workbooks;
                    Assert.AreEqual(1, workbooks.Count);

                    applicationController.CloseDocument(officeDocument, false);
                    Assert.AreEqual(0, workbooks.Count);
                }
                finally
                {
                    if (null != officeDocument)
                        Marshal.ReleaseComObject(officeDocument);

                    officeDocument = null;

                    if (null != workbooks)
                        Marshal.ReleaseComObject(workbooks);

                    workbooks = null;
                }
            }
        }
        private void TestOpenWithMergableDoc_Worker()
        {
			using (ExcelApplicationController controller = new ExcelApplicationController(true))
            {
                controller.CreateHostApplication(2000);
				object officeDocument = controller.OpenDocument(CopyFile(Path.Combine(m_testPath, "DoYouWantToMerge.xls")), false);
                Assert.IsNotNull(officeDocument);
                try
                {
                    Assert.IsInstanceOf(typeof(Excel.Workbook), officeDocument);
                }
                finally
                {
                    if (null != officeDocument)
                        Marshal.ReleaseComObject(officeDocument);

                    officeDocument = null;
                }
            }
        }
 public void TestOpenDocumentInvalidHostApplication()
 {
     using(ExcelApplicationController applicationController = new ExcelApplicationController(true))
     {
         applicationController.OpenDocument(m_testPath + "test.xls", false);
     }
 }
 public void TestOpenDocumentMissingFile()
 {
     using(ExcelApplicationController applicationController = new ExcelApplicationController(true))
     {
         applicationController.CreateHostApplication(2000);
         applicationController.OpenDocument(m_testPath + "doesNotExist.xls", false);
     }
 }
 public void TestOpenInvalidDocument()
 {
     using(ExcelApplicationController applicationController = new ExcelApplicationController(true))
     {
         applicationController.CreateHostApplication(2000);
         applicationController.OpenDocument(null, false);
     }
 }