Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make hyperlink in MS Word Add-in

is there any way to programmaticaly add an hyperlink to the selected text in a MS Word Add-In?

Thanks in advance.

like image 356
Jose Gregorio Avatar asked Nov 30 '25 10:11

Jose Gregorio


2 Answers

The code below converts selected text into a hyperlink which points to the Microsoft site:

        Microsoft.Office.Interop.Word.Range currentRange = Globals.ThisAddIn.Application.Selection.Range;

        if (currentRange != null)
        {
            Microsoft.Office.Interop.Word.Hyperlink hp = (Microsoft.Office.Interop.Word.Hyperlink)
                currentRange.Hyperlinks.Add(currentRange, "http://www.microsoft.com");

        }

The actual text of the hyperlink,by default, will be your selected text. If you need this text to be of different value, for instance - the actual url address, you can simply change the TextToDisplay property:

hp.TextToDisplay = "http://www.microsoft.com";

I'm not sure exactly how dynamic your logic needs to be but I believe the above example will give you a push in the right direction.

like image 193
Denys Wessels Avatar answered Dec 06 '25 05:12

Denys Wessels


If you are wanting to do this in VBA, it's

ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, ...

Sytnax:

expression.Add(Anchor, Address, SubAddress, ScreenTip, TextToDisplay, Target)
like image 37
chris neilsen Avatar answered Dec 06 '25 07:12

chris neilsen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!