2017-03-01 63 views
0

我能夠通過UserForm從用戶輸入填充Word文檔的書籤位置。Word UserForm VBA:書籤超鏈接

我想要做的是將輸入的文本轉換爲超鏈接。

下面的代碼SNIPPIT用來插入文本到適當的位置:

Private Sub CommandButton1_Click() 

    Dim benchmarkURL As Range 
    Set benchmarkURL = ActiveDocument.Bookmarks("benchmark").Range 
    benchmarkURL.Text = Me.benchmarkURLTextBox.Value 
    ActiveDocument.Bookmarks.Add "benchmark", benchmarkURL 

    Me.Repaint 

    'Update the fields to populate the references of the bookmarks 
    UpdateAllFields 

    UserForm1.Hide 

End Sub 

我嘗試以下,沒有工作:

Private Sub CommandButton1_Click() 

    Dim benchmarkURL As Range 
    Set benchmarkURL = ActiveDocument.Bookmarks("benchmark").Range 
    benchmarkURL.Text = Me.benchmarkURLTextBox.Value 
    Hyperlinks.Add(ActiveDocument.Bookmarks.Add "benchmark", benchmarkURL) 

    Me.Repaint 

    'Update the fields to populate the references of the bookmarks 
    UpdateAllFields 

    UserForm1.Hide 

End Sub 

任何意見將非常感激

在此先感謝

回答

0
Hyperlinks.Add(ActiveDocument.Bookmarks.Add "benchmark", benchmarkURL) 

這條線至少有兩件事是錯誤的,可能更多取決於您希望超鏈接鏈接到什麼。

  1. 您已省略超鏈接的應該是ActiveDocument的父對象。
  2. 不應該有任何括號作爲返回值 Hyperlinks.Add沒有被分配給任何東西。

你可以在這裏找到更多的信息:https://msdn.microsoft.com/en-us/library/office/ff837214(v=office.15).aspx

0

我找到了一個更好的解決方案。對於那些需要,它張貼如下:

'URL of Benchmark Data 
Dim benchmarkURL As Range 
Set benchmarkURL = ActiveDocument.Bookmarks("benchmark").Range 
benchmarkURL.Text = Me.benchmarkURLTextBox.Value 
ActiveDocument.Hyperlinks.Add Anchor:=benchmarkURL, Address:= _ 
benchmarkURL.Text, SubAddress:="", ScreenTip:="", TextToDisplay:= _ 
"Benchmark Data"