Consulting

Results 1 to 7 of 7

Thread: 4605 error inserting page cross reference field

  1. #1
    VBAX Regular
    Joined
    Apr 2011
    Posts
    20
    Location

    4605 error inserting page cross reference field

    Hi,
    I am a firmware engineer trying to help the Tech Pubs guys out by automating register table creation for our Programmer's Reference documents. For the past three weeks I have been teaching myself VBA, which obviously is not enough because there are a lot of gaps in my knowledge.
    All is going well until I try to add a page cross-reference field to my summary table. I can (and do) add a separate hyperlink in the next column, but I get a 4605 'Command not available' error when attempting to insert the field.

    This is the code for building a single row in the summary table. I have attached an example file that shows a completed summary table (except for the page reference field), along with the tables that are used to create the summary table.

    Any help would be greatly appreciated...

    Regards,
    Lee

    [VBA]
    Function SummaryTableEntry(bmIndex As Long, nRow As row) As Boolean
    Dim offsetText As String
    Dim tableOffsetText As String
    Dim xTable As Table
    Dim colonPos As Long
    Dim i As Long

    offsetText = Right(Trim(ActiveDocument.Bookmarks(bmIndex).Name), 4)

    ' Insert offset from bookmark name into offset column
    nRow.Cells(1).Range.Text = offsetText

    ' Get the actual register name from the bookmarked table
    Set xTable = ActiveDocument.Bookmarks(bmIndex).Range.Tables(1)
    For i = 2 To xTable.Rows.count
    ' get the offset part of the Address/Offset field for this row
    tableOffsetText = Main.fCellText(xTable.Rows(i).Cells(1).Range.Text)
    colonPos = InStr(1, tableOffsetText, ":")
    If colonPos <> 0 Then
    tableOffsetText = Right(tableOffsetText, 4)

    ' If the bookmark offset value is the same as this row
    ' of the table offset, get the register name
    If (InStr(1, offsetText, tableOffsetText) <> 0) Then

    ' Add register name to thrird column
    nRow.Cells(3).Range.Text = Main.fCellText(xTable.Rows(i).Cells(2).Range.Text)

    ' Add hyperlink to table in third column
    ActiveDocument.Hyperlinks.Add _
    Anchor:=nRow.Cells(3).Range, _
    Address:=ActiveDocument.FullName, _
    SubAddress:=ActiveDocument.Bookmarks(bmIndex).Name, _
    ScreenTip:=""

    ' Add Page reference hyperlink in second column
    ActiveDocument.Range.Fields.Add _
    Range:=nRow.Cells(2).Range, _
    Type:=wdFieldPage, _
    Text:="", _
    PreserveFormatting:=True

    Exit For
    End If
    Else
    Exit For
    End If
    Next

    SummaryTableEntry = True
    End Function

    [/VBA]

  2. #2
    VBAX Master
    Joined
    Feb 2011
    Posts
    1,480
    Location
    The help on Fields.Add method indicates it replaces the range if the passed range isn't collapsed. You can't replace the entirety of the nRow.Cells(2).Range with a field (since that includes the "end of cell" marker, and would create a black hole in your table, causing your computer monitor to disappear).

    That's why microsoft doesn't allow the command

    Just dim a rngWhere at the top of your routine, and then do...

    set rngWhere = nRow.Cells(2).Range
    and then either...
    rngWhere.Collapse wdCollapseStart (or End)
    or
    rngWhere.MoveEnd wdCharacter, -1 '(for the end of cell character)

    (depending on whether you want to replace the existing text, etc)

  3. #3
    VBAX Regular
    Joined
    Apr 2011
    Posts
    20
    Location
    Wow, quick service - thanks for your repsonse

    I am still learning ranges, selections, etc. What you suggested worked fine, thanks, but I want to understand why this:

    [VBA]
    Set rngWhere = nRow.Cells(2).Range
    rngWhere.Collapse wdCollapseStart
    [/VBA]

    is not equivalent to:

    [VBA]
    nRow.Cells(2).Range.Collapse wdCollapseStart
    [/VBA]

    (I tried it, it didn't work)

    Thanks,
    Lee

  4. #4
    VBAX Master
    Joined
    Feb 2011
    Posts
    1,480
    Location
    Some days are faster than others. Well, the reason one works is because when you have your range variable-- you can defined it as anything-- this is my "mountain range", or my "forest range" or my "ocean range"... but you can't point at an actual mountain range and say "become a forest"

    So the range of a specific cell is just that-- the range of that cell. You can't change that definition.

    But you can point your own range "variable" (in the true definition of something which varies) at a specific cell's range, and then adjust your variable however you like.

  5. #5
    VBAX Regular
    Joined
    Apr 2011
    Posts
    20
    Location
    As I said, I'm still learning. Thanks again.

    Lee

  6. #6
    VBAX Master
    Joined
    Feb 2011
    Posts
    1,480
    Location
    I hope my comment didn't come off as snide. It wasn't intended that way. Glad to help!

  7. #7
    VBAX Regular
    Joined
    Apr 2011
    Posts
    20
    Location
    Not at all - I was simply giving myself an excuse for not understanding. It's clear when you put it the way you did.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •