none
Adding hyperlinks to word document's paragraph using (foreach loop, Interop.word assembly) RRS feed

  • Question

  • I have a Paragraph variable located in ms-word document which have the following Text :

    DI : Note de délégation du pouvoir, Note de crédit, Tarifs, notes «Modalité de paiement », Grille des parités monétaires, Fichier fournisseur, Conventions particulières, Budget, Mode Opératoire « Avant vente », Mode Opératoire « Revue de Contrat », Procédures : « Maîtrise des documents », « Maîtrise des enregistrements », « Maîtrise des non-conformité », « Gestion des Réclamations Clients », « Audit Interne », « Actions Correctives & Préventives », Spécification Qualité « Contrôle à la Réception »

    and a An array of strings that contains some strings phrom the paragraph, for example :

    ["Note de crédit" , "Tarifs" , "Spécification Qualité « Contrôle à la Réception »"]

    I need is to add an Hyper-link to that string in the paragraph.

    I tried this:

    var docInt = new List<string>{ "Note de crédit#http://link1.com", "Tarifs#http://link2.com", "Spécification Qualité « Contrôle à la Réception »#http://link3.com" };

     foreach (var docs in docInt)
                            {
                                var word = docs.Split('#')[0];
                                var link = docs.Split('#')[1];

                                var startIndex = paragraph.Range.Text.IndexOf(word, StringComparison.CurrentCulture);
                                var endIndex = word.Length;
                                if (startIndex > 0)
                                {
                                    startIndex += paragraph.Range.Start;
                                    endIndex += startIndex;
                                }

                                object startLocation = startIndex;
                                object endLocation = endIndex;
                                var rng = document.Range(ref startLocation, ref endLocation);
                                // document.Hyperlinks.Add(rng, link);
                                rng.Hyperlinks.Add(rng, link);
                                //   paragraph.Range.Hyperlinks.Add(rng, link);
                            }

    In case the "docs" List contains one string element it works .

    in case the "docs" List contains many elements, either i got a misplaced hyperlinks or only one hyperlink.

    i tried document.Hyperlinks.Add(rng, link); &&  rng.Hyperlinks.Add(rng, link); && paragraph.Range.Hyperlinks.Add(rng, link); and the same result.

    any help please ?? i'm kind of stuck here for 3 days now.

    thanks in advance


    • Edited by Soufiaane Monday, May 13, 2013 9:50 AM
    • Moved by CoolDadTx Monday, May 13, 2013 1:57 PM Office related
    Monday, May 13, 2013 9:49 AM

Answers

  • Hello,

    Sorry for my delayed reply.

    I have reproduced the issue as you described using the code you provided. However, I find something interesting.

    In order to check which part of the content will be added with hyperlink, I add this statement before "rng.Hyperlinks.Add(rng, link);":

    MessageBox.Show(rng.Text);

    For the first "docs" in "docInt", it shows as expected. But for the rest of "docs", it shows something odd.

    But if you comment out the statement which add the hyperlink("rng.Hyperlinks.Add(rng, link);"), all the "docs" show as expected!

    So I guess the hyperlink added into the document will overran the index of text. Then, I found that there seems to be a regular pattern within this. Please have a look at these code:

    var docInt = new List<string>{ "Note de crédit#http://link1.com", 
        "Tarifs#http://link2.com", "Spécification Qualité « Contrôle à la Réception »#http://link3.com"};
                
    int lenthOfLink = 0;
    foreach (var docs in docInt)
    {
        var word = docs.Split('#')[0];
        var link = docs.Split('#')[1];
        var startIndex = myDoc.Paragraphs[1].Range.Text.IndexOf(word, StringComparison.CurrentCulture) + lenthOfLink;
        var endIndex = word.Length + startIndex;
    
        object startLocation = startIndex;
        object endLocation = endIndex;
    
        var rng = myDoc.Range(ref startLocation, ref endLocation);
        rng.Hyperlinks.Add(rng, link);
    
        int a = docInt.Count;
    
        lenthOfLink += link.Length*2;
        int docIndex = docInt.IndexOf(docs);
        for (int i = 1; i <= docIndex + 1; ++i)
        {
            lenthOfLink += i;
        }
    }

    This code snippet should work as expected.

    In order to get the correct index, we need to add extra numbers after adding a hyperlink in the document. I haven't found any explanation of this, but I hope my code snippet could be a workaround for you.

    Best Regards,


    Damon Zheng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Soufiaane Wednesday, May 15, 2013 2:16 PM
    Wednesday, May 15, 2013 1:42 PM
    Moderator
  • i solved this by reversing the docInt List fist so i start adding Hyper-links backwards, this way the startIndex remains the same. but i guess your method should work as well. many thanks
    Wednesday, May 15, 2013 2:15 PM

All replies

  • 31 Views and no replies;

    well after debugging this is what i get in the rng.Text value for eache iteration:

    1) "Note de crédit"

    2)"ne\\Documents\"

    3)"isual"

    and if i add more Strings items to the "docInt" variable i get theses:

    4)"udio 2010\\Projects\\FenieBro"

    5)"etteIntra\\WebSite\\Configura"

    6)"on\\Qualite\\Proces"

    7)"s\\FichProcess\\Processus"

    8)"ommerc"

    9)"l 2013/Documents Interne/Proces"

    10)"s Commercial 2013.doc" Note de dél"

    11)"ation du pouvoir, Note de crédit, Tari"

    12)", notes «Modalité de paiement »,"

    13)"\Documents\\Visual Studio 2010\"

    14)"rojects\\FenieBrossetteIntra\\WebSit"

    15)"\Configuration\\Q"

    ....

    ....

    so as i see from N°2 to N°10 it's strings that if concatenated looked like the path to the application

    N°11 and N° 12 is situated within the paragraph but it's arbitrary and it's hyper-links.

    and again from N°13 and so on look like another loop into the application path .

    I don't know what i am doing wrong here ?

    any reply would be appreciated , thanks.

    Monday, May 13, 2013 11:59 AM
  • "Fack".replace('a','u') + " " + "Microsoft";
    Please watch your tongue! This is not a paid forum, no one has the responsibility to reply as you expect.
    Wednesday, May 15, 2013 5:33 AM
  • Hello,

    Sorry for my delayed reply.

    I have reproduced the issue as you described using the code you provided. However, I find something interesting.

    In order to check which part of the content will be added with hyperlink, I add this statement before "rng.Hyperlinks.Add(rng, link);":

    MessageBox.Show(rng.Text);

    For the first "docs" in "docInt", it shows as expected. But for the rest of "docs", it shows something odd.

    But if you comment out the statement which add the hyperlink("rng.Hyperlinks.Add(rng, link);"), all the "docs" show as expected!

    So I guess the hyperlink added into the document will overran the index of text. Then, I found that there seems to be a regular pattern within this. Please have a look at these code:

    var docInt = new List<string>{ "Note de crédit#http://link1.com", 
        "Tarifs#http://link2.com", "Spécification Qualité « Contrôle à la Réception »#http://link3.com"};
                
    int lenthOfLink = 0;
    foreach (var docs in docInt)
    {
        var word = docs.Split('#')[0];
        var link = docs.Split('#')[1];
        var startIndex = myDoc.Paragraphs[1].Range.Text.IndexOf(word, StringComparison.CurrentCulture) + lenthOfLink;
        var endIndex = word.Length + startIndex;
    
        object startLocation = startIndex;
        object endLocation = endIndex;
    
        var rng = myDoc.Range(ref startLocation, ref endLocation);
        rng.Hyperlinks.Add(rng, link);
    
        int a = docInt.Count;
    
        lenthOfLink += link.Length*2;
        int docIndex = docInt.IndexOf(docs);
        for (int i = 1; i <= docIndex + 1; ++i)
        {
            lenthOfLink += i;
        }
    }

    This code snippet should work as expected.

    In order to get the correct index, we need to add extra numbers after adding a hyperlink in the document. I haven't found any explanation of this, but I hope my code snippet could be a workaround for you.

    Best Regards,


    Damon Zheng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Soufiaane Wednesday, May 15, 2013 2:16 PM
    Wednesday, May 15, 2013 1:42 PM
    Moderator
  • i solved this by reversing the docInt List fist so i start adding Hyper-links backwards, this way the startIndex remains the same. but i guess your method should work as well. many thanks
    Wednesday, May 15, 2013 2:15 PM
  • Cheers for you!

    I should have found this way. Adding hyperlinks backwards won't overrun the index of the text before it.

    Have a good day.


    Damon Zheng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, May 15, 2013 2:45 PM
    Moderator