粘贴的形状不视为“最新”形状

时间:2018-07-04 09:08:08

标签: excel vba excel-vba powerpoint-vba shapes

我正在从PowerPoint和Excel电子表格中自动生成PowerPoint报表。在粘贴表格之前,我已经完成了整个过程。

我正在使用PPApp.CommandBars.ExecuteMso ("PasteSourceFormatting")将表格粘贴到PowerPoint,表格在幻灯片上显示为一个形状(第三个形状)。

要引用我正在使用的Set pShape = Slide2.Shapes(Slide2.Shapes.Count)的新形状,但是现在粘贴时,pShape被指定为“形状2”(而不是“形状3”)。在对象的粘贴和分配之间需要做些什么?

下面的代码,注释了发生问题的位置。 (已删除完整代码;可见的here

'Copy tables from Excel
Set rng = ws.Range("A:A")
rng.ColumnWidth = 22.75
Set rng = ws.Range("A4:C27")

'Copy the table range
Application.CutCopyMode = False
rng.Copy
Application.Wait (Now + TimeValue("0:00:02"))

'The issue occurs here!!! '-------------------------------------
'Paste the table in to the slide
Slide2.Select
PPApp.CommandBars.ExecuteMso ("PasteSourceFormatting")

'Name the new shape object
Set pShape = Slide2.Shapes(Slide2.Shapes.Count)
pShape.Name = "Slide_2_Table_1"
pShape.LockAspectRatio = False

1 个答案:

答案 0 :(得分:4)

'Shapes.Count'≠Shape Index#!

.Count与当前形状.Index的数字上限不同。

通过列出文档中的所有形状,可以更容易地理解编号系统:

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
  

注意: 这篇文章的底部有 Excel的替代代码

为了演示,我们可以add shapes到一个新文档:

  • 首先,通过单击Insert(在功能区上)手动添加一个矩形
  • [如果使用Excel,请单击Illustrations],然后单击Shapes和矩形符号。 <code>▯</code>
  • 绘制形状,然后按 Ctrl + C 复制该形状,然后按 Ctrl + C 四个次粘贴4份。
  • 运行上面的过程,输出将是:

    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         

请注意,索引不是该对象的属性,而是根据Excel将形状存储在内存中的顺序进行计数(与For Each..Next语句返回的顺序相同。< / p>

  • 您可以通过运行以下内容来证明这一点:

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

    ...在这种情况下返回Rectangle 5

了解Excel如何存储形状的另一种方法是使用监视窗口。在循环的中间添加一个折线或Stop,然后突出显示ws.Shapes,右键单击它,选择Add Watch...,然后单击“确定”。浏览树以发现文档中形状的各种属性/属性。


  • 接下来,如果我们删除“中间矩形”并再次运行上述过程,我们将得到:

    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         

其余形状的IDName不变,但 Index 重新编号以反映新的“顺序”。 / p>

...因此,我们现在需要使用以下名称来返回名称Rectangle 5

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

引用形状(包括controls

当您使用数字引用形状时,例如.Shapes(),是指 索引编号 而不是ID数。 索引号根据需要动态分配,因此 不是稳定的方法 来引用形状。

  • 因此,.Count索引号的形状无关。

理想情况下,应使用.Name.ID数字来引用形状。如果要动态生成形状,则理想情况下,最好将形状列表存储在数组或集合中,以便可以根据需要查看该列表。


检索“最后创建的形状”

如果使用索引编号的唯一原因是要检索“最后创建的形状”,则可以使用如下函数来获取索引编号:

Function idxLastShape(slideNum As Long) As Long
    Dim sh As Shape
    For Each sh In ActivePresentation.Slides(slideNum).Shapes
        idxLastShape = idxLastShape + 1
    Next sh
End Function

用法示例:

Debug.Print idxLastShape(1) 'Returns index of last shape on slide#1
  

注意: 这篇文章的底部有 Excel的替代代码


或者,您可以让函数返回对实际 shape对象 的引用,而不是数字,如下所示:

Function LastShape(slideNum As Long) As Shape
    Dim sh As Shape
    For Each sh In ActivePresentation.Slides(slideNum).Shapes
        Set LastShape = sh
    Next sh
End Function

...因此,您可以通过以下方式获得“最后一个形状”的名称:

Debug.Print LastShape(1).Name

删除最近创建的形状

使用上述功能,您可以使用通常用于形状的任何方法。例如,您可以删除在幻灯片#1上创建的“最后一个形状”:

LastShape(1).Delete
  

Caution 注意!

     

帖子中的示例(包括删除示例!)不加区分地指出它们将返回/编辑/删除哪种形状!

     

从图形到声音/视频和controls的形状类型多达数十种。您可以使用.Type对象的Shape属性以及其他方法来过滤这些过程枚举的形状。部分列表here和下面的链接中有更多信息。


Excel的替代代码:

列出工作表(Excel)上的所有形状

Sub ListShapes()
    'hit CTRL+G to view output in Immediate Window
    Dim sh As Shape, ws As Worksheet, idx As Long
    Set ws = Sheets("Sheet1") '<-- change to your worksheet name
    For Each sh In ws.Shapes
        idx = idx + 1
        Debug.Print "Shape ID#" & sh.ID, "Index #" & idx, "Name: " & sh.Name
    Next sh
    Debug.Print "Count of shapes: " & Sheets("Sheet1").Shapes.Count
End Sub

“最后形状”(Excel)的返回索引号

Function idxLastShape(shtName As String) As Long
    Dim sh As Shape
    For Each sh In Sheets(shtName).Shapes
        idxLastShape = idxLastShape + 1
    Next sh
End Function

示例用法: Debug.Print idxLastShape("Sheet1")


返回对“最终形状”对象(Excel)的引用

Function LastShape(shtName As String) As Shape
    Dim sh As Shape
    For Each sh In Sheets(shtName).Shapes
        Set LastShape = sh
    Next sh
End Function

示例用法: Debug.Print LastShape("Sheet1").Name


更多信息:

从Excel复制到Powerpoint的其他方法:

相关问题