使用表格进行字互操作 [英] Word interop using tables

查看:71
本文介绍了使用表格进行字互操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Word文档,其中包含文本段落和表格.我想搜索一个文本开头为该法案已更新为"的表格.该表有一个一格.第1列,第1列.如何使用代码查找此表.不熟悉使用表格和单词互操作.谢谢.

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.

推荐答案

我已经从我的一个项目中部分复制了此示例 (已替换/删除了一些代码-因此可能包含语法错误),但是如果您已经在使用互操作和早期绑定-可能会有所帮助

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

这篇关于使用表格进行字互操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆