【发布时间】:2019-06-02 04:48:31
【问题描述】:
我想使用 Excel VBA 根据 Excel 单元格范围在 Word 文档中创建链接。但是当它到达hyperlinks.add 行时,我得到"run-time error ‘450’: Wrong number of arguments or invalid property assignment"。
几乎完全相同的代码在 Word VBA 中运行良好。我不明白错误信息。虽然我对 Excel VBA 非常熟悉,但 Word VBA 的选择和范围让我很困惑。
我在下面使用字符串而不是范围创建了代码示例,在每种情况下,代码都会在 test.docx 文档的末尾成功插入文本,但是当 Word VBA 插入带有链接下方的文本时,Excel VBA 代码在hyperlinks.add 行。
这是不工作的 Excel VBA 代码:
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 代码:
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
谢谢!
【问题讨论】: