#1
|
|||
|
|||
Inserting Cross References into Cell
I'm trying to insert a cross reference into a cell but my code doesn't seem to work. Everything executes properly but when the macro finishes, nothing appears in the cell in which it was inserted. Updating all fields doesn't do anything either. Here's my code: Code:
With ActiveDocument.Range.Tables(4) ... With .Cell(2, 2).Range ... .InsertCrossReference ReferenceType:=wdRefTypeBookmark, ReferenceKind:= wdContentText, _ ReferenceItem:="DATE", InsertAsHyperlink:=True End With End With |
#2
|
||||
|
||||
The code sequence you have shown works in Word 2013, with the proviso that you have a bookmarked text called "Date" in the document. If there is a problem with the macro it may relate to the missing parts of the macro.
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#3
|
|||
|
|||
Okay so in the same document, I created a new macro with the code below. It successfully creates the bookmark but puts it in cell (2,1) instead of (2,2). Changing the code to put it in (2,1) puts it in the same spot. I don't understand what's happening here.
Furthermore, replacing ".InsertCrossReference" with ".Text = "test"" successfully sets the text of the specified cell to "test". It seems something is wrong with my bookmarks (they reference an XML linked content control if that makes a difference.) The thing that confuses me is that when I place the cursor in the cell and use "Selection.InsertCrossReference", it inserts the bookmark correctly, no problems. Code:
Sub CrossRef() ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=2, NumColumns:= _ 2, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _ wdAutoFitFixed With ActiveDocument.Range.Tables(4) With .Cell(2, 2).Range .InsertCrossReference ReferenceType:=wdRefTypeBookmark, ReferenceKind:=wdContentText, ReferenceItem:="DATE", InsertAsHyperlink:=True End With End With End Sub |
#4
|
||||
|
||||
Inserting something into a range can be problematic when it already has content.
Try defining a range and then collapsing that to a specific location Code:
Dim aRng As Range Set aRng = ActiveDocument.Range.Tables(1).Cell(2, 2).Range aRng.Collapse wdCollapseStart aRng.InsertCrossReference ReferenceType:=wdRefTypeBookmark, ReferenceKind:=wdContentText, _ ReferenceItem:="DATE", InsertAsHyperlink:=True
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#5
|
|||
|
|||
Yes thank you. Collapsing the range beforehand does the trick.
For anyone else who may run into this problem, if you use "wdCollapseEnd" to insert a cross reference after preexisting text in a cell, it will put it in the next cell. To avoid this, use: Code:
.MoveEnd Unit:=wdCharacter, Count:=-1 .Collapse wdCollapseEnd |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Convert manual cross references in footnotes to other footnotes to automatic cross references | ghumdinger | Word VBA | 7 | 11-20-2014 11:47 PM |
Bookmarks & cross-references | Suchoklates | Word | 1 | 09-19-2013 02:32 AM |
Cross-References | acolussi | Word | 9 | 05-16-2013 02:11 AM |
MS Word cross-references? | entity022 | Word | 3 | 06-03-2010 06:37 AM |
Cross References | egcharles | Office | 0 | 04-19-2009 06:20 AM |