PDA

View Full Version : 4605 error inserting page cross reference field



Digilee
04-21-2011, 11:33 AM
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


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

Frosty
04-21-2011, 12:21 PM
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)

Digilee
04-21-2011, 01:03 PM
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:


Set rngWhere = nRow.Cells(2).Range
rngWhere.Collapse wdCollapseStart


is not equivalent to:


nRow.Cells(2).Range.Collapse wdCollapseStart


(I tried it, it didn't work)

Thanks,
Lee

Frosty
04-21-2011, 01:07 PM
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.

Digilee
04-21-2011, 05:29 PM
As I said, I'm still learning. Thanks again.

Lee

Frosty
04-21-2011, 05:37 PM
I hope my comment didn't come off as snide. It wasn't intended that way. Glad to help!

Digilee
04-21-2011, 05:39 PM
Not at all - I was simply giving myself an excuse for not understanding. It's clear when you put it the way you did.