我需要修改.xls工作簿中的很多超链接。我的链接是这样的:
\\\mysrv001\some\path\documents.doc
我需要将\\\mysrv001
替换为\\\mysrv002
我试过类似这样的东西,但是我收到一个错误:“对象不支持这个属性或方法”。我该如何解决这个问题?
Sub test()
Dim hLink As Hyperlink
Dim wSheet As Worksheet
Dim path As String
For Each wSheet In Worksheets
For Each hLink In wSheet.Hyperlinks
path = Right(hLink, Len(hLink) - 11)
hLink.Address = "\\mysrv003\" & path
Next hLink
Next
End Sub
PS:我正在使用Office 2000
答案 0 :(得分:8)
试试这个
Sub test()
Dim hLink As Hyperlink
Dim wSheet As Worksheet
For Each wSheet In Worksheets
For Each hLink In wSheet.Hyperlinks
hLink.Address = Replace(hLink.Address, "\\mysrv001\", "\\mysrv002\")
Next hLink
Next
End Sub
答案 1 :(得分:5)
糟糕!您正在提取并保留路径字符串的左侧部分,您真正想要做的是丢弃它!
编辑:此外,您不能在这样的超链接对象上使用字符串函数(Left
,Right
,Len
...)。这是造成错误的原因。您必须提取Hyperlink对象的Address属性 - 这是一个字符串。
替换
path = Left(hLink, 11) ' throws error: Object doesn't support this property...
带
path = Mid(hLink.Address, 12) ' returns "some\path\documents.doc"
' or, equivalently:
'path = Right(hLink.Address, Len(hLink.Address) - 11)