Word interop using tables

南笙酒味 提交于 2019-12-13 06:24:48

问题


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.


回答1:


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;
    }
}



回答2:


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



来源:https://stackoverflow.com/questions/17484422/word-interop-using-tables

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!