问题
is there any way to programmaticaly add an hyperlink to the selected text in a MS Word Add-In?
Thanks in advance.
回答1:
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.
回答2:
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)
来源:https://stackoverflow.com/questions/8534231/make-hyperlink-in-ms-word-add-in