How do I refer to a shape's hyperlinks using E

2019-09-08 04:54发布

I have a spreadsheet which contain several hyperlinks, some of which are from shapes. I am trying to loop through them, to see where each of them point in order to later remove some of them. For the hyperlinks contained in cells the following loop has worked:

Sub a()
  Dim ws As Worksheet, hl As Hyperlink, o As Shape

  For Each ws In Worksheets
    For Each hl In ws.Hyperlinks
      Debug.Print hl.Address
    Next
  Next
End Sub

But that seems to skip all the hyperlinks originating from shapes or other objects.

Is there any way I can loop through those as well? I have tried stuff like:

Sub a()
  Dim ws As Worksheet, hl As Hyperlink, o As Shape


  For Each ws In Worksheets
    For Each o In ws.Shapes
      For Each hl In o.Hyperlinks
        Debug.Print hl.Address
      Next
    Next
  Next
End Sub

But that gives me a runtime error 91 on the debug.print line. Googling gives me nothing. So, have any of you got an idea of how to print the addresses?

1条回答
叛逆
2楼-- · 2019-09-08 05:24

A Shape doesn't have a .Hyperlinks property, only a .Hyperlink one and you'll get an error from it if there is no associated hyperlink, so you need an error handler. For example:

        On Error Resume Next
        Set hl = o.Hyperlink
        On Error GoTo 0
        If Not hl Is Nothing Then 
            Debug.Print hl.Address
            set hl = Nothing
        End If
查看更多
登录 后发表回答