Make hyperlink in MS Word Add-in

流过昼夜 提交于 2019-12-02 01:11:43

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!