Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace part of hyperlink

Tags:

excel

vba

I need to modify a lot of hyperlinks in an .xls workbook. My links are like this:

\\\mysrv001\some\path\documents.doc and I need to replace \\\mysrv001 with \\\mysrv002

I tried something like this, but I get an error: "Object doesn't support this property or method". How do I fix this?

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: I'm using Office 2000

like image 895
Remus Rigo Avatar asked Dec 07 '22 19:12

Remus Rigo


2 Answers

try this

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
like image 95
chris neilsen Avatar answered Dec 20 '22 15:12

chris neilsen


Whoops! You're extracting and keeping the left part of your path string, where what you really want to do is to discard it!

EDIT: Also, you can't use string functions (Left, Right, Len...) on a Hyperlink object like that. This is what is causing the error. You have to extract the Hyperlink object's Address property -- that's a string.

Replace

path = Left(hLink, 11) ' throws error: Object doesn't support this property...

with

path = Mid(hLink.Address, 12) ' returns "some\path\documents.doc"
' or, equivalently:
'path = Right(hLink.Address, Len(hLink.Address) - 11) 
like image 41
Jean-François Corbett Avatar answered Dec 20 '22 13:12

Jean-François Corbett