如何使用 Office.Interop 确定书签所属的表格单元格

发布于 2024-08-09 09:06:28 字数 96 浏览 9 评论 0原文

我正在用 C# 创建一个应用程序。该应用程序需要根据模板创建 MS Word 文档。该模板包含一个表格和存储在表格单元格之一中的书签。我需要引用该单元格,即我需要确定它的行和列。

I am creating an application in C#. That application needs to create a MS Word document out of the template. That template contains a table and a bookmark stored in one of the table's cells. I need to reference that cell, i.e. I need to determine it's row and column.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悟红尘 2024-08-16 09:06:28

好吧,事实证明这很麻烦。不过,我想出了一个小技巧:
书签与表格单元格没有任何关系。无法从书签确定单元格。然而,可能的是在添加书签的位置设置值,然后迭代表单元格以查找该值。找到该值后,我们可以引用该单元格并在必要时删除该值。因此,(temp) 值充当书签和单元格之间的中介。整洁的!

Well, this turned out to be quite a nuisance. However, I came up with a little trick:
Bookmarks are not related to table cells in any way. There is no way to determine the cell from bookmark. What is possible, however, is to set the value at the bookmarked position and then to iterate through the table cells looking for that value. Once the value is found, we can reference the cell and delete that value if necessary. So, the (temp) value serves as a mediator between the bookmark and the cell. Neat!

梦旅人picnic 2024-08-16 09:06:28

我的第一篇文章(请温柔)。无论如何,这个怎么样:

// at this point objWordApp should be an instance of word with the document open<br>
object objBookmarkName = "mybookmark";<br>
object objGotoBookmark = Word.WdGoToItem.wdGoToBookmark;<br>
Word.Table objTable;<br>
Word.Range objRange;<br>
objTable = objWordApp.ActiveDocument.GoTo(ref objGotoBookmark, ref objMissing, ref objMissing, ref objBookmarkName).Tables.Item(1);<br>
objWordApp.Selection.GoTo(ref objGotoBookmark, ref objMissing, ref objMissing, ref objBookmarkName);

int intRow = objRange.Cells.Item(1).RowIndex;<br>int intCol = objRange.Cells.Item(1).ColumnIndex;

My First post (please be gentle). Anyway, how about this:

// at this point objWordApp should be an instance of word with the document open<br>
object objBookmarkName = "mybookmark";<br>
object objGotoBookmark = Word.WdGoToItem.wdGoToBookmark;<br>
Word.Table objTable;<br>
Word.Range objRange;<br>
objTable = objWordApp.ActiveDocument.GoTo(ref objGotoBookmark, ref objMissing, ref objMissing, ref objBookmarkName).Tables.Item(1);<br>
objWordApp.Selection.GoTo(ref objGotoBookmark, ref objMissing, ref objMissing, ref objBookmarkName);

int intRow = objRange.Cells.Item(1).RowIndex;<br>int intCol = objRange.Cells.Item(1).ColumnIndex;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文