#1
|
|||
|
|||
Add a bookmark at a certain line/column
Hi all - I have a bunch of documents that I need to add the same three bookmarks to ad the exact same place (lets say, lines 3, 4 and 5 at column 20). Is there a simple way to add the bookmark at the specified locations? I've tried playing with the range property but I cannot seem to get it to work. Thanks for your help. |
#2
|
||||
|
||||
You need to define the locations based on how Word refers to its own structure
Word doesn't really work in terms of 'lines' since that is fluid based on a huge number of factors. 'Paragraphs' is the usual reference point. Columns could be either talking about a page layout column or a table column. Either way, 20 is an extraordinarily large number for a real world situation. Page layout columns is another ill-defined term like 'lines' since it is also fluid. However table columns are easy to work with but you need to specify which table you are talking about. If you want code that will work, you should post a sample document and identify what are the triggers that uniquely identify your chosen locations eg always the 3rd table, 3rd row, 20th column, 1st paragraph.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#3
|
|||
|
|||
Thanks for your in depth response! See attached screenshot - these are pre-built Word documents, I want to put bookmarks next to "Effective Date", "Policy Number" and "Issued to" - which are always on line 7, 8 and 9 in all of these Word documents. I have some VBA code in Excel that looks for these bookmarks and inserts data from Excel into Word based on the bookmark names. (I've inserted the bookmarks manually into a Word document and the code has worked, I just thought I could save some time by creating these bookmarks in the 100+ Word documents programmatically rather than creating them in each individual Word file). Thank you!
Quote:
|
#4
|
||||
|
||||
Firstly, I will say I'm not a fan of using bookmarks for a job like this because populating the text inside the bookmark is problematic. Instead, I would recommend you add Content Controls where you want to populate this content.
Secondly, I wouldn't bother with pre-setting the locations in your documents since that seems like an additional step which could simply be part of the Excel macro. This code is how I would do it for a single document and it could be adapted to sit in your Excel code. Code:
Sub PopItIn() FillCC sCCtitle:="Effective Date", sValue:="my date", sFind:="Effective Date: " FillCC sCCtitle:="Policy Number", sValue:="123456", sFind:="Policy Number: " FillCC sCCtitle:="Issued To", sValue:="Joe Dirt", sFind:="Issued To: " End Sub Function FillCC(sCCtitle As String, sValue As String, sFind As String, Optional aDoc As Document) As ContentControl Dim aCC As ContentControl, aRng As Range If aDoc Is Nothing Then Set aDoc = ActiveDocument If aDoc.SelectContentControlsByTitle(sCCtitle).Count = 0 Then Set aRng = aDoc.Range With aRng.Find .ClearFormatting .Text = sFind If .Execute Then aRng.Collapse Direction:=wdCollapseEnd Set aCC = aDoc.ContentControls.Add(Type:=wdContentControlText, Range:=aRng) aCC.Title = sCCtitle Else Debug.Print "Neither Content Control nor Anchor text: " & sFind Exit Function End If End With Else Set aCC = aDoc.SelectContentControlsByTitle(sCCtitle)(1) End If aCC.Range.Text = sValue End Function
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#5
|
|||
|
|||
Thank you so much!
|
Tags |
bookmarks |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
REf Fields show Bookmark whole cell when Bookmark is created by code. | pmcpowell | Word VBA | 2 | 11-16-2019 07:05 PM |
Find Bookmark, move to bookmark, execute code, repeat | raymm3852 | Word VBA | 10 | 04-15-2016 06:21 PM |
Table column line moves | joannechee2 | Word Tables | 7 | 03-17-2016 12:21 AM |
Delete Line of Bookmark if the Text is Empty | ljg713 | Word VBA | 1 | 12-21-2015 04:27 PM |
Multi Line Bookmark | streng | Word | 3 | 07-13-2009 07:54 AM |