コード例 #1
0
        // Compare the two word documents
        private void button3_Click(object sender, EventArgs e)
        {
            doc = wordApp.CompareDocuments(doc1, doc2, MsWord.WdCompareDestination.wdCompareDestinationNew,
                                           MsWord.WdGranularity.wdGranularityWordLevel, true, true, true, true, true, true, true, true, true, true, "", false);
            if
            (doc1.Content.Text != doc2.Content.Text)
            {
                MessageBox.Show("There are differences between the 2 documents. Please click ok to view diffences");

                // Close first document
                doc1.Close(missing, missing, missing);

                // Close second document
                doc2.Close(missing, missing, missing);

                // Show the compared document
                wordApp.Visible = true;

                //var FileName = "C;/Docs/Comparrison.doc";
                doc.SaveAs2(@"C:\Docs\Comparison Result " + DateTime.Now.ToString("MM_dd_yyyy_hh_mm") + ".docx");
                Close();
            }

            else
            {
                MessageBox.Show("Both Documents are identical. Please click ok to close");

                Close();
            }



            // Quit the application
        }
コード例 #2
0
        // Compare the two word documents
        private void button3_Click(object sender, EventArgs e)
        {
            doc = wordApp.CompareDocuments(doc1, doc2, MsWord.WdCompareDestination.wdCompareDestinationNew,
                                           MsWord.WdGranularity.wdGranularityWordLevel, true, true, true, true, true, true, true, true, true, true, "", false);

            // Close first document
            doc1.Close(missing, missing, missing);

            // Close second document
            doc2.Close(missing, missing, missing);

            // Show the compared document
            wordApp.Visible = true;

            // Quit the application
            Close();
        }
コード例 #3
0
        private void Button3_Click(object sender, EventArgs e)
        {
            Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
            wordApp.Visible       = false;
            wordApp.DisplayAlerts = WdAlertLevel.wdAlertsNone;
            object wordTrue  = (object)true;
            object wordFalse = (object)false;
            object missing   = Type.Missing;

            Microsoft.Office.Interop.Word.Document doc1 = wordApp.Documents.Open(textBox1.Text, ref missing, ref wordFalse, ref wordFalse, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref wordTrue, ref missing, ref missing, ref missing, ref missing);

            // read 2nd document
            Microsoft.Office.Interop.Word.Document doc2 = wordApp.Documents.Open(textBox2.Text, ref missing, ref wordFalse, ref wordFalse, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

            // compare the two documents
            Microsoft.Office.Interop.Word.Document doc = wordApp.CompareDocuments(doc1, doc2, WdCompareDestination.wdCompareDestinationNew, WdGranularity.wdGranularityWordLevel, true, true, true, true, true, true, true, true, true, true, "", false);
        }
コード例 #4
0
        public void Compare()
        {
            Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
            wordApp.Visible       = false;
            wordApp.DisplayAlerts = WdAlertLevel.wdAlertsNone;
            object wordTrue  = (object)true;
            object wordFalse = (object)false;

            object fileToOpen = _sourceFilePath;
            object missing    = Type.Missing;

            Microsoft.Office.Interop.Word.Document doc1 = wordApp.Documents.Open(ref fileToOpen,
                                                                                 ref missing, ref wordFalse, ref wordFalse, ref missing,
                                                                                 ref missing, ref missing, ref missing, ref missing,
                                                                                 ref missing, ref missing, ref wordTrue, ref missing,
                                                                                 ref missing, ref missing, ref missing);

            object fileToOpen1 = _destinationFilePath;

            Microsoft.Office.Interop.Word.Document doc2 = wordApp.Documents.Open(ref fileToOpen1,
                                                                                 ref missing, ref wordFalse, ref wordFalse, ref missing,
                                                                                 ref missing, ref missing, ref missing, ref missing,
                                                                                 ref missing, ref missing, ref missing, ref missing,
                                                                                 ref missing, ref missing, ref missing);

            Microsoft.Office.Interop.Word.Document doc = wordApp.CompareDocuments(doc1, doc2, WdCompareDestination.wdCompareDestinationNew, WdGranularity.wdGranularityCharLevel,
                                                                                  true, true, true, true, true, true, true, true, true, true, "", false);

            object tempFile = "c:\\result.docx";

            doc.SaveAs2(ref tempFile, ref missing, ref missing, ref missing, ref wordFalse, ref missing, ref wordTrue, ref missing, ref missing,
                        ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref wordTrue, ref missing);


            try
            {
                doc1.Close();
                doc2.Close();
                doc.Close();
            }
            finally { }
        }
コード例 #5
0
        /// <summary>
        /// 比较文件
        /// </summary>
        /// <param name="filePath1">文件路径1</param>
        /// <param name="filePath2">文件路径2</param>
        /// <param name="savePath">文件保存路径</param>
        public static string CompareFile(string filePath1, string filePath2, string savePath)
        {
            Word.Application wordApp = null;
            Word.Document    doc     = null;
            Word.Document    doc1    = null;
            Word.Document    doc2    = null;

            try
            {
                _log.Debug($"OfficeHelper CompareFile Start.");
                wordApp         = new Word.Application();
                wordApp.Visible = false;
                object wordTrue  = (object)true;
                object wordFalse = (object)false;
                object missing   = Type.Missing;
                object document1 = filePath1;
                object document2 = filePath2;

                // 对比文件1
                doc1 = wordApp.Documents.Open(ref document1, ref missing, ref wordFalse, ref wordFalse, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref wordTrue, ref missing, ref missing, ref missing, ref missing);
                // 对比文件2
                doc2 = wordApp.Documents.Open(ref document2, ref missing, ref wordFalse, ref wordFalse, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

                // 清除文件备注
                var  oDocBuiltInProps    = doc2.BuiltInDocumentProperties;
                Type typeDocBuiltInProps = oDocBuiltInProps.GetType();
                //Set the Comments property.
                typeDocBuiltInProps.InvokeMember("Item", BindingFlags.Default | BindingFlags.SetProperty, null, oDocBuiltInProps, new object[] { "Comments", "" });
                doc2.Save();

                // 对比后文件
                doc = wordApp.CompareDocuments(doc1, doc2, Word.WdCompareDestination.wdCompareDestinationNew, Word.WdGranularity.wdGranularityWordLevel, true, true, true, true, true, true, true, true, true, true, "", false);

                _log.Debug("OfficeHelper CompareFile Save File Start");
                // 保存对比文件到本地
                object format          = Word.WdSaveFormat.wdFormatDocumentDefault; //Word文档的保存格式
                object documentCompare = $"{savePath}\\{doc.Name}";
                doc.SaveAs2(documentCompare, format);
                _log.Debug("OfficeHelper CompareFile Save File End");

                // 对比文件路径
                var cFilePath = $"{documentCompare}.docx";
                return(cFilePath);
            }
            catch (Exception ex)
            {
                _log.Debug($"OfficeHelper CompareFile Exception: {ex.Message}");
                throw ex;
            }
            finally
            {
                KillProcessesTask("WINWORD");

                try
                {
                    if (doc != null)
                    {
                        doc.Close();
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(doc);
                    }
                }
                catch (Exception ex)
                {
                    doc = null;
                    _log.Debug($"OfficeHelper CompareFile DocClose Finally Exception: {ex.Message}");
                }
                try
                {
                    if (doc1 != null)
                    {
                        doc1.Close();
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(doc1);
                    }
                }
                catch (Exception ex)
                {
                    doc1 = null;
                    _log.Debug($"OfficeHelper CompareFile Doc1Close Finally Exception: {ex.Message}");
                }
                try
                {
                    if (doc2 != null)
                    {
                        doc2.Close();
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(doc2);
                    }
                }
                catch (Exception ex)
                {
                    doc2 = null;
                    _log.Debug($"OfficeHelper CompareFile Doc2Close Finally Exception: {ex.Message}");
                }
                try
                {
                    wordApp.Quit();
                }
                catch (Exception ex)
                {
                    wordApp = null;
                    _log.Debug($"OfficeHelper CompareFile AppClose Finally Exception: {ex.Message}");
                }
                _log.Debug($"OfficeHelper CompareFile End.");
            }
        }
コード例 #6
0
ファイル: WordDiff.cs プロジェクト: CIOIL/WordCompare
        static void Main(string[] args)
        {
            //
            //      Check for filenames and validate files exist
            //
            if (args.Length != 2)
            {
                System.Console.WriteLine("To compare two Microsoft Word documents use the following syntax:");
                System.Console.WriteLine("\tWordDiff [Document1] [Document2]");
                return;
            }

            object file1 = args[0];
            object file2 = args[1];

            if (!System.IO.File.Exists((string)file1))
            {
                System.Console.WriteLine("File does not exist: {0}", file1);
                return;
            }
            object fullPath1 = System.IO.Path.GetFullPath((string)file1);

            if (!System.IO.File.Exists((string)file2))
            {
                System.Console.WriteLine("File does not exist: {0}", file2);
                return;
            }
            object fullPath2 = System.IO.Path.GetFullPath((string)file2);

            //
            //      Open Word and compare files
            //
            MSWord.Application msWordApp = new MSWord.Application();
            msWordApp.Visible = false;
            object trueObj    = (object)true;
            object falseObj   = (object)false;
            object missingObj = Type.Missing;

            MSWord.Document msWordDocument1 = msWordApp.Documents.Open(ref fullPath1, ref missingObj, ref falseObj, ref falseObj,
                                                                       ref missingObj, ref missingObj, ref missingObj, ref missingObj, ref missingObj, ref missingObj, ref missingObj,
                                                                       ref trueObj, ref missingObj, ref missingObj, ref missingObj, ref missingObj);

            MSWord.Document msWordDocument2 = msWordApp.Documents.Open(ref fullPath2, ref missingObj, ref falseObj, ref falseObj,
                                                                       ref missingObj, ref missingObj, ref missingObj, ref missingObj, ref missingObj, ref missingObj, ref missingObj,
                                                                       ref missingObj, ref missingObj, ref missingObj, ref missingObj, ref missingObj);

            MSWord.Document msWordDocumentDiff = msWordApp.CompareDocuments(msWordDocument1, msWordDocument2,
                                                                            MSWord.WdCompareDestination.wdCompareDestinationNew, MSWord.WdGranularity.wdGranularityWordLevel,
                                                                            true, true, true, true, true, true, true, true, true, true, "", true);

            msWordDocument1.Close(ref missingObj, ref missingObj, ref missingObj);
            msWordDocument2.Close(ref missingObj, ref missingObj, ref missingObj);

            //
            //      Make sure Word is active and in the foreground
            //
            msWordApp.Visible = true;
            msWordApp.Activate();
            msWordApp.ActiveWindow.SetFocus();
            SetForegroundWindow((System.IntPtr)msWordApp.ActiveWindow.Hwnd);

            return;
        }
コード例 #7
0
        public static Document compared(Document memo, Document template)
        {
            var ComparedDocuments = wordApp.CompareDocuments(template, memo);

            return(ComparedDocuments);
        }