PowerPoint VBA在运行期间无法识别Excel表

我有一个代码,从我的Excel文件复制一个范围,并将其粘贴到我的ActiveSlide在PowerPoint中。 经过几个小时的尝试,从excel作为表(不是图像)粘贴范围,我发现下面的代码工作成功。 注意:myPP = powerpoint应用程序。

myPP.CommandBars.ExecuteMso "PasteExcelTableSourceFormatting" myPP.CommandBars.ReleaseFocus 

问题是,当我执行macros时,表被粘贴,但vba不识别表,除非代码是通过。 我已经用下面的代码testing过了。 在执行过程中,代码被完全跳过,但在执行过程中触发。

 For Each shp In activeSlide.Shapes If shp.HasTable Then MsgBox shp.Name End If Next 

以下是完整的代码。 基本上我只想把excel的范围作为桌子粘贴到我的简报中,并且把桌子扩大到适合幻灯片。 我愿意修改一下这个build议。 谢谢你的帮助

 Dim myPP as Object Dim activeSlide as Object Dim shp as Object Worksheets("Sheet2").Activate Worksheets("Sheet2").Range(Cells(1,1), Cells(4,7)).Copy Worksheets("Sheet1").Activate myPP.CommandBars.ExecuteMso "PasteExcelTableSourceFormatting" myPP.CommandBars.ReleaseFocus Dim myTable As String For Each shp In activeSlide.Shapes If shp.HasTable Then MsgBox shp.Name myTable = shp.Name End If Next With activeSlide.Shapes(myTable) .Left = 23 .Top = 105 .Width = 650 .Height = 375 End With 

对于ASH

 Dim myPP As Object 'Powerpoint.Application Dim myPres As Object 'Powerpoint.Presentation Dim activeSlide As Object 'Powerpoint.Slide Set myPP = CreateObject("Powerpoint.Application") myPP.Visible = True Set myPres = myPP.Presentations.Add myPP.ActiveWindow.ViewType = 1 'ppViewSlide Set activeSlide = myPres.slides.Add(1, 12) 'ppLayoutBlank 

问题来自于我们无法预测粘贴操作将持续多久,以及何时完成。 我们需要等待它的完成。

 ' first let us count the shapes in the slide Dim shapeCount As Integer: shapeCount = activeSlide.Shapes.Count myPP.CommandBars.ExecuteMso "PasteExcelTableSourceFormatting" Do '<~~ wait completion of paste operation DoEvents Loop Until activeSlide.Shapes.Count > shapeCount ' Now, our table is the last in the shapes collection. With activeSlide.Shapes(activeSlide.Shapes.Count) .Left = 23 .Top = 105 .Width = 650 .Height = 375 End With