Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Word UserForm VBA:来自书签的超链接_Vba_Ms Word - Fatal编程技术网

Word UserForm VBA:来自书签的超链接

Word UserForm VBA:来自书签的超链接,vba,ms-word,Vba,Ms Word,我能够通过用户表单从用户输入填充Word文档上的书签位置 我想做的是将输入的文本转换为超链接 以下代码片段用于将文本插入适当位置: Private Sub CommandButton1_Click() Dim benchmarkURL As Range Set benchmarkURL = ActiveDocument.Bookmarks("benchmark").Range benchmarkURL.Text = Me.benchmarkURLTextBox.Valu

我能够通过用户表单从用户输入填充Word文档上的书签位置

我想做的是将输入的文本转换为超链接

以下代码片段用于将文本插入适当位置:

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
任何建议都将不胜感激

提前谢谢

Hyperlinks.Add(ActiveDocument.Bookmarks.Add "benchmark", benchmarkURL)
这一行至少有两个错误,可能更多地取决于您希望超链接链接到的内容

  • 您省略了超链接的父对象,该超链接应为ActiveDocument
  • 不应该有任何括号作为 超链接。添加未分配给任何对象

  • 您可以在这里找到更多信息:

    我找到了更好的解决方案。对于那些需要的人,发布如下:

    '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"
    

    只是为了对Jame的回答做进一步的描述

    '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"
    
    意思是把这些信息放进去

    'URL of Benchmark Data
    ActiveDocument.Hyperlinks.Add Anchor:=<<Where link will display>>, Address:= <<Where the link will go to>>, SubAddress:="", ScreenTip:="", TextToDisplay:="<<what the link     text should say"
    
    '/////////////
    Set benchmarkURL = oDoc.Bookmarks(strText2).Range
    With oDoc.Tables(r).Cell(i, 1)
      .Range.Hyperlinks.Add Anchor:=oDoc.Tables(r).Cell(i, 1).Range, Address:="", SubAddress:=strText2, ScreenTip:="", TextToDisplay:=strText
    End With