Exemplo n.º 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();
     }
 }
Exemplo n.º 2
0
        //public void SetPage(Orientation orientation, double width, double height, double topMargin, double leftMargin, double rightMargin, double bottomMargin)
        //{
        //    wordDoc.PageSetup.PageWidth = wordApp.CentimetersToPoints((float)width);
        //    wordDoc.PageSetup.PageHeight = wordApp.CentimetersToPoints((float)height);
        //    if (orientation == Orientation.横板)
        //    {
        //        wordDoc.PageSetup.Orientation  = Microsoft.Office.Interop.Word.WdOrientation.wdOrientLandscape;
        //    }
        //    wordDoc.PageSetup.TopMargin = (float)(topMargin * 25);//上边距
        //    wordDoc.PageSetup.LeftMargin = (float)(leftMargin * 25);//左边距
        //    wordDoc.PageSetup.RightMargin = (float)(rightMargin * 25);//右边距
        //    wordDoc.PageSetup.BottomMargin = (float)(bottomMargin * 25);//下边距
        //}

        //public void SetPage(Orientation orientation, double topMargin, double leftMargin, double rightMargin, double bottomMargin)
        //{
        //    SetPage(orientation, 21, 29.7, topMargin, leftMargin, rightMargin, bottomMargin);
        //}
        //public void SetPage(double topMargin, double leftMargin, double rightMargin, double bottomMargin)
        //{
        //    SetPage(Orientation.竖板, 21, 29.7, topMargin, leftMargin, rightMargin, bottomMargin);
        //}
        #endregion


        #region  - 关闭当前文档 -
        public bool CloseDocument()
        {
            try
            {
                object Nothing          = System.Reflection.Missing.Value;
                object doNotSaveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
                wordDoc.Close(ref doNotSaveChanges, ref Nothing, ref Nothing);
                wordDoc = null; return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
Exemplo n.º 3
0
 public bool CloseDocument()
 {
     try
     {
         object doNotSaveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
         oDoc.Close(ref doNotSaveChanges, ref Nothing, ref Nothing);
         oDoc = null;
         return(true);
     }
     catch (Exception)
     {
         return(false);
     }
 }
Exemplo n.º 4
0
        public bool Open(string filePath, bool isVisible)
        {
            try
            {
                oWord.Visible = isVisible;

                object path = filePath;
                oDoc = oWord.Documents.Open(ref path,
                                            ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
                                            ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
                                            ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);

                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
Exemplo n.º 5
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);
            }
        }
Exemplo n.º 6
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;
            }
        }
Exemplo n.º 7
0
 public void OpenNewWordFileToEdit()
 {
     oDoc = oWord.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
 }
Exemplo n.º 8
0
        public bool CloseDocument()
        {
            try
            {
                object doNotSaveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
                oDoc.Close(ref doNotSaveChanges, ref Nothing, ref Nothing);
                oDoc = null;
                return true;

            }
            catch (Exception)
            {
                return false;
            }
        }
Exemplo n.º 9
0
        public bool Open(string filePath, bool isVisible)
        {
            try
            {
                oWord.Visible = isVisible;

                object path = filePath;
                oDoc = oWord.Documents.Open(ref path,
                ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
                ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
                ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
Exemplo n.º 10
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);
        }
Exemplo n.º 11
0
 public void OpenNewWordFileToEdit()
 {
     oDoc = oWord.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
 }