在图表从 Excel 粘贴到 Powerpoint 之前程序继续
Procedure continues before chart has been pasted from Excel to Powerpoint
有时您需要在 Excel 中复制图表并使用 VBA 将其粘贴到 PowerPoint 演示文稿中。当您想要自动化报告时,这会派上用场。
问题:假设您没有将图形转换为图像,并且根据您尝试粘贴的图形的大小和复杂性,图形没有时间在 VBA 尝试继续该过程之前粘贴。当您想在粘贴图表后对其进行操作时(例如将其放置在幻灯片上),这很烦人。实际上发生的是您最终尝试在幻灯片上移动图表...尚未 'exist' 作为 PowerPoint 形状。
这个问题很烦人,在我看来实际上是一个错误。我还没有看到针对 SO 问题的可靠解决方案,John Peltier 的 可能是个例外。他的回答很可能是正确的(并且已在线程中指定为正确的)但不幸的是我无法在我的实现中使用它。
我在下面列出的问题。
代码(为便于参考做了简化):
Public Sub X2P()
'Copy chart and paste
ActiveChart.ChartArea.Copy
Set mySlide = myPresentation.Slides(2)
PasteChartIntoSlide mySlide
End Sub
Function PasteChartIntoSlide(theSlide As Object) As Object
CreateObject("PowerPoint.Application").CommandBars.ExecuteMso ("PasteSourceFormatting")
End Function
我看到其他人试图分解脚本并使用单独的函数复制、粘贴和定位图表。这个比较干净,但对我没用。
我找到的解决方案是在代码粘贴图表之前计算幻灯片上的形状数,num_obj
,并将变量 num_obj_final
设置为 num_obj + 1
(即粘贴图表后的形状总数)。然后我在粘贴事件之后创建一个循环,我在每次迭代中重新计算 num_obj
。只有当 num_obj
等于 num_obj_final
时,程序才会退出循环。然后脚本可以按预期继续,因为您可以确定幻灯片上现在的形状 'exists'。
PasteChartIntoSlide() 函数的最终代码:
Function PasteChartIntoSlide(theSlide As Object) As Object
theSlide.Select
num_obj = 0
num_obj_final = theSlide.Shapes.Count + 1
CreateObject("PowerPoint.Application").CommandBars.ExecuteMso ("PasteSourceFormatting")
DoEvents
Do Until num_obj = num_obj_final
num_obj = theSlide.Shapes.Count
'Debug.Print num_obj
Loop
End Function
有时您需要在 Excel 中复制图表并使用 VBA 将其粘贴到 PowerPoint 演示文稿中。当您想要自动化报告时,这会派上用场。
问题:假设您没有将图形转换为图像,并且根据您尝试粘贴的图形的大小和复杂性,图形没有时间在 VBA 尝试继续该过程之前粘贴。当您想在粘贴图表后对其进行操作时(例如将其放置在幻灯片上),这很烦人。实际上发生的是您最终尝试在幻灯片上移动图表...尚未 'exist' 作为 PowerPoint 形状。
这个问题很烦人,在我看来实际上是一个错误。我还没有看到针对 SO 问题的可靠解决方案,John Peltier 的
我在下面列出的问题。
代码(为便于参考做了简化):
Public Sub X2P()
'Copy chart and paste
ActiveChart.ChartArea.Copy
Set mySlide = myPresentation.Slides(2)
PasteChartIntoSlide mySlide
End Sub
Function PasteChartIntoSlide(theSlide As Object) As Object
CreateObject("PowerPoint.Application").CommandBars.ExecuteMso ("PasteSourceFormatting")
End Function
我看到其他人试图分解脚本并使用单独的函数复制、粘贴和定位图表。这个比较干净,但对我没用。
我找到的解决方案是在代码粘贴图表之前计算幻灯片上的形状数,num_obj
,并将变量 num_obj_final
设置为 num_obj + 1
(即粘贴图表后的形状总数)。然后我在粘贴事件之后创建一个循环,我在每次迭代中重新计算 num_obj
。只有当 num_obj
等于 num_obj_final
时,程序才会退出循环。然后脚本可以按预期继续,因为您可以确定幻灯片上现在的形状 'exists'。
PasteChartIntoSlide() 函数的最终代码:
Function PasteChartIntoSlide(theSlide As Object) As Object
theSlide.Select
num_obj = 0
num_obj_final = theSlide.Shapes.Count + 1
CreateObject("PowerPoint.Application").CommandBars.ExecuteMso ("PasteSourceFormatting")
DoEvents
Do Until num_obj = num_obj_final
num_obj = theSlide.Shapes.Count
'Debug.Print num_obj
Loop
End Function