0

I have a word document with text paragraphs as well as tables. I want to search for a table which text starts with "This Act has been update to". The table has one one cell. row 1, column 1. How do i find this table using code. Not familiar with using tables and word interop. Thanks.

2 Answers 2

4

i have partially copied this example from one of my projects (replaced/removed some code - so it may contains syntax errors), but if you are already working with interop and early binding - it might be helpfull

using Word = Microsoft.Office.Interop.Word;

var wordApplication = new Word.Application();
var filename = "C:\test.doc";
Word.Application wordApp = null;

if (wordApplication != null)
    wordApp = wordApplication as Word.ApplicationClass;

Word.Document wordDoc = null;
if (File.Exists(fileName.ToString()) && wordApp != null)
        {
            object readOnly = isReadonly;
            object isVisible = true;
            object missing = System.Reflection.Missing.Value;
            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 isVisible, ref missing, ref missing, ref missing,
                                             ref missing);
        }

Word.Document wordDocument = wordDoc as Word.Document;
int tablecount = wordDocument.Tables.Count;
wordDocument.Activate();
for (int i = 1; i <= tablecount; i++)
{
Word.Table wTable = wordDocument.Tables[i];
Word.Cell pCell = wTable.Cell(1, 1);
if (pCell.Range.Text == "This Act has been update to") 
    {
        MessageBox.Show("Bingo !!!");
        break;
    }
}
1
  • The Requested Component does not exist at wordDocument.Tables[0] eventhough the tables count is 1 Jan 15, 2020 at 10:38
0

NetOffice has something to offer for you. Another solution is to save your word file in HTML format then use HTML Agility Pack.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.