Kannan Rajan
Kannan Rajan

Reputation: 35

Inserting a cross reference link to a table cell

the following code works if the ReferenceItem is hard coded with bookmark name, but inserts the cross reference link the incorrect cell (inserts in cell(2, 1) instead of cell(2, 6). if i use hlpageNo as shown here then it throws an error. Not sure what i'm wrong here. Appreciate any support. Thanks.

Sub setMSPageCrossRef()
  Dim iRng As Word.Range
  Dim cRng As Word.Range
  Dim iDoc As Word.Document
  Dim tableRowCount, iTableRow, rownow As Long
  Dim hlPageNo As String
  Set iDoc = ActiveDocument
  Set iRng = iDoc.Range    
  iRng.Find.Text = "Failure Mode"

  Do While iRng.Find.Execute(Forward:=True, Wrap:=wdFindStop) = True
    Pageno = iRng.Information(wdActiveEndAdjustedPageNumber)
    rowno = iRng.Information(wdEndOfRangeRowNumber)
    Colno = iRng.Information(wdEndOfRangeColumnNumber)
    tableRowCount = iRng.Tables(1).Range.Rows.Count
    Call RemoveBlanksinTable(iRng.Tables(1).Range)

    For iTableRow = rowno To tableRowCount
        Set cRng = iRng.Tables(1).Cell(iTableRow + 1, 6).Range
        hlPageNo = "p" & mid(cRng.Text, 1, Len(cRng.Text) - 1)
        cRng.Collapse wdCollapseStart        
        cRng.InsertCrossReference ReferenceType:="Bookmark", ReferenceKind:= _
        wdPageNumber, ReferenceItem:=hlPageNo, InsertAsHyperlink:=True, _
        IncludePosition:=False, SeparateNumbers:=False, SeparatorString:=" "
    Next    
  Loop    
End Sub

Upvotes: 0

Views: 203

Answers (1)

user11818836
user11818836

Reputation:

The problem is in this line:

hlPageNo = "p" & mid(cRng.Text, 1, Len(cRng.Text) - 1)

which should be changed to this:

hlPageNo = "p" & mid(cRng.Text, 1, Len(cRng.Text) - 2)

When a Word Range such as cRng is set to be the Range of a Table Cell, it always ends with the 2-character sequence ChrW(13) & ChrW(7). Removing 1 character just removes the ChrW(7). So hlPageNo would always end in ChrW(13) which (a) would not match with any of the bookmark names in the document and (b) would not necessarily be obvious when debugging.

Working with Word Table Cell ranges can be confusing because different code samples may show the range length being used without adjustment, or being reduced by 1 or even being reduced by 2, and it isn't immediately obvious why. For example, you can set the Text of a Cell's range without having to deal with those extra characters at all, e.g.

Dim r as Word.Range
Set r = ActiveDocument.Tables(1).Cell(1,1).Range
r.Text = "abc"
Set r = Nothing

causes no problem. But if you want to add a field to the range and you try

Dim r as Word.Range
Set r = ActiveDocument.Tables(1).Cell(1,1).Range
r.Fields.Add r, WdFieldType.wdFieldEmpty, "QUOTE ""abc""", False
Set r = Nothing

you will get RunTime error 4605. To avoid that, you have to ensure that the Range does not include the ChrW(7) EndOfCell character, e.g.

Dim r as Word.Range
Set r = ActiveDocument.Tables(1).Cell(1,1).Range
r.End = r.End - 1
r.Fields.Add r, WdFieldType.wdFieldEmpty, "QUOTE ""abc""", False
Set r = Nothing

and in that case it makes no difference if you use r.End = r.End - 2 either.

Finally, there is another unrelated problem in the posted code which is when iTableRow reaches tableRowCount,

iTableRow + 1

will be larger than the number of rows in the table.

Upvotes: 1

Related Questions