none
Sometimes Document.Descendants<DocumentFormat.OpenXml.Wordprocessing.Hyperlink>() won't return every hyperlink in a word document RRS feed

  • Question

  • I have following code ,  try to query every custom hyperlink, replace its uri or remove the link completely, depends on the parameter.

    I found out for some word document, this method will skip some links, b/c Document.Descendants<DocumentFormat.OpenXml.Wordprocessing.Hyperlink>() didn't return a whole list, for the same document, if I modify it a little bit, insert some text before each link, then following code will work fine.  Thanks. Chang

            public static bool FixWordLinks(string docFile, string version, bool isDeleteLink)
            {
                try
                {
                    int count = 0;
                    using (WordprocessingDocument package = WordprocessingDocument.Open(docFile, true))
                    {
                        Document mainDocument = package.MainDocumentPart.Document;

                        foreach (DocumentFormat.OpenXml.Wordprocessing.Hyperlink hyperlink in mainDocument.Descendants<DocumentFormat.OpenXml.Wordprocessing.Hyperlink>())
                        {
                            HyperlinkRelationship oldLinkRelationship = package.MainDocumentPart.HyperlinkRelationships.FirstOrDefault(c => c.Id == hyperlink.Id.Value);

                            count++;
                            string oldUri = oldLinkRelationship.Uri.AbsoluteUri;
                            if (oldUri.StartsWith("aura://", StringComparison.OrdinalIgnoreCase))
                            {
                                if (isDeleteLink)
                                {
                                    DocumentFormat.OpenXml.Wordprocessing.Text newText = new DocumentFormat.OpenXml.Wordprocessing.Text("Deleted");
                                    DocumentFormat.OpenXml.Wordprocessing.Run run = new DocumentFormat.OpenXml.Wordprocessing.Run();
                                    run.AppendChild(newText);


                                    hyperlink.Parent.InsertBefore(run, hyperlink);
                                    hyperlink.Remove();

                                }
                                else
                                {
                                    HyperlinkRelationship newHRL = package.MainDocumentPart.AddHyperlinkRelationship(new Uri("aura://2222/2345/01-2334"), true);
                                    hyperlink.Id.Value = newHRL.Id;
                                }

                                package.MainDocumentPart.DeleteReferenceRelationship(oldLinkRelationship);
                            }
                        }
                    }

                    Console.WriteLine(count);
                    return true;

                }
                catch (Exception ex)
                {
                    return false;
                }
            }

    Tuesday, November 2, 2010 9:54 PM

All replies

  • More info: mainDocument.Descendants<DocumentFormat.OpenXml.Wordprocessing.Hyperlink>() returns the links based on how I create the hyperlink in word.  Say I am going to create 10 hyperlink, with url like abc://121212121/1121-aasdsa-121/basd-1/object

    If I click right mouse to bring up context menu, and select 'Hyperlink', enter the display text and url, the link will display like a regular link object with underline, for this kind of links, above method can return 10 links correctly.

    However, if I enter 10 lines of text, then select first word of each line, go to Insert|Hyperlink menu, enter url, the link doesn't show underline like regular one, for this kind of links, above method will only return partial links, may be 3 or 5. Further more, with second kind of links I created, now I enter some text before the links, I will get more links returned, even though may not be all of them. So seems like the function behaves totally depends on how I created the links? Is this a bug, and how to fix it?

    Any one can help?

     

    Wednesday, November 3, 2010 2:04 PM
  • Hi,

    Thanks for the reporting the problem.

    I tried your test ( then select first word of each line, go to Insert|Hyperlink menu, enter url, the link doesn't show underline like regular one) and I always get the underline and it gets recognize as a link.

    I tested this in:

    Word 2007 :12.0.6545.5000 SP2 MSO 12.0.6545.5004
    Word 2010:14.0.5128.5000

     If possible, could you please share your document?

    Thanks,
    Ankush


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. Regards, Ankush Microsoft Online Community Support
    Thursday, November 11, 2010 5:47 PM
  • How can I attach my doc to you?
    • Proposed as answer by SeanOK Friday, January 7, 2011 4:08 PM
    Thursday, November 11, 2010 9:58 PM
  • Hi Ankush,

    I have posted one word doc and my test app for you to take a look:

    https://cid-1e4383426d040a96.skydrive.live.com/redir.aspx?resid=1E4383426D040A96!115&authkey=L*nwweDnn0o%24

    Within the doc, there are 8 hyperlinks, after I ran the test app, only 3 of 8 was detected and fixed.

    Thanks

     

    Chang

     

     

     

    Friday, November 12, 2010 4:31 PM
  • Hello Chang,

    Thanks for the document.

    My first question is, how did you generate this document.

    My guess is, some links are inserted as within the page and some are external.

    If the links are within the page, they are inserted using a special field. If the links are external, they are the actual hyperlinks.

    I see these 2 declarations:

    <

     

    w:r>

    <

     

    w:fldChar w:fldCharType="begin"/>

    </

     

    w:r>

    <

     

    w:r w:rsidR="008D133A">

    <

     

    w:instrText>HYPERLINK "aura://e753ce21-2c22-4d88-9811-c064fd60ce63/b51f5aab-05ed-4caa-8fed-a498f51c2b58_0/faefad64-77db-46fc-87c3-466a9c6df869/5/1/-1/null"</w:instrText>

    </

     

    w:r>

    <

     

    w:r>

    <

     

    w:fldChar w:fldCharType="separate"/>

    </

     

    w:r>

    and

    <

     

    w:hyperlink r:id="rId4" w:history="1">

    <

     

    w:proofErr w:type="spellStart"/>

    <

     

    w:r w:rsidR="008902AD">

    <

     

    w:rPr>

    <

     

    w:rStyle w:val="Hyperlink"/>

    </

     

    w:rPr>

    <

     

    w:t>Tirthankar</w:t>

    </

     

    w:r>

    <

     

    w:proofErr w:type="spellEnd"/>

    </

     

    w:hyperlink>

    so the first one is within the document and the second one is a hyperlink.

    Thanks
    Ankush


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. Regards, Ankush Microsoft Online Community Support
    • Proposed as answer by Ankush_Bhatia Monday, November 15, 2010 2:40 PM
    Monday, November 15, 2010 2:40 PM
  • Hi Ankush,

    I am not sure I quite understand the difference between links within a page and external links. The links were created with these steps:

    Select the text, then click on menu 'Insert|Hyperlink', with Insert Hyperlink window, just enter the url, such as aura://e753ce21-2c22-4d88-9811-c064fd60ce63/b51f5aab-05ed-4caa-8fed-a498f51c2b58_0/faefad64-77db-46fc-87c3-466a9c6df869/5/1/-1/null, and repeat this steps for all other links.  Like my second post described, if I insert a tab before the links,and run the app, it will recongize more links, seems like some internal data was changed.

    Thanks

    Chang

     

    Monday, November 15, 2010 5:19 PM
  • Hi Chang,

    In the left of "Insert Hyperlink" dialog, you will see 4 options: which decides where this link point to (to a webpage or in the same document..etc)

    I suggest you try this, create a document and insert hyperlinks with "Existing file or webpage" option and then insert some links with "place in This document" option and then analyze the document.xml and see how these hyperlinks gets represented.

     

    Thanks,
    Ankush


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. Regards, Ankush Microsoft Online Community Support
    Friday, November 19, 2010 11:54 AM