Exemple #1
0
        public void SelectionToLink(IRibbonControl control)
        {
            Range range = Globals.ThisAddIn.Application.Selection.Range;

            range.MoveStartWhile("[({             ​  \r\a\n\t", WdConstants.wdForward);
            range.MoveEndWhile("             ​  \r\a\n\t})]", WdConstants.wdBackward);
            range.Select();
            string findRef = range.Text.Trim();
            bool   found   = false;

            if (findRef.Length != 0)
            {
                object[] refTypes = { WdReferenceType.wdRefTypeHeading, WdCaptionLabelID.wdCaptionTable, WdCaptionLabelID.wdCaptionFigure, WdCaptionLabelID.wdCaptionEquation, WdReferenceType.wdRefTypeNumberedItem };
                foreach (var refType in refTypes)
                {
                    var references = (Array)(object)Globals.ThisAddIn.Application.ActiveDocument.GetCrossReferenceItems(refType);
                    for (int idx = references.GetLowerBound(0); idx < references.GetUpperBound(0); idx++)
                    {
                        if (references.GetValue(idx).ToString().Contains(findRef))
                        {
                            found = true;
                            WdReferenceKind referenceKind = WdReferenceKind.wdNumberNoContext;
                            if ((WdReferenceType)refType != WdReferenceType.wdRefTypeHeading)
                            {
                                referenceKind = WdReferenceKind.wdOnlyLabelAndNumber;
                            }
                            Forms.DialogResult response = Forms.MessageBox.Show("\"Yes\" for just the item number, \"No\" for just the heading/title, or \"Cancel\" to cancel.",
                                                                                "Link Type", Forms.MessageBoxButtons.YesNoCancel);
                            if (response == Forms.DialogResult.No)
                            {
                                if (refType == (object)WdReferenceType.wdRefTypeHeading)
                                {
                                    referenceKind = WdReferenceKind.wdContentText;
                                }
                                else
                                {
                                    referenceKind = WdReferenceKind.wdEntireCaption;
                                }
                            }
                            Globals.ThisAddIn.Application.Selection.InsertCrossReference(refType, referenceKind, idx, true, false);
                        }
                        if (found)
                        {
                            return;
                        }
                    }
                }
            }
            Forms.MessageBox.Show("Reference text not found.", "Not Found", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
        }
 /// <summary>
 /// Set the display option to be used for this particular cross reference.
 /// </summary>
 /// <param name="p"></param>
 internal void setCurrentDisplayOption(int p)
 {
     currentDisplayOption = listOfOptions[p];
 }