locked
compare two word document (both contain table) and find matches RRS feed

  • Question

  • i want to compare two word document and find matches. documents both contains table. the source could be in another document or richtextbox. 
    i started this example but i don't know how can i change it for compare.

    this opens a word doc. and in a textbox, i am writing the word that i want to find and matches' color changing red. its ok but the main text is so long. i got 2500 lines and when found a match, i must scroll page an find it myself. on the other hand if i couldnt do it programmatically i must to do it nearly 600 times. but if its possible i want to compare all words in table. 

    thank you for your time.
    private void button1_Click(object sender, EventArgs e)
            {
                openFileDialog1.ShowDialog();
                textBox1.Text = openFileDialog1.FileName;
    
                if (textBox1.Text != "")
                {
                    object val= System.Reflection.Missing.Value;
                    object readWrite = false;
                    object show = true;
                    object path = textBox1.Text;
                    Microsoft.Office.Interop.Word.Application metin;
                    txt = new Microsoft.Office.Interop.Word.Application();
                    txt.Visible = false;
                    txt.Documents.Add(ref path, ref val, ref readWrite, ref show);
                    txt.Documents.Application.ActiveDocument.Range(ref val, ref val).InsertAfter(richTextBox1.Text);
                    richTextBox2.Text = txt.Documents.Application.ActiveDocument.Range(ref val, ref val).Text;
      }
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                int i = 0;
                string mainTxt = richTextBox2.Text;
                string childTxt = textBox2.Text;
                int ndx = mainTxt.IndexOf(childTxt);
                while (ndx != -1)
                {
                    ndx = mainTxt.IndexOf(childTxt, ndx + 1);
                    i++;
                }
                
                MatchCollection mc = Regex.Matches(mainTxt, childTxt, RegexOptions.IgnoreCase);
                if (mc.Count == 0)
                {
                    MessageBox.Show("no matches");
                    textBox2.Text = "";
                }
    
                foreach (Match found in mc)
                {
                    richTextBox2.Select(found.Index, found.Length);
                    richTextBox2.SelectionColor =Color.Red;
                 }
    
    
            }

    Wednesday, December 30, 2009 7:43 PM

All replies

  • Hi,

    It appears the code above will check if the text in textBox2 matches the text in richTextBox2.
    Do you mean you need to change the text in textBox2 every time? and you will need to set the text and perform the match 600 times ?

    I'm afraid there is no means to compare all words in tables. I think this is the only way to do the job.

    Please correct me if I misunderstood.
    Harry
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, January 5, 2010 2:56 AM
  • Hi Harry Zhu, thank you for your answer. 

    yes, your thought is correct, i am changing text in textBox2 manual. but the text which i changed, in a table so if i use a loop, i don't know how can i get the data from first column of table, because when i create the word doc. in richTextBox, it isn't looking like table anymore.  and the mainText i want to use is in another table and when i compare them, i need the data which in second column (or both).  so if i will use a loop, first i must create a table. i don't know much thing about word application and confused. now i am looking for create tables.
    thank you again. and i would be happy if you or someone helps me. 
    Tuesday, January 5, 2010 11:41 AM
  • Hi,

    I have no idea about the table you are talking about.
    Could you please show us more info ?
    What do you want to compare?

    Harry
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, January 6, 2010 2:17 AM