2013-07-05 41 views
0

我有一个文本段落以及表格的文档。我想搜索一个文本以“此法已更新到”开头的表格。该表有一个单元格。第1行,第1列。如何使用代码找到此表。不熟悉使用表格和单词互操作。谢谢。Word互操作使用表

回答

1

我已经部分地从我的项目之一 复制这个例子(更换/删除一些代码 - 因此它可能包含语法错误),但如果你已经与互操作和早期绑定工作 - 这可能是有益的

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