Excel VBA: Index = ZOrderPosition in einer Shapes-Auflistung?

Entspricht der Index einer Form in einer Shapes-Auflistung eines Arbeitsblatts immer der ZOrderPosition? (Grundsätzlich kann man nicht direkt nach dem Index einer bestimmten Form fragen).

Ich habe bestätigt, dass dies in einigen Fällen zutrifft (mit bis zu 3000 Shapes), aber ich habe keine Dokumentation dazu gefunden.

Ich habe die gesamte Sammlung durchlaufen und nach möglichen Unterschieden zwischen Index und ZOrderPosition gefragt:

Sub dump_shapes()
' Dump information on all shapes in a Shapes collection
    Dim shc As Shapes
    Set shc = ActiveSheet.Shapes
    Dim shp As Shape
    For Each shp In shc
      Dim sh2 As Shape
      Set sh2 = sh2idxzosh_shc(shp)
      Dim zoidx As Long
      ' The second argument is not actually the Index, but since we are traversing the 
      ' whole collection, and Index and ZOrderPosition are at most permutations, we are
      ' covering all of the possible Indexes.
      zoidx = idx2zo_shc(shc, shp.ZOrderPosition)
    Next shp
End Sub

Die für die Abfrage verwendeten Funktionen sind nachstehend aufgeführt. Da die Warnung in den MsgBox'es nie auftauchte, bedeutet dies für die bewerteten Fälle, dass Index = ZOrderPosition.

' Functions between the set of shapes S and the set of natural numbers N.
' O=ZOrderPosition : S -> N  (function exists)
' D=From Index : N -> S  (function exists)
' D^-1=Index : S -> N  (function does not exist)
' f=OoD : N -> N  (can be constructed; this is expected to be only a permutation, 
'  i.e., bijective)
' g=DoO : S -> S  (can be constructed)

Function sh2idxzosh_shc(ByRef sh As Shape) As Shape
    Dim shc As Shapes
    Set shc = sh.Parent.Shapes
    Dim zo As Long
    zo = sh.ZOrderPosition
    Dim sh2 As Shape
    Set sh2 = shc(zo)
    ' g=DoO : S -> S
    ' Test Shape : g(S)=S for all s? If so, g=DoO=I ; D=O^-1 ; D^-1=O. Thus, the Index 
    '  is equal to the ZOrderPosition.
    ' Use ZOrderPosition to test Shape : O(g(s))=O(s) for all s? I.e., OoDoO=O? If so, 
    '  given that O is bijective, OoD=I ; D=O^-1 ; D^-1=O. Thus, the index is equal to 
    '  the ZOrderPosition.
    Dim zo2 As Long
    zo2 = sh2.ZOrderPosition
    If (zo2 <> zo) Then
      MsgBox ("Compound ZOrderPosition: " & zo2 & ", ZOrderPosition: " & zo)
    End If
    Set sh2idxzosh_shc = sh2
    'Set sh2 = Nothing
End Function

Function idx2zo_shc(ByRef shc As Shapes, idx As Long) As Integer
    Dim sh As Shape
    Set sh = shc(idx)
    Dim zo As Long
    zo = sh.ZOrderPosition
    ' f=OoD : N -> N
    ' Test index : f(i)=i for all i? If so, f=OoD=I ; D=O^-1 ; D^-1=O. Thus, the Index is 
    '  equal to the ZOrderPosition.
    If (zo <> idx) Then
      MsgBox ("Index: " & idx & ", ZOrderPosition: " & zo)
    End If
    idx2zo_shc = zo
End Function

PS: Ich habe die Funktionen für die ChartObjects-Auflistung eines Arbeitsblatts angepasst und auch den Äquivalenzindex = ZOrder überprüft.

PS2: Man kann sich fragen, ob dies typisch (oder gar garantiert) für eine Sammlung ist. ImExcel VBA: Nicht fortlaufende Nummerierung von ZOrderPosition in der Shapes-Auflistung Ich habe einen Fall gemeldet, in dem nicht nur dies nicht zutrifft, sondern auch Index und ZOrderPosition keine Permutationen sind (beachten Sie, dass es sich um die Shapes-Auflistung der mit einem ChartObject verknüpften Shape handelt, die sich von der oben genannten unterscheidet).

Bearbeiten: Siehe Bearbeiten inExcel VBA: So erhalten Sie einen Verweis auf eine Form aus dem ChartObject.

Antworten auf die Frage(1)

Ihre Antwort auf die Frage