Quantcast
Channel: Word for Developers forum
Viewing all articles
Browse latest Browse all 4350

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

$
0
0

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



Viewing all articles
Browse latest Browse all 4350

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>