Example #1
0
        /// <summary>
        /// Loads the file. This method is protected, thus clients should call
        /// LoadDocument method of the IDocument abstract class.
        /// </summary>
        protected override bool Load(string documentPath)
        {
            if (app == null)
            {
                app            = new global::Microsoft.Office.Interop.Word.ApplicationClass();
                app.Visible    = false;
                hasInternalApp = true;
            }

            object oFileName = documentPath;
            object nullobj   = System.Reflection.Missing.Value;

            // TODO: If its already loaded ?!

            try
            {
                currentDoc = app.Documents.Open(ref oFileName, ref nullobj, ref nullobj, ref nullobj,
                                                ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj,
                                                ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj);
                hasInternalDoc = true;
            }
            catch (Exception)
            {
                // TODO: Consider this exception
                // sina: How about only returning false, and not throwing any exceptions?
                throw;
            }

            IsLoaded = true;
            return(true);
        }
Example #2
0
        /// <summary>
        /// Creates an instance of this class by providing a reference to the
        /// Word Document of the Microsoft Object Model, corresponding to this object
        /// of the MSWordDocument class.
        /// This approach is used when working with Microsoft Word Addins.
        /// </summary>
        public MSWordDocument(global::Microsoft.Office.Interop.Word.Document document)
        {
            currentDoc     = document;
            hasInternalDoc = false;
            app            = document.Application;
            hasInternalApp = false;

            // prevent loading a file by clients of this class by calling LoadDocument of the base class
            IsLoaded = true;
        }
Example #3
0
        /// <summary>
        /// Releases unmanaged and - optionally - managed resources
        /// </summary>
        /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
        protected virtual void Dispose(bool disposing)
        {
            if (!disposed)
            {
                if (disposing)
                {
                    // dispose managed resources
                    // e.g. call their Dispose()
                }
                // now dispose unmanaged resources
                // e.g. close files or database connections

                object nullobj = System.Reflection.Missing.Value;

                if (hasInternalDoc && currentDoc != null)
                {
                    try
                    {
                        //object saveChanges = false;
                        currentDoc.Close(ref nullobj, ref nullobj, ref nullobj);
                    }
                    catch (Exception)
                    {
                        // Ignore
                    }
                    finally // FIXME: sina: finally block added by me, but not sure if it's correct
                    {
                        currentDoc     = null;
                        hasInternalDoc = false;
                    }
                }

                if (hasInternalApp && app.Documents.Count == 0)
                {
                    try
                    {
                        app.Quit(ref nullobj, ref nullobj, ref nullobj);
                    }
                    catch (Exception)
                    {
                        // Ignore
                    }
                    finally
                    {
                        app            = null;
                        hasInternalApp = false;
                    }
                }

                disposed = true;
            }
        }
Example #4
0
 /// <summary>
 /// This method is called by DocFactory's Dispose mthod.
 /// </summary>
 internal static void Cleanup()
 {
     try
     {
         object nullobj = System.Reflection.Missing.Value;
         foreach (Document doc in app.Documents)
         {
             doc.Close(ref nullobj, ref nullobj, ref nullobj);
         }
         if (hasInternalApp)
         {
             app.Quit(ref nullobj, ref nullobj, ref nullobj);
         }
     }
     catch (Exception)
     {
     }
     finally
     {
         app            = null;
         hasInternalApp = false;
     }
 }