2017-03-01 65 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"