2011-12-16 67 views

回答

4

代码选定的文本转换成指向微软网站的超链接:

 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"); 

     } 

的超级链接的实际文本,默认情况下,将是您选择的文本。如果你需要这个文本是不同的价值,例如 - 实际的URL地址,你可以简单地改变TextToDisplay属性:

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

我不知道你的逻辑究竟是如何动态的需要是,但我相信上面的例子会给你一个正确的方向。

2

如果你想做到这一点的VBA,这是

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

Sytnax:

expression.Add(Anchor, Address, SubAddress, ScreenTip, TextToDisplay, Target) 
+0

这工作就好了!谢谢 – 2012-01-02 17:33:19