开发者

How can I determine the table cell the bookmark belongs to using Office.Interop

开发者 https://www.devze.com 2022-12-08 23:25 出处:网络
I am creating an application in C#. That application needs to create a MS Word document out of the templ开发者_如何学Cate. That template contains a table and a bookmark stored in one of the table\'s c

I am creating an application in C#. That application needs to create a MS Word document out of the templ开发者_如何学Cate. 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.


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!


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

精彩评论

暂无评论...
验证码 换一张
取 消