deHaar
deHaar

Reputation: 18578

How can I replace a plain URL string in a Paragraph.Text by a Hyperlink?

I have a Microsoft Word Document with several Paragraphs and I have to replace a plain URL in one of the paragraphs by a Hyperlink that can actually be clicked.

The entire link is known before, I just have to find the paragraph containing it and thenreplace that text by a real link (HTML doesn't work).

// this is how I determine the positions in the text of the paragraph
var urlStartIndex = paragraphRange.Text.IndexOf(linkUrl);
var urlEndIndex = urlStartIndex + linkUrl.Length;

but I am not able to replace just the part of the Paragraph.Text with a link.

The example text in the paragraph is like

Click the following link: https://www.somewhereinthe.net/something/somethingmore

where just the part starting with https should be entirely replaced by a real link to make it

Click the following link: somewhere

At first, I tried to directly insert it instead of the url, but simply removing the url and pasting the link instead can obviously not easily be done.

The following try replaced the entire paragraph by a link that is clickable but not highlighted (blue and underlined font), but has its TextToDisplay as Address, too, and thus, leads to nowhere in the web:

private static void ReplacePaypalLinkIn(Word.Paragraph paragraph, Word.Document docx, string linkUrl)
{
    var paragraphRange = paragraph.Range;
    // find the start index of the url
    var urlStartIndex = paragraphRange.Text.IndexOf(linkUrl);
    // calculate the last index of the url
    var urlEndIndex = urlStartIndex + linkUrl.Length;
    // and add it to the hyperlinks of the document at this very paragraph (Anchor)
    var link = docx.Hyperlinks.Add(Anchor: paragraphRange, Address: linkUrl, TextToDisplay: "somewhere");
}

How can I replace that URL in a way that actually results in a Paragraph that includes the text before the URL and, instead of the URL, a clickable and well formatted link substituted by the given TextToDisplay?

Upvotes: 0

Views: 898

Answers (1)

Cindy Meister
Cindy Meister

Reputation: 25673

Word manages hyperlinks through HYPERLINK fields. Fields are what Word uses to work with dynamic content. Some examples, besides hyperlinks, are page numbers, dates, cross-references - anything that can change or trigger an action.

So what needs to happen here is 1) locate the URL and 2) replace it with a hyperlink.

Usually, it's expedient to use Word's Find feature to locate a specific string in the text. (The technique illustrated by the code in the question relies on the index location of the string in the document. This approach is, unfortunately, not at all reliable as Word document's tend to harbour "hidden characters": control characters that aren't visible, but change the index location values of document content.)

The following code snippet demonstrates how to use Find to locate the URL string in the document and insert a Hyperlink (field) in its place, using the method Hyperlinks.Add:

string targetUrl = @"https://www.somewhereinthe.net/something/somethingmore";
string displayText = "somewhere";
Word.Range rngUrl = doc.Content;

Word.Find fd = rngUrl.Find;
fd.Text = targetUrl;
bool success = fd.Execute();
if (success)
{
    doc.Hyperlinks.Add(rngUrl, targetUrl, Type.Missing, Type.Missing, displayText, Type.Missing);
}

What various properties are available for Find can be found in the Word language reference. It's a good idea to understand what all can be involved when Execute runs. What's shown here is reduced to what's needed to demonstrate the basic principle.

What's important to know, immediately, is that the Range the search is running on will switch to the "found" term. So rngUrl begins by encompassing the body of the document, but on locating the URL, it reduces to the URL. Thus, the hyperlink will replace the URL.

Upvotes: 1

Related Questions