Exemple #1
0
 private bool IsReloadingPossible(COMException exception)
 {
     _tracer.TraceException(exception);
     return(COM_Error.IsRPC_ServerIsUnavailable(exception) ||
            COM_Error.RemoteProcCallFailed(exception));
 }
Exemple #2
0
        private string GetDocumentText(object path, int tryCount)
        {
            if (tryCount++ > 1)
            {
                return(string.Empty);
            }
            Word.Documents documents = null;
            try
            {
                if (_MSWord == null)
                {
                    if (!LoadWordInstance())
                    {
                        return(string.Empty);
                    }
                }
                documents = _MSWord.Documents;
            }
            catch (InvalidCastException exception)
            {
                _tracer.TraceException(exception);
                return(GetDocumentTextWithReloadingWord(path, tryCount));
            }
            catch (System.Runtime.InteropServices.COMException exception)
            {
                _tracer.TraceException(exception);
                if (IsReloadingPossible(exception))
                {
                    return(GetDocumentTextWithReloadingWord(path, tryCount));
                }
                return(string.Empty);
            }
            catch (Exception exception)
            {
                _tracer.TraceException(exception);
                return(string.Empty);
            }

            Word.Document doc      = null;
            string        bodyText = string.Empty;
            object        pass     = "******";

            try
            {
                DisableAutomationSecurity();
                doc = documents.Open(ref path, ref FALSE, ref TRUE, ref FALSE,
                                     ref pass,
                                     ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue,
                                     ref MissingValue,
                                     ref MissingValue, ref MissingValue);

                Word.Range range = doc.Content;
                bodyText = range.Text;
            }
            catch (System.Runtime.InteropServices.COMException exception)
            {
                _tracer.TraceException(exception);
                if (IsReloadingPossible(exception))
                {
                    _tracer.Trace("Try to load new instance of MS Word");
                    return(GetDocumentTextWithReloadingWord(path, tryCount));
                }
                if (COM_Error.CouldNotOpenMacroStorage(exception))     //This exception leads to not closed documents
                {
                    LoadWordInstance();
                    return(string.Empty);
                }
            }
            catch (InvalidCastException exception)
            {
                _tracer.TraceException(exception);
                return(GetDocumentTextWithReloadingWord(path, tryCount));
            }
            catch (Exception exception)
            {
                _tracer.TraceException(exception);
                return(string.Empty);
            }
            RestoreAutomationSecurity();

            try
            {
                if (doc != null)
                {
                    doc.Close(ref FALSE, ref MissingValue, ref MissingValue);
                }
                return(bodyText);
            }
            catch (System.Runtime.InteropServices.COMException exception)
            {
                if (IsReloadingPossible(exception))
                {
                    return(GetDocumentTextWithReloadingWord(path, tryCount));
                }
            }
            catch (InvalidCastException exception)
            {
                _tracer.TraceException(exception);
                return(GetDocumentTextWithReloadingWord(path, tryCount));
            }
            catch (Exception exception)
            {
                _tracer.TraceException(exception);
            }
            return(string.Empty);
        }