Pasted Shape not seen as “Latest” Shape

后端 未结 1 1441
Happy的楠姐
Happy的楠姐 2021-01-22 01:03

I\'m in the process of automating the production of a PowerPoint report from and Excel spreadsheet. I\'ve got the process working up until I paste a table.

I\'m pasting

1条回答
  •  失恋的感觉
    2021-01-22 01:34

    'Shapes.Count' ≠ Shape Index# !

    The .Count is not the same as the upper limit of current shape .Index numbers.

    The numbering system is easier understood by listing all the shapes within the document:

    Sub ListShapes()
        'hit CTRL+G to view output in Immediate Window
        Dim sh As Shape, sld As Slide, idx As Long
        Set sld = ActivePresentation.Slides(1) '<-- change to your slide number
        For Each sh In sld.Shapes
            idx = idx + 1
            Debug.Print "Shape ID#" & sh.Id, "Index #" & idx, "Name: " & sh.Name
        Next sh
        Debug.Print "Count of shapes: " & sld.Shapes.Count
    End Sub
    

    NOTE: There is alternative code for Excel at the bottom of this post!

    To demonstrate, we can add shapes to a new document:

    • First, add one rectangle manually by clicking Insert (on the ribbon)
    • [If using Excel, click Illustrations], then Shapes, and the rectangle symbol.
    • Draw the shape, then hit Ctrl+C to copy it, and hit Ctrl+C four times to paste 4 copies.
    • Run the above procedure, and the output will be:

      Shape ID#2 Index #1 Name: Rectangle 1
      Shape ID#3 Index #2 Name: Rectangle 2
      Shape ID#4 Index #3 Name: Rectangle 3
      Shape ID#5 Index #4 Name: Rectangle 4
      Shape ID#6 Index #5 Name: Rectangle 5
      Count of shapes: 5         

    Note that the Index is not a property of this object, but it counted in order that Excel's storing the shapes in memory (same as the order returned by the For Each..Next statement.

    • You can prove this by running:

      Debug.Print ActivePresentation.Slides(1).Shapes(5).Name  
      

      ...which in this case return Rectangle 5.

    Another way to understand how Excel is storing the shapes is with the Watch Window. Add a breakline or Stop in the middle of the loop, then highlight ws.Shapes, right-click it, choose Add Watch... and click OK. Browse through the tree to discover the varies properties/attributes of the shapes within the document.


    • Next, if we delete the "middle rectangle" and run the above procedure again, we will get:

      Shape ID#2 Index #1 Name: Rectangle 1
      Shape ID#3 Index #2 Name: Rectangle 2
      Shape ID#5 Index #3 Name: Rectangle 4
      Shape ID#6 Index #4 Name: Rectangle 5
      Count of shapes: 4         

    The ID and Name of remaining shapes do not change, but the Index is renumbered to reflect the new "order".

    ...thus to return the name Rectangle 5 we now need to use:

    Debug.Print ActivePresentation.Slides(1).Shapes(4).Name  
    

    Referring to shapes (including controls)

    When you refer to a shape by number, like .Shapes(

    0 讨论(0)
提交回复
热议问题