none
Removing cross-references from word doc RRS feed

  • Question

  • Hi,

    I have a c# program that searches for specific strings in a word document and copies them to the UI. the problem is that when the string constitute a cross-reference, it is not found. I was trying to find how to remove all cross reference from the word doc, and leave only the text which they include. for example: "For more information see paragraph 3" (while 3 is cross reference). 

    This is the code I've tried to use in order delete the cross reference:

            public void Removehyperlink(Document oDoc)
            {
                Microsoft.Office.Interop.Word.Hyperlinks links = oDoc.Hyperlinks;
                for (int i = 0; i < links.Count; i++)
                {
                    object index = (object)i;
                    links[index].Delete(); //Here i am getting Com exception, see below
                }
                //Microsoft.Office.Interop.Word.


                Microsoft.Office.Interop.Word.InlineShapes inlineShapes = oDoc.InlineShapes;

                foreach (Microsoft.Office.Interop.Word.InlineShape iShape in inlineShapes)
                {

                    if (iShape.Type == Microsoft.Office.Interop.Word.WdInlineShapeType.wdInlineShapeEmbeddedOLEObject ||

                        iShape.Type == Microsoft.Office.Interop.Word.WdInlineShapeType.wdInlineShapeLinkedOLEObject)
                    {

                        iShape.Delete();

                    }

                }
            }



    this is where the search fails to find the number:

                    //Now Define & Search the rest of the document For the Number
                    DynamicContentRng = oDoc.Content;
                    DynamicContentRng.Start = Convert.ToInt32(startNumber);
                    DynamicContentRng.Find.Text = ".";
                    DynamicContentRng.Find.Forward = true;

    Tuesday, January 15, 2019 7:22 PM

All replies

  •         public void Removehyperlink(Document oDoc)
            {
                Microsoft.Office.Interop.Word.Hyperlinks links = oDoc.Hyperlinks;


                while(oDoc.Fields.Count>=1)
                {
                    oDoc.Fields[oDoc.Fields.Count].Select();
                    string fieldText = oDoc.Fields[oDoc.Fields.Count].Result.Text;

                    if (String.IsNullOrWhiteSpace(fieldText)) //this is for auto numbering
                        oDoc.Fields[oDoc.Fields.Count].Unlink();
                    else //this is for cross-references
                    {
                        //Microsoft.Office.Interop.Word.Range ranger = oDoc.Fields[oDoc.Fields.Count].Result;
                        //oDoc.Fields[oDoc.Fields.Count].Delete();
                        //string resultString = System.Text.RegularExpressions.Regex.Match(fieldText, @"\d+").Value;
                        //ranger.Text = resultString;
                    }
                }

                //object path = ProjectFunctinos.CligalWorkingFolder + "\\testing.doc";
                //MIPS.SaveWord(oDoc, path);
            }
    Tuesday, January 22, 2019 3:25 PM