Beispiel #1
0
		public static void Revoke()
		{
			if (null == m_newFilter)
				return;

			try
			{
				ApplicationControllerTrace trace = new ApplicationControllerTrace("Unregister message filter");

				IOleMessageFilter oldFilter = null;
				long hresult = CoRegisterMessageFilter(m_oldFilter, out oldFilter);
				if (hresult != S_OK)
				{
					trace.Log(String.Format("Could not revoke MessageFilter (hresult = 0x{0:X} - possibly called from MTA thread (unsupported))", hresult));
				}

				if (null != oldFilter)
				{
					StringBuilder message = new StringBuilder("The previous message filter is ");
					message.Append(oldFilter.GetType());
					trace.Log(message.ToString());
				}
			}
			catch (System.Exception e)
			{
				ApplicationControllerTrace trace = new ApplicationControllerTrace("Unregister message filter");
				trace.Log(e.Message);
			}
			finally
			{
				m_newFilter = null;
				m_oldFilter = null;
			}
		}
        protected override void Dispose(bool disposing)
        {
            lock (m_lock)
            {
                if (m_disposed)
                    return;

                ApplicationControllerTrace trace = new ApplicationControllerTrace();

                m_disposed = true;
                                
                try
                {
                    Quit();
                }
                catch (Exception e)
                {
                    trace.Log(e);
                    // ignore.
                }
                finally
                {
                    trace.Log("Releasing word application");

                    if (null != m_hostApplication)
                        Marshal.FinalReleaseComObject(m_hostApplication);

                    m_hostApplication = null;
                }

                base.Dispose(disposing);
            }
        }
Beispiel #3
0
		public static void Register()
		{
			if (null != m_newFilter)
				return;

			ApplicationControllerTrace trace = new ApplicationControllerTrace("Register message filter");

			MessageFilter newFilter = new MessageFilter();
			long hresult = CoRegisterMessageFilter(newFilter, out m_oldFilter);
			if (hresult == S_OK)
			{
				m_newFilter = newFilter;
			}
			else
			{
				trace.Log(String.Format("Could not register MessageFilter (hresult = 0x{0:X} - possibly called from MTA thread (unsupported))", hresult));
			}

			if (null == m_oldFilter)
			{
				trace.Log("The process did not contain a message filter");
			}
			else
			{
				StringBuilder message = new StringBuilder("The previous message filter is ");
				message.Append(m_oldFilter.GetType());
				trace.Log(message.ToString());
			}
		}
		protected override void Dispose(bool disposing)
		{
			ApplicationControllerTrace trace = new ApplicationControllerTrace("Dispose requested");
			lock (m_lock)
			{
				if (m_disposed)
					return;

				m_disposed = true;

				trace.Log("Dispose started");

				try
				{
					Quit();
				}
				catch (System.Exception e)
				{
					trace.Log(e.Message);
					// ignore.
				}
				finally
				{
					trace.Log("Releasing host application");

					if (null != m_hostApplication)
						Marshal.FinalReleaseComObject(m_hostApplication);

					m_hostApplication = null;

					trace.Log("Dispose completed");
				}

				base.Dispose(disposing);
			}
		}
        private void MarkTemplatesAsSaved()
        {
            if (null == m_hostApplication)
                throw new ArgumentNullException("m_hostApplication", "Invalid host application pointer");

            Templates templates = null;
            try
            {
                templates = m_hostApplication.Templates;
                foreach (Template template in templates)
                {
                    template.Saved = true;
                    Marshal.ReleaseComObject(template);
                }
            }
            catch (System.Exception e)
            {
                ApplicationControllerTrace trace = new ApplicationControllerTrace();
                trace.Log(e);
            }
            finally
            {
                if (null != templates)
                    Marshal.ReleaseComObject(templates);
            }
        }
        private void Quit(bool forceQuit)
        {
            base.Quit();

            lock (m_lock)
            {
                if (null == m_hostApplication)
                {
                    return;
                }

                ApplicationControllerTrace trace = new ApplicationControllerTrace();

                try
                {
                    if (m_hostApplication.Visible && !forceQuit)
                        return;

                    DisableMacros(true);
                    MarkTemplatesAsSaved();

                    object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
                    object originalFormat = null;
                    object routeDocument = false;

                    m_hostApplication.Options.ConfirmConversions = _confirmConversion;
                    m_hostApplication.Quit(ref saveChanges, ref originalFormat, ref routeDocument);
                }
                catch (COMException e)
                {
                    trace.Log(e);

                    if (RPC_E_DISCONNECTED != e.ErrorCode)
                        throw e;
                }
                finally
                {
                    if (null != m_hostApplication)
                        Marshal.FinalReleaseComObject(m_hostApplication);

                    m_hostApplication = null;
                }
            }
        }
		private void ScreenUpdating(bool enabled)
		{
			if (!IsResponding())
				return;

			ApplicationControllerTrace trace = new ApplicationControllerTrace();
			try
			{
				if (null == m_hostApplication)
					return;


				m_hostApplication.ScreenUpdating = enabled;
			}
			catch (System.Exception e)
			{
				trace.Log(e);
				// Ignore.
			}
		}
		private bool IsResponding()
		{
			ApplicationControllerTrace trace = new ApplicationControllerTrace();
			Excel.Workbooks workbooks = null;
			try
			{
				if (null == m_hostApplication)
					return false;

				workbooks = m_hostApplication.Workbooks;

				int count = workbooks.Count;

				return true;
			}
			catch (System.Exception e)
			{
				trace.Log(e);
				return false;
			}
			finally
			{
				if (null != workbooks)
					Marshal.ReleaseComObject(workbooks);

				workbooks = null;
			}
		}
		public override void Quit()
		{
			base.Quit();

			lock (m_lock)
			{
				if (null == m_hostApplication)
					return;

				ApplicationControllerTrace trace = new ApplicationControllerTrace("Quit started");

				try
				{
					if (m_hostApplication.Visible)
						return;

					WaitForExcelToRespond();

					m_hostApplication.DisplayAlerts = false;
					m_hostApplication.Quit();

					trace.Log("Quit complete");
				}
				catch (COMException e)
				{
					trace.Log(e);

					if (RPC_E_DISCONNECTED != e.ErrorCode)
						throw e;
				}
				finally
				{
					trace.Log("Releasing application");
					if (null != m_hostApplication)
						Marshal.FinalReleaseComObject(m_hostApplication);

					m_hostApplication = null;
				}
			}
		}
		public override void CloseDocument(object document, bool saveChanges)
		{
			if (null == document)
				return;

			base.CloseDocument(document, saveChanges);

			WaitForExcelToRespond();

			ApplicationControllerTrace trace = new ApplicationControllerTrace();

			if (!(document is Excel.Workbook))
			{
				StringBuilder errorMessage = new StringBuilder();
				errorMessage.Append("Expected the document to be of type 'Excel.Workbook', but was ");
				errorMessage.Append(document.GetType().ToString());
				throw new ArgumentException(errorMessage.ToString(), "document");
			}

			if (null == m_hostApplication)
				throw new ArgumentNullException("m_hostApplication", "Invalid host application pointer");

			Excel.Workbook workbook = null;
			try
			{
				workbook = (Excel.Workbook) document;
				if (null == workbook)
					return;

				object vtSaveChanges = saveChanges;
				object vtFalse = false;
				object vtMissing = System.Reflection.Missing.Value;
				workbook.Close(vtSaveChanges, vtMissing, vtFalse);
			}
			catch (COMException e)
			{
				trace.Log(e);

				if ((RPC_E_DISCONNECTED != e.ErrorCode) && (E_NOINTERFACE != e.ErrorCode))
					throw e;
			}
			finally
			{
				if (null != workbook)
					Marshal.ReleaseComObject(workbook);

				workbook = null;

				ScreenUpdating(true);
			}
		}
		private Excel.Workbook FindOpenDocument(string filename)
		{
			if (null == m_hostApplication)
				throw new ArgumentNullException("m_hostApplication", "Invalid host application pointer");

			ApplicationControllerTrace trace = new ApplicationControllerTrace();

			Excel.Workbooks workbooks = m_hostApplication.Workbooks;
			try
			{
				foreach (Excel.Workbook workbook in workbooks)
				{
					if (0 == string.Compare(filename, workbook.FullName, true))
						return workbook;

					Marshal.ReleaseComObject(workbook);
				}
			}
			catch (COMException e)
			{
				trace.Log(e);
				throw e;
			}
			finally
			{
				if (null != workbooks)
					Marshal.ReleaseComObject(workbooks);

				workbooks = null;
			}
			return null;
		}
		private void WaitForWorkbookOpened(string filename)
		{
			string workbookName = filename;
			int index = workbookName.LastIndexOfAny(new char[] { '\\', '/' });
			if (-1 != index)
			{
				workbookName = workbookName.Substring(++index, workbookName.Length - index);
			}

			if (null == m_hostApplication)
				throw new ArgumentNullException("m_hostApplication", "Invalid host application pointer");

			ApplicationControllerTrace trace = new ApplicationControllerTrace();

			int attempts = 200; // about 20 seconds
			while (0 <= attempts--)
			{
				Excel.Workbook workbook = null;
				try
				{
					workbook = m_hostApplication.Workbooks[workbookName];
					return;
				}
				catch (Exception e)
				{
					trace.Log(e);
					// Ignore
				}
				finally
				{
					if (null != workbook)
						Marshal.ReleaseComObject(workbook);

					workbook = null;
				}
				System.Threading.Thread.Sleep(100);
			}
			throw new Exception(string.Format("Failed to open workbook [{0}], wait time expired", filename));
		}
		private Excel.Workbook DoOpenDocument(string filename, bool readOnly)
		{
			if (null == m_hostApplication)
				throw new ArgumentNullException("m_hostApplication", "Invalid host application pointer");

			WaitForExcelToRespond();

			Excel.Workbooks workbooks = m_hostApplication.Workbooks;

			ApplicationControllerTrace trace = new ApplicationControllerTrace();

			int newDocumentCount = workbooks.Count + 1;
			Excel.Workbook workbook = null;
			try
			{
				workbook = TryToOpenDocument(workbooks, filename, readOnly, 1);

				WaitForWorkbookOpened(filename);
			}
			catch (Exception e)
			{
				trace.Log(e);

				if (newDocumentCount != workbooks.Count)
					throw e;

				workbook = workbooks[newDocumentCount];
			}
			finally
			{
				if (null != workbooks)
					Marshal.ReleaseComObject(workbooks);

				workbooks = null;
			}
			workbook.Saved = true;
			return workbook;
		}
		private void SaveWorkbook(Excel.Workbook workbook)
		{
			ApplicationControllerTrace trace = new ApplicationControllerTrace();
			try
			{
				if (!workbook.Saved)
					workbook.Save();
			}
			catch (Exception e)
			{
				trace.Log(e);
			}
		}