예제 #1
0
 public void Close()
 {
     try
     {
         if (wordDoc != null)
         {
             object saveOption = Word.WdSaveOptions.wdDoNotSaveChanges;
             wordDoc.Close(ref saveOption, ref missing, ref missing);
         }
         if (wordApp != null)
         {
             object saveOption = Word.WdSaveOptions.wdDoNotSaveChanges;
             wordApp.Quit(ref saveOption, ref missing, ref missing);
         }
         if (wordApp != null)
         {
             System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApp);
             wordApp = null;
         }
     }
     catch (Exception e1)
     {
         Log.Error("DocOpen exception, and close exception. " + e1.Message);
     }
     finally
     {
         wordDoc = null;
         wordApp = null;
         closeProc(filename);
         GC.Collect();
     }
 }
예제 #2
0
        public bool CreateWord(bool isVisible)
        {
            try
            {
                oWord         = new Microsoft.Office.Interop.Word.Application();
                oWord.Visible = isVisible;

                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
예제 #3
0
        public bool Quit()
        {
            try
            {
                object saveOption = Word.WdSaveOptions.wdDoNotSaveChanges;
                oWord.Quit(ref saveOption, ref Nothing, ref Nothing);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(oWord);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(oDoc);
                oWord = null;
                oDoc  = null;
                odoc  = null;
                GC.Collect();

                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
예제 #4
0
        public bool Quit()
        {
            try
            {
                object saveOption = Word.WdSaveOptions.wdDoNotSaveChanges;
                oWord.Quit(ref saveOption, ref Nothing, ref Nothing);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(oWord);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(oDoc);
                oWord = null;
                oDoc = null;
                odoc = null;
                GC.Collect();

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
예제 #5
0
        public bool CreateWord(bool isVisible)
        {
            try
            {
                oWord = new Microsoft.Office.Interop.Word.Application();
                oWord.Visible = isVisible;

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
예제 #6
0
        public bool Open(string path)
        {
            try
            {
                Util.ConvertWord2PDF(path, Path.GetDirectoryName(path), Path.GetFileNameWithoutExtension(path) + ".pdf");
            }
            catch (Exception e)
            {
            }

            //if (Global.getPPTMaximize())
            //{
            minisizeProc();
            //}


            filepath = path;
            filename = Path.GetFileName(path);
            object FileName = filepath;
            object readOnly = true;


            wordApp = new Microsoft.Office.Interop.Word.Application(); //可以打开word程序
            wordApp.DisplayAlerts = WdAlertLevel.wdAlertsNone;
            wordApp.Visible       = true;



            try
            {
                wordDoc = wordApp.Documents.Open(ref FileName,
                                                 ref missing, ref readOnly, ref missing, ref missing, ref missing,
                                                 ref missing, ref missing, ref missing, ref missing, ref missing,
                                                 ref missing, ref missing, ref missing, ref missing, ref missing);

                if (wordApp != null && wordDoc != null)
                {
                    wordApp.ActiveWindow.View.Type = Microsoft.Office.Interop.Word.WdViewType.wdReadingView;
//Word.WdViewType.wdMasterView //大纲视图模式
//Word.WdViewType.wdNormalView //普通视图模式
//Word.WdViewType.wdOutlineView //和大纲视图模式类似
//Word.WdViewType.wdReadingView //阅读版式
//Word.WdViewType.wdPrintPreview //打印预览模式
//Word.WdViewType.wdPrintView //普通视图模式
//Word.WdViewType.wdWebView // 'Web版式视图


                    maximizeWordProc();
                    Word.WdStatistic stat = Word.WdStatistic.wdStatisticPages;
                    int nPage             = wordDoc.ComputeStatistics(stat, ref missing);
                    getPageInfo();
                    return(true);
                }
                else
                {
                    throw new Exception("openDoc failed.");
                }
            }
            catch (Exception ex)
            {
                int errorcode = 0;
                var w32ex     = ex as Win32Exception;
                if (w32ex == null)
                {
                    w32ex = ex.InnerException as Win32Exception;
                }
                if (w32ex != null)
                {
                    errorcode = w32ex.ErrorCode;
                }
                Log.Error("DocOpen exception. " + ex.Message + ", errorcode=" + errorcode);//Error HRESULT E_FAIL has been returned from a call to a COM component.

                Close();
            }
            finally
            {
            }
            return(false);
        }