简体   繁体   English

如何使用Excel VBA引用形状的超链接

[英]How do I refer to a shape's hyperlinks using Excel VBA

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. 但这给了我debug.print行上的运行时错误91。 Googling gives me nothing. 谷歌搜索给我什么。 So, have any of you got an idea of how to print the addresses? 那么,你们中的任何一个都知道如何打印地址吗?

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. 一个形状没有.Hyperlinks财产,只有.Hyperlink一个,你会从中得到一个错误,如果没有关联的超链接,所以你需要一个错误处理程序。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM