Disclaimer: I have tried to search for a answer here, but I cannot seem to find one that matches my situation.
Currently I have a function that searches and replaces text in a document on original location. This is based on a object class I have created called the Translation Dictionary which contains the following properties:
The first property is the text I am searching, the second property is the text the search text will be replaced with. And the third property is the alternative text.
One of the scenario's that do happen is that the text that I am searching for gets replaced with link. This works great but the link itself is sadly not clickable in the final document. I do have a function to check if the SearchText is a valid URL or not. But that as far as I have gotten. Also I do not really know how to change that URL to an alternative text without losing the link itself. In theory this should be just another search and replace then.
But considering the link is not clickable. It just replaces that URL to the alternative text I have defined making me lose the link.
public void ConvertDocumentWords(string inputlocation, List<TranslationInformationCarrier> inputList)
{
try
{
string FileName = Path.GetFileName(inputlocation);
Application WordApp = new Application();
Word.Document doc = WordApp.Documents.Open($@"{inputlocation}");
doc.Activate();
foreach (var item in inputList)
{
string ReplacementText = item.ReplacementText;
string SearchText = item.SearchText;
string AlternativeText = item.AlternativeText;
FindAndReplace(WordApp, SearchText, ReplacementText);
DataLogger.LogInformation($"Replaced text {item.SearchText} in file {FileName}");
if (IsValidUrl(ReplacementText))
{
//Stuck here.
}
}
doc.Close(true);
WordApp.Quit(true);
GarbageSystemCleanUp();
}
catch (Exception ex)
{
DataLogger.LogError($"An exception occurred in the find and replace process: {ex.StackTrace}");
throw;
}
}
public static bool IsValidUrl(string url)
{
return Uri.IsWellFormedUriString(url, UriKind.Absolute);
}
/// <summary>
/// Function to find and replace a text.
/// </summary>
/// <param name="fileOpen">Word Application that is open.</param>
/// <param name="findText">Text to find the document.</param>
/// <param name="replaceWithText">Text to replace the find text to.</param>
static void FindAndReplace(Microsoft.Office.Interop.Word.Application fileOpen, object findText, object replaceWithText)
{
object matchCase = false;
object matchWholeWord = true;
object matchWildCards = false;
object matchSoundsLike = false;
object matchAllWordForms = false;
object forward = true;
object format = false;
object matchKashida = false;
object matchDiacritics = false;
object matchAlefHamza = false;
object matchControl = false;
object read_only = false;
object visible = true;
object replace = 2;
object wrap = 1;
//execute find and replace
fileOpen.Selection.Find.Execute(ref findText, ref matchCase, ref matchWholeWord,
ref matchWildCards, ref matchSoundsLike, ref matchAllWordForms, ref forward, ref wrap, ref format, ref replaceWithText, ref replace,
ref matchKashida, ref matchDiacritics, ref matchAlefHamza, ref matchControl);
}
After some time searching a lot of around and experimenting, I have found the answer to my own question. The important thing is that first setup the range as the target object and use. The range can be found using my search text. Then I can setup the Hyperlink using my replacement text, including my alternative text and link.
if (IsValidUrl(ReplacementText))
{
Hyperlinks myLinks = doc.Hyperlinks;
Range range = doc.Content;
range.Find.Execute(SearchText);
Hyperlink myLink = myLinks.Add(range, ReplacementText, null, ReplacementText, AlternativeText);
}