3

I think I have found a bug with the InsertCrossReference. If I use from code for the last list item in the document and there being nothing after that list item, it fails with error "Run-time error '4198' Command failed". If you do it manually, it all works. So you are thinking, I have written the code incorrectly, but this isn't the case. To make sure, i recorded a macro of inserting the cross reference and then ran the macro it recorded and the same error happens.

I have googled this problem and seen a couple of people raise it but a)they haven't pointed out that it only fails for the last list item and there being nothing in the document after that list item b)they haven't had any work-around replies.

I am using Word 2010 but I have also tried it on Word 2013 and the same thing happens.

If you want an example, if I set up the following:

This is my xref

  1. Hello
  2. Bye

where 1 and 2 are standard numbered lists and I have nothing in the document after "Bye" and then I run:

ActiveDocument.Range(16, 16).InsertCrossReference ReferenceType:="Numbered item", _
ReferenceKind:=wdNumberFullContext, ReferenceItem:="2", InsertAsHyperlink _
:=True, IncludePosition:=False, SeparateNumbers:=False, SeparatorString:=" "

Note having nothing after "Bye" is key. If you start a new line, with or without a list type, the code above will work

If anyone has any work-around for this, I'd appreciate it

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
crookie
  • 240
  • 3
  • 14
  • I encountered the same issue with Word 2013, and the only workaround I could find was to add a paragraph at the end of the document. The line from @Toby worked for me - do `ActiveDocument.Paragraphs.Add Range:=ActiveDocument.Range(ActiveDocument.Range.End -1, ActiveDocument.Range.End -1)` before adding the xref if the target paragraph is the last one in the document. – cxw Dec 28 '17 at 15:05

1 Answers1

1

You cannot insert anything AFTER the last paragraph mark in a document. I've had this issue in the past. My work around was to pragmatically add a paragraph mark in.

Something like the following:

Dim CRRange as Word.Range 'Make a range object to store where the cross reference will go.
set CRRange = ActiveDocument.Range(16,16)

if CRRange.end >= ActiveDocument.Range.End then 'Check to see if we reached the end of the document
    ActiveDocument.Paragraphs.Add Range:=ActiveDocument.Range(ActiveDocument.Range.End -1, ActiveDocument.Range.End -1)
End If

CRRange.InsertCrossReference ReferenceType:="Numbered item", _
    ReferenceKind:=wdNumberFullContext, ReferenceItem:="2", InsertAsHyperlink _
    :=True, IncludePosition:=False, SeparateNumbers:=False, SeparatorString:=" "
gNerb
  • 867
  • 2
  • 12
  • 28
  • Hi Toby. Thanks for your reply, but the position I am adding the InsertCrossReference is not after the last paragraph mark, I am adding it after "This is my xref " and so when i try your code, it does not go in the If statement. It cannot be anything to do with the place I am adding it in, as if I add a reference to the second list member (ie ReferenceItem:="1") with everything else being the same, it all works – crookie Mar 05 '15 at 14:56