none
Msword comparison is failing to compare Headers and Footers. RRS feed

  • Question

  • I would like to compare two MSWord files with difference in the headers and footers. I would like to highlight  the difference in the both files including the mismatches in headers and footers. However, MSWord comparison is failing to highlight the differences in header and footer in the generated document.

    The line of code that i used comparison.

    wordApp.CompareDocuments(Doc1, Doc2,

    WdCompareDestination.wdCompareDestinationNew, WdGranularity.wdGranularityWordLevel, True, True, True, True, True, True, True, True, True, True, "", False)

    If hope I'm missing the point here. Could you suggest the solution for this.

    Monday, January 30, 2012 12:59 PM

Answers

  • Hi arms5,

     

    This is my snippet. It works fine on my side. Would you please try it?

     

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using Word = Microsoft.Office.Interop.Word;
    using System.Diagnostics;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            private string strDoc1 { set; get; }
            private string strDoc2 { set; get; }
            private Word.Application WdApplication { set; get; }
            public Form1()
            {
                InitializeComponent();
            }
    
            private void setDoc(int index)
            {
                OpenFileDialog dialog = new OpenFileDialog();
                dialog.Filter = "Doc|*.docx";
                dialog.ShowDialog();
                switch (index)
                {
                    case 1:
                        strDoc1 = dialog.FileName;
                        label1.Text = strDoc1;
                        break;
                    case 2:
                        strDoc2 = dialog.FileName;
                        label2.Text = strDoc2;
                        break;
                }
            }
            private void button1_Click(object sender, EventArgs e)
            {
                setDoc(1);
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                setDoc(2);
            }
    
            private void button3_Click(object sender, EventArgs e)
            {
                Process[] process = Process.GetProcessesByName("WINWORD");
                //object missing = Type.Missing;
                object ostrDoc1 = strDoc1;
                object ostrDoc2 = strDoc2;
                if (process.Length > 0)
                {
                    WdApplication = (Word.Application)System.Runtime.InteropServices
                        .Marshal.GetActiveObject("Word.Application");
                }
                else
                {
                    WdApplication = new Word.Application();
                }
                WdApplication.Visible = true;
                Word.Document doc1 = WdApplication.Documents.Open(ref ostrDoc1);
                Word.Document doc2 = WdApplication.Documents.Open(ref ostrDoc2);
                WdApplication.CompareDocuments(doc1, doc2, 
                    Word.WdCompareDestination.wdCompareDestinationNew,
                    Word.WdGranularity.wdGranularityWordLevel);
            }
    
            private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                if (WdApplication != null)
                {
                    System.Runtime.InteropServices.Marshal
                        .ReleaseComObject(WdApplication);
                }
            }
        }
    }
    

    Have a good day,

     

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Friday, February 3, 2012 6:29 AM
    Moderator

All replies

  • Hi arms5,

     

    Thanks for posting in the MSDN Forum.

     

    Would you please tell me your Office version and Visual Studio version for further research?

     

    Have a good day,

     

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Wednesday, February 1, 2012 6:31 AM
    Moderator
  • Hi Tom,

    Thanks for the timely response, the Office version we are using is 2010 and where as the version of VSTS is 2010.

    Friday, February 3, 2012 4:10 AM
  • Hi arms5,

     

    This is my snippet. It works fine on my side. Would you please try it?

     

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using Word = Microsoft.Office.Interop.Word;
    using System.Diagnostics;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            private string strDoc1 { set; get; }
            private string strDoc2 { set; get; }
            private Word.Application WdApplication { set; get; }
            public Form1()
            {
                InitializeComponent();
            }
    
            private void setDoc(int index)
            {
                OpenFileDialog dialog = new OpenFileDialog();
                dialog.Filter = "Doc|*.docx";
                dialog.ShowDialog();
                switch (index)
                {
                    case 1:
                        strDoc1 = dialog.FileName;
                        label1.Text = strDoc1;
                        break;
                    case 2:
                        strDoc2 = dialog.FileName;
                        label2.Text = strDoc2;
                        break;
                }
            }
            private void button1_Click(object sender, EventArgs e)
            {
                setDoc(1);
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                setDoc(2);
            }
    
            private void button3_Click(object sender, EventArgs e)
            {
                Process[] process = Process.GetProcessesByName("WINWORD");
                //object missing = Type.Missing;
                object ostrDoc1 = strDoc1;
                object ostrDoc2 = strDoc2;
                if (process.Length > 0)
                {
                    WdApplication = (Word.Application)System.Runtime.InteropServices
                        .Marshal.GetActiveObject("Word.Application");
                }
                else
                {
                    WdApplication = new Word.Application();
                }
                WdApplication.Visible = true;
                Word.Document doc1 = WdApplication.Documents.Open(ref ostrDoc1);
                Word.Document doc2 = WdApplication.Documents.Open(ref ostrDoc2);
                WdApplication.CompareDocuments(doc1, doc2, 
                    Word.WdCompareDestination.wdCompareDestinationNew,
                    Word.WdGranularity.wdGranularityWordLevel);
            }
    
            private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                if (WdApplication != null)
                {
                    System.Runtime.InteropServices.Marshal
                        .ReleaseComObject(WdApplication);
                }
            }
        }
    }
    

    Have a good day,

     

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Friday, February 3, 2012 6:29 AM
    Moderator