2011-12-16 34 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