Question

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.

Was it helpful?

Solution

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!

OTHER TIPS

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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top