none
Selection.InsertCrossReference RRS feed

  • Question

  • Hi,

    I want to add cross reference to a figure caption I have added before using c# and word interop.

    I am using the following code:

    object oRefType = WdCaptionLabelID.wdCaptionFigure;

    Array items = (Array)myDocument.GetCrossReferenceItems(ref oRefType);
    // take the last one, because it was just added before
    object oRefItem = items.GetValue(items.Length);

    myApplication.Selection.InsertCrossReference(ref oRefType, WdReferenceKind.wdOnlyLabelAndNumber, ref oRefItem,
                                                                       ref oMissing, ref oMissing, ref oMissing, ref oMissing);

    The oRefItem contains the correct caption label plus text, but InsertCrossReference always throws and COMException "Command Failed", "ErrorCode -2146824090". I have tried already different ReferenceKinds but none of them seems to work.

    Looking forward for your help!

    Wednesday, February 9, 2011 9:58 AM

Answers

  • Hi Cindy,

    inserting the caption worked, but adding a cross-reference to this failed.

    Recording a macro gave me:

    Selection.InsertCrossReference ReferenceType:="Figure", ReferenceKind:= _
            wdOnlyLabelAndNumber, ReferenceItem:="1", InsertAsHyperlink:=True, _
            IncludePosition:=False, SeparateNumbers:=False, SeparatorString:=" "

    So I changed my code to:

    object oRefType = WdCaptionLabelID.wdCaptionFigure;

    Array items = (Array)myDocument.GetCrossReferenceItems(ref oRefType);
    // take the last one, because it was just added before
    object oRefItem = items.Length;

    myApplication.Selection.InsertCrossReference(ref oRefType, WdReferenceKind.wdOnlyLabelAndNumber, ref oRefItem,
                                                                       ref oTrue, ref oMissing, ref oMissing, ref oMissing);

    So the ReferenceItem must be an index, then it works. Thanks a lot for your hints!

     

     

    Friday, February 11, 2011 10:14 AM

All replies

  • Version of Word?

    Are you able to insert the caption as an end-user, in the UI? (For example, is the oRefType available in the list)

    What do you get if you record a macro?

    Sometimes, Word loses the caption types. This could happen, for example, if Normal.dot(m) gets damaged. So that's where I'd start the investigation.


    Cindy Meister, VSTO/Word MVP
    Thursday, February 10, 2011 5:54 PM
    Moderator
  • Hi Cindy,

    inserting the caption worked, but adding a cross-reference to this failed.

    Recording a macro gave me:

    Selection.InsertCrossReference ReferenceType:="Figure", ReferenceKind:= _
            wdOnlyLabelAndNumber, ReferenceItem:="1", InsertAsHyperlink:=True, _
            IncludePosition:=False, SeparateNumbers:=False, SeparatorString:=" "

    So I changed my code to:

    object oRefType = WdCaptionLabelID.wdCaptionFigure;

    Array items = (Array)myDocument.GetCrossReferenceItems(ref oRefType);
    // take the last one, because it was just added before
    object oRefItem = items.Length;

    myApplication.Selection.InsertCrossReference(ref oRefType, WdReferenceKind.wdOnlyLabelAndNumber, ref oRefItem,
                                                                       ref oTrue, ref oMissing, ref oMissing, ref oMissing);

    So the ReferenceItem must be an index, then it works. Thanks a lot for your hints!

     

     

    Friday, February 11, 2011 10:14 AM