none
Selection.InsertCrossReference throws exception when referencing the last paragraph in document RRS feed

  • Question

  • Hi there

    When I am inserting a cross reference that references the very last paragraph of the document, the method throws an exception "Command failed" with the error code -2146824090.

    As soon as there is at least one more paragraph mark (not necessarily a numbered item), InsertCrossReference works fine.

    This is the crucial line of code:

    selection.InsertCrossReference(

    WdReferenceType.wdRefTypeNumberedItem, WdReferenceKind.wdNumberRelativeContext, itemNumber, true, includePosition, false, "");

    When I am referencing the same item manually, by means of the Word GUI, it works just fine.

    Am I doing something wrong? Can this be helped? If the answer is no, does somebody have workaround ideas?

    Thanks, kind regards

    Nora

    Tuesday, November 4, 2014 8:30 AM

Answers

  • Hi Nora,

    >>This is the crucial line of code:

    selection.InsertCrossReference(

    WdReferenceType.wdRefTypeNumberedItem, WdReferenceKind.wdNumberRelativeContext, itemNumber, true, includePosition, false, "");

    When I am referencing the same item manually, by means of the Word GUI, it works just fine.<<

    I am able to reprdoce the issue successfully. And as you metioned this issues didn't occur when there is one more paragraph behind the section postion.

    As a workaround, we can insert a empty paragraph first then we can insert the crossreference. At last we can remove the paragraphy we inserted. Here is a samlpe with VBA for your reference:

    Sub InsertCrossreferenceAtEndofDocument()
    
    'insert an pargraph befor inserting crossreference
    Application.Selection.TypeParagraph
    
    'insert the crossreference
        Selection.InsertCrossReference ReferenceType:="Numbered item", _
            ReferenceKind:=wdNumberRelativeContext, ReferenceItem:="3", _
            InsertAsHyperlink:=True, Include        SeparatorString:=" "
         
    'remove the paragraph
    Application.Selection.Move WdUnits.wdParagraph, -1
    Application.Selection.EndOf Unit:=wdParagraph, Extend:=wdMove
    Application.Selection.MoveLeft Unit:=wdCharacter, Count:=1
    Application.Selection.Delete Unit:=wdCharacter, Count:=1
         
            
    End Sub
    ScreeShot:

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by nora.sailer Wednesday, November 5, 2014 8:59 AM
    Wednesday, November 5, 2014 7:37 AM
    Moderator

All replies

  • Hi Nora,

    >>This is the crucial line of code:

    selection.InsertCrossReference(

    WdReferenceType.wdRefTypeNumberedItem, WdReferenceKind.wdNumberRelativeContext, itemNumber, true, includePosition, false, "");

    When I am referencing the same item manually, by means of the Word GUI, it works just fine.<<

    I am able to reprdoce the issue successfully. And as you metioned this issues didn't occur when there is one more paragraph behind the section postion.

    As a workaround, we can insert a empty paragraph first then we can insert the crossreference. At last we can remove the paragraphy we inserted. Here is a samlpe with VBA for your reference:

    Sub InsertCrossreferenceAtEndofDocument()
    
    'insert an pargraph befor inserting crossreference
    Application.Selection.TypeParagraph
    
    'insert the crossreference
        Selection.InsertCrossReference ReferenceType:="Numbered item", _
            ReferenceKind:=wdNumberRelativeContext, ReferenceItem:="3", _
            InsertAsHyperlink:=True, Include        SeparatorString:=" "
         
    'remove the paragraph
    Application.Selection.Move WdUnits.wdParagraph, -1
    Application.Selection.EndOf Unit:=wdParagraph, Extend:=wdMove
    Application.Selection.MoveLeft Unit:=wdCharacter, Count:=1
    Application.Selection.Delete Unit:=wdCharacter, Count:=1
         
            
    End Sub
    ScreeShot:

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by nora.sailer Wednesday, November 5, 2014 8:59 AM
    Wednesday, November 5, 2014 7:37 AM
    Moderator
  • Hi Fei

    The pararagraph mark that is missing is at the end of the document, not at the Selection. But yes, of course, I could follow the same principle: Get the range end of the document, insert a paragraph, insert the reference, delete the paragraph again.

    So I'll mark this workaraound as answer.

    Thanks, have a nice day

    Nora

    Here's the code:

           

    try


    {

              selection.InsertCrossReference(

    WdReferenceType.wdRefTypeNumberedItem, WdReferenceKind.wdNumberRelativeContext, itemNumber, true, includePosition, false, "");

            }

           

    catch


            {

             

    Rangerange = activeDocument.Content;

             

    intrangeEnd = range.End;

              range.InsertAfter(

    "\n");

             

    try


              {

                selection.InsertCrossReference(

    WdReferenceType.wdRefTypeNumberedItem, WdReferenceKind.wdNumberRelativeContext, itemNumber, true, includePosition, false, "");

              }

             

    catch

              {

               

    // Explicitly do nothing


              }

             

    if(range.End > rangeEnd)

              {

                range.Start = range.End - 1;

                range.Delete(

    Type.Missing, Type.Missing);

              }

            }


    • Edited by nora.sailer Wednesday, November 5, 2014 10:42 AM Added code example
    Wednesday, November 5, 2014 8:59 AM