Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-15-2019, 06:26 PM
pmcpowell pmcpowell is offline REf Fields show Bookmark whole cell when Bookmark is created by code. Windows 8 REf Fields show Bookmark whole cell when Bookmark is created by code. Office 2013
Novice
REf Fields show Bookmark whole cell when Bookmark is created by code.
 
Join Date: Feb 2016
Posts: 6
pmcpowell is on a distinguished road
Default 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
Attached Files
File Type: zip Automated_Sttements_Working_v_4.0.zip (77.2 KB, 6 views)
Reply With Quote
  #2  
Old 11-15-2019, 10:20 PM
gmayor's Avatar
gmayor gmayor is offline REf Fields show Bookmark whole cell when Bookmark is created by code. Windows 10 REf Fields show Bookmark whole cell when Bookmark is created by code. Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Attached Files
File Type: docm Automated_Sttements_Working_v_4.1.docm (83.3 KB, 5 views)
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 11-16-2019, 07:05 PM
pmcpowell pmcpowell is offline REf Fields show Bookmark whole cell when Bookmark is created by code. Windows 8 REf Fields show Bookmark whole cell when Bookmark is created by code. Office 2013
Novice
REf Fields show Bookmark whole cell when Bookmark is created by code.
 
Join Date: Feb 2016
Posts: 6
pmcpowell is on a distinguished road
Default

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
Reply With Quote
Reply

Thread Tools
Display Modes


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
REf Fields show Bookmark whole cell when Bookmark is created by code. 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
REf Fields show Bookmark whole cell when Bookmark is created by code. 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

Other Forums: Access Forums

All times are GMT -7. The time now is 03:43 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft