#1
|
|||
|
|||
REf Fields show Bookmark whole cell when Bookmark is created by code.
Hi - This is my first post in this forum for a few years. My other forum couldn't help me.
I created a macro which takes user inputted data from a Table Column and creates a Bookmark named in the adjacent column. The macro works just fine after many attempts and being told that it couldn't be done. However, when the Ref" fields in the body of the document are updated, they bring over the whole cell (in fact they create a Table with 1 row) at the insertion point. This of course messes up the document layout. If I go into each bookmark in the Table and Select inside the square brackets and insert the same bookmark, then the "Ref" field is fine. This of course defeats the object of having a macro. You can see that the code references the .text property is used which should prevent this from happening. The sample of the document is attached and the code is as follows: --------------------------------------------------------------------------------- Sub Mcr_Set_Bookmarks() ' ' Mcr_Set_Bookmarks Macro ' This Macro Takes the User Inputted Data in the "Value" Column of the Data Sheet Table ' and allocates the Bookmark Name from the "Bookmark Name" Column Dim BKMkName As Variant Dim BkmkRange As Variant Dim BkmkNameLength As Integer Dim BkmkRangeLength As Integer Dim rV As Integer Dim Endrow As Integer Dim StartRow As Integer '--------------------------------------------------------------------- 'On Error Resume Next 'StartRow = Val(InputBox("Enter First Row Number Which Contains Data", "First Row")) StartRow = 2 Endrow = ActiveDocument.Tables(1).Rows.Count rV = StartRow '------------------------------------------------------------------ Loop Through all Values in Table and allocate Bookmarks For rV = StartRow To Endrow BkmkNameLength = Len(ActiveDocument.Tables(1).Cell(rV, 4).Range) - 2 BkmkRangeLength = Len(ActiveDocument.Tables(1).Cell(rV, 3).Range) - 2 BKMkName = Left(ActiveDocument.Tables(1).Cell(rV, 4).Range, BkmkNameLength) Debug.Print BKMkName BkmkRange = Left(ActiveDocument.Tables(1).Cell(rV, 3).Range, BkmkRangeLength) ActiveDocument.Tables(1).Cell(rV, 3).Select BkmkRange = Left(Selection.Range.Text, BkmkRangeLength) 'Debug.Print BkmkRange() ActiveDocument.Bookmarks.Add Range:=ActiveDocument.Tables(1).Cell(rV, 3), Name:=BKMkName Next rV End Sub ----------------------------------------------------------------------------------- The attached document contains a snip which shows what I mean with the whole cell appearing at the insertion point. Any help would be greatly appreciated as I am at my wits end. Thanks, Peter |
#2
|
||||
|
||||
You are overcomplicating this. To bookmark the values in Column 3 with the names from Column 4: ....(see also attached)
Code:
Sub Mcr_Set_Bookmarks() Dim BKMkName As String Dim BkmkRange As Range Dim oCell As Range Dim rV As Integer For rV = 2 To ActiveDocument.Tables(1).Rows.Count Set oCell = ActiveDocument.Tables(1).Cell(rV, 4).Range oCell.End = oCell.End - 1 'Omit cell end character BKMkName = oCell.Text Set BkmkRange = ActiveDocument.Tables(1).Cell(rV, 3).Range BkmkRange.End = BkmkRange.End - 1 'Omit cell end character 'Add the bookmark to the cell content range ActiveDocument.Bookmarks.Add Range:=BkmkRange, Name:=BKMkName Next rV Set oCell = Nothing Set BkmkRange = Nothing End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#3
|
|||
|
|||
Wow - I can't thank you enough Graham.
I've been struggling with this for a few weeks now, just wish I'd come to this forum first. Best Regards, Peter |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
How do I code bookmark globally for different template? | Yong Xiang | Word VBA | 17 | 02-06-2019 01:09 PM |
Field Code: Show a value only if bookmark is not empty | Cosmo | Word | 7 | 08-24-2018 01:46 PM |
Bookmark will not show/hide based on CC Checkbox | lord_kaiser | Word VBA | 1 | 04-17-2018 01:19 AM |
Bookmark - add two merge fields | dfin | Word VBA | 5 | 09-06-2016 10:08 PM |
Find Bookmark, move to bookmark, execute code, repeat | raymm3852 | Word VBA | 10 | 04-15-2016 06:21 PM |