Hyperlinking a specific part of a sentence in a Word document

Hi Everyone,

I have a PowerShell script that presents the user with a Windows Form and lets them input certain information. Then, the user can click a button to generate a Word document that is based off a Word doc template that has default information in it. The information is contained within a table which has with a header/banner as the first row., then subsequent rows have 2 cells each, with the left cells containing descriptors for what is contained in the right cells. The right cells contain the user input data obtained from the textbox inputs.

One one of the rows, I have 2 long numbers that I’d like to have hyperlinked when the user clicks the button to generate the Word document. I’ve been successful in creating $tempRange1 variable that holds the range for the first line, which contains the long number I’d like to hyperlink and a $tempRange2 variable that holds the range for the second line, which contains the 2nd long number I’d like to hyperlink.

However, this hyperlinks everything on the first line and second line, respectively. I only want to hyperlink the long numbers in the sentence #1 and sentence #2, instead of hyperlink the entire sentences.

This requires me to set ranges more granular than to just the sentence level. I have seen an option to use the Characters method, but am not sure how to properly use it, and not sure if there’s not a better way to create offsets for start and end of range, so I can narrow the range down to just the long numbers I want to have hyperlinked only.

Could someone give me some sample usage tips so I can successfully specify start and end points to ranges within a sentence?

Below is some of my code. All it’s doing is checking the Windows Form textboxes #8 and #9 and if there is user input, it will replace the Word template’s default long numbers #1 and #2 with the values in the textboxes. Next, it sets the ranges at the Sentences level for where the long numbers #1 and #2 are located. In this case, they reside together in the same cell, found in sentence #1 and sentence #2 respectively. Next I add a hyperlink to the range by specifying the range and the desired hyperlink. Unfortunately, this hyperlinks the entire sentence rather than just the long number in the sentence. What I’d like is to hyperlink only the long number within the sentence and not the entire sentence. I’m just not sure the best way to go about this… Also, it seems that with this code, I can only replace text for one or the other… in other words, if I comment one block below out, the other works fine. When both are enabled, only 1 works. I’m not sure why. but my main issue is how to set a more granular range for sentence #1 and #2, to only the long numbers I want to hyperlink… I’d appreciate any help I can get. Thank you!

if ($textBox8.TextLength -ne 0)

$tempRange1 = $document.Tables(1).Cell(7,2).Range.Sentences[1]
$tempRange1.Text = $tempRange1.Text.replace("12345678", $textBox8.Text)
$tempRange1.Hyperlinks.Add($tempRange1, [hyperlink URL here])


if ($textBox9.TextLength -ne 0)

$tempRange2 = $document.Tables(1).Cell(7,2).Range.Sentences[2]
$tempRange2.Text = $tempRange1.Text.replace("23456789", $textBox9.Text)
$tempRange2.Hyperlinks.Add($tempRange2, [hyperlink URL here])


Can you please share sample of excel file, that way i can able to help.