Reputation: 1
I'm trying to bookmark the contents of a Word table cell (the contents will be numbers) so that I can cross-reference the bookmark in text elsewhere in the document - the goal being that if the numbers in the cells change, by running an "update all fields" macro (which I already have) I can update all of the in-text references to these numbers without having to manually scour the entire document. I've been through the interwebs and found something similar (my adapted version is shown below), however the issue with this method is that when I try to cross-reference the bookmark, it keeps the cell formatting - so there will be text and then suddenly a random cell and then more text. The good thing about this method is that the number does actually update as it's supposed to, I just can't get rid of the cell formatting.
Sub BookmarkCurrentCell()
If Selection.Information(wdWithInTable) Then
selectedTable = ActiveDocument.Range(0, Selection.Tables(1).Range.End).Tables.Count
selectedColumn = Selection.Information(wdStartOfRangeColumnNumber)
selectedRow = Selection.Information(wdStartOfRangeRowNumber)
End If
ActiveDocument.Bookmarks.Add Name:="Bookmark_" & selectedTable & "_" & selectedRow & "_" & selectedColumn, Range:=ActiveDocument.Tables(selectedTable).Cell(selectedRow, selectedColumn).Range
End Sub
Thanks in advance!
Upvotes: 0
Views: 1719
Reputation: 3294
Try this:
Sub BookmarkCurrentCell()
Dim rng As Range
If Selection.Information(wdWithInTable) Then
selectedTable = ActiveDocument.Range(0, Selection.Tables(1).Range.End).Tables.Count
selectedColumn = Selection.Information(wdStartOfRangeColumnNumber)
selectedRow = Selection.Information(wdStartOfRangeRowNumber)
End If
Set rng = ActiveDocument.Tables(selectedTable).Cell(selectedRow, selectedColumn).Range
rng.End = rng.End - 1
ActiveDocument.Bookmarks.Add Name:="Bookmark_" & selectedTable & "_" & selectedRow & "_" & selectedColumn, Range:=rng
End Sub
I think the problem is that you are applying the bookmark to the entire cell, the modified code sets a range = to the cell range, then moves the end of the range back 1 character so that it only includes the cell contents (and not the actual cell).
Upvotes: 1