gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 01:36:02 28 4
gpt4 key购买 nike

我正在自动从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

最佳答案

'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和矩形符号。 <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         


请注意,Index不是此对象的属性,但它是按照Excel将形状存储在内存中的顺序进行计数的(与 For Each..Next语句返回的顺序相同)。


您可以运行以下命令证明这一点:

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不变,但索引已重新编号以反映新的“顺序”。

...因此返回名称 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



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



更多信息:


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
WiseOwl: 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)

关于excel - 粘贴的形状不被视为“最新”形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51169904/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com