粘贴的形状未显示为最新形状(&Q;) [英] Pasted Shape not seen as "Latest" Shape
问题描述
我正在从和Excel电子表格自动生成PowerPoint报表。在粘贴表格之前,我一直在执行该过程。
我正在使用PPApp.CommandBars.ExecuteMso ("PasteSourceFormatting")
将表格粘贴到PowerPoint,并且表格在幻灯片上显示为一个形状(第三个形状)。
为了引用我使用的Set pShape = Slide2.Shapes(Slide2.Shapes.Count)
新形状,现在当我粘贴时,pShape
被指定为"Shape 2"(而不是"Shape 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
推荐答案
‘Shapes.Count’≠形状索引#!
.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
和矩形符号。 - 绘制形状,然后单击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
语句返回的顺序相同。
您可以通过运行以下命令来证明这一点:
Debug.Print ActivePresentation.Slides(1).Shapes(5).Name
.,在本例中返回
/li>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
其余形状的ID
和Name
不会更改,但索引重新编号以反映新的"顺序"。
.因此,要返回名称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的替代代码!
或者,您也可以让函数返回对实际形状对象的引用,而不是返回数字,如下所示:
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
注意!
帖子中的示例(包括删除示例!)不分青红皂白地返回/编辑/删除哪种形状!
有几十种形状类型,从图形到声音/视频和controls。您可以使用
Shape
对象的.Type
属性以及其他方法对这些过程枚举的形状进行过滤操作。下面的链接中有部分列表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")
返回对"Last Shape"对象(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
详细信息:
- msdn:
Shapes
Object (PowerPoint/VBA) - msdn:
Shapes
Object (Excel/VBA) - msdn:MsoShapeType Enumeration (Office)
- 堆栈溢出:Overview of working with Form Controls and ActiveX Controls
- msdn:Working with Shapes (Drawing Objects)
- Office.com:How to add Shapes
- BreezeTree:Programming Shapes (AutoShapes) with VBA
- 智慧猫头鹰:Working with Shapes (Tutorial)
从Excel复制到Powerpoint的其他方式:
- SpreadsheetGuru:Copy & Paste An Excel Range Into PowerPoint With VBA
- ExcelOffTheGrid:Controlling Powerpoint from Excel using VBA
- mvps.org:Paste Excel chart as pictures in PowerPoint (Paste Special)
这篇关于粘贴的形状未显示为最新形状(&Q;)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!