如何使用 VBA 从 Excel 单元格添加 Word 超链接 (hyperlink.add) [英] How to add Word hyperlinks from Excel cells using VBA (hyperlink.add)

查看:321
本文介绍了如何使用 VBA 从 Excel 单元格添加 Word 超链接 (hyperlink.add)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 Excel VBA 根据 Excel 单元格范围在 Word 文档中创建链接.但是当它到达 hyperlinks.add 行时,我得到 run-time error ‘450’: Wrong number of arguments or invalid property assignment".

I want to use Excel VBA to make links in a Word document based upon Excel cell ranges. But when it gets to the hyperlinks.add line I get "run-time error ‘450’: Wrong number of arguments or invalid property assignment".

几乎完全相同的代码在 Word VBA 中运行良好.我不明白错误信息.虽然我对 Excel VBA 非常熟悉,但 Word VBA 选择和范围让我感到困惑.

Almost the exact same code works fine in Word VBA. I don't understand the error message. While I'm quite familiar with Excel VBA, Word VBA selections and ranges confuse me.

我在下面使用字符串而不是范围制作了代码示例,在每种情况下,代码都会在 test.docx 文档的末尾成功插入文本,但 Word VBA 会在其下方插入带有链接的文本,Excel VBA 代码在 hyperlinks.add 行失败.

I made code examples below using strings instead of ranges, in each case the code inserts text successfully at the end of the test.docx document but while the Word VBA inserts text with a link below that, the Excel VBA code fails at the hyperlinks.add line.

这是不起作用的 Excel VBA 代码:

Here is the Excel VBA Code that is not working:

Sub wordLinkFromExcelRanges()
Dim wApp As Word.Application, wDoc As Word.Document
Dim linkText As String, link As String
  linkText = "google"
  link = "http://www.google.com"
  Set wApp = New Word.Application
  wApp.Visible = True
  Set wDoc = wApp.Documents.Open("C:\test\test.docx")
  With wApp.Selection
    .EndKey 6, 0 'go to end of doc
    .TypeParagraph
    .TypeText "text without link"
    .TypeParagraph
    wDoc.Hyperlinks.Add Anchor:=Selection.Range, Address:=link, _
    SubAddress:="", ScreenTip:="", TextToDisplay:=linkText
  End With
  wApp.Quit
  Set wDoc = Nothing
  Set wApp = Nothing
End Sub

这是有效的 Word VBA 代码:

Here is the Word VBA code that is working:

Sub wordLinkFromWord()
Dim wD As Document
Dim linkText As String, link As String
linkText = "google"
link = "http://www.google.com"
Set wD = ActiveDocument
With Selection
  .EndKey 6, 0
  .TypeParagraph
  .TypeText "text without link"
  .TypeParagraph
  wD.Hyperlinks.Add Anchor:=Selection.Range, Address:=link, _
  SubAddress:="", ScreenTip:="", TextToDisplay:=linkText
End With
End Sub

谢谢!

推荐答案

我想我发现了问题.您指的是 Selection.Range,但我认为在此上下文中没有选择任何内容.

I think I found the issue. You are referring to Selection.Range but I don't think anything is selected in this context.

你可以试试这个:

Sub wordLinkFromExcelRanges()
    Dim wApp        As Word.Application: Set wApp = New Word.Application
    Dim wDoc        As Word.Document
    Dim linkText    As String: linkText = "google"
    Dim link        As String: link = "http://www.google.com"

    wApp.Visible = True
    Set wDoc = wApp.Documents.Open("C:\test\test.docx")
    With wApp.Selection
      .EndKey 6, 0
      .TypeParagraph
      .TypeText "text without link"
      .TypeParagraph
      wDoc.Hyperlinks.Add Anchor:=.Range, Address:=link, SubAddress:="", ScreenTip:="", TextToDisplay:=linkText
    End With
    wApp.Quit
End Sub

这篇关于如何使用 VBA 从 Excel 单元格添加 Word 超链接 (hyperlink.add)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆