[英]Excel VBA to Create PowerPoint Presentation
Looking for some help on updating a VBA Script that completes the following (basic algorithm): 寻找有关完成以下内容(基本算法)的VBA脚本更新的帮助:
Note : This macro was actually created based on a feedback from this forum 注意 :此宏实际上是根据该论坛的反馈创建的
This macro works great in Windows 7 with Office 2013, but generates errors in Windows 10, Office 2016 after slide 8 is created, randomly during one of the paste chart actions, but never gets past slide 10 of a 17-slide deck. 此宏在带有Office 2013的Windows 7中可以很好地工作,但是在创建幻灯片8后,Windows 10,Office 2016在Windows 10,Office 2016中会生成错误,在粘贴图表操作之一期间是随机的,但是永远不会越过17幻灯片的幻灯片10。
Errors: 错误:
Runtime Error '-2147188160 (80048240)
Method 'PasteSpecial'of object 'Shapes' failed.
Or 要么
Runtime Error '-2147023170 (800706be)':
Automation Error
The Remote procedure call failed.
I'm not sure if this is an object issue or some other piece that I'm missing. 我不确定这是对象问题还是我遗失的其他部分。
Code below: 代码如下:
Sub CreatePowerPointPresentation()
'=========================================================================
'Create PowerPoint Presentation
'Assigned to Index Tab
'==========================================================================
Dim newPowerPoint As PowerPoint.Application
Dim activeSlide As PowerPoint.Slide
Dim CHT As Excel.ChartObject
Dim fmt As String
Dim hgt As String
Dim wth As String
‘this code allows for the user to select whether to paste the charts as Excel Charts or PNG Formatted images.
Sheets("Index").Select
If Range("AB7").Value = "Excel Charts" Then
fmt = ppPasteDefault
Else
fmt = ppPastePNG
End If
'Establishes the global height and width of the graphics or charts pasted from Excel
hgt = 280
wth = 710
'Look for existing instance
On Error Resume Next
Set newPowerPoint = GetObject(, "PowerPoint.Application")
On Error GoTo 0
'Create a new PowerPoint
If newPowerPoint Is Nothing Then
Set newPowerPoint = New PowerPoint.Application
End If
'Make a presentation in PowerPoint
If newPowerPoint.Presentations.Count = 0 Then
newPowerPoint.Presentations.Add
End If
'Show the PowerPoint
newPowerPoint.Visible = True
Application.EnableEvents = True
Application.ScreenUpdating = True
'Apply Template & Create Title Slide 1
newPowerPoint.ActivePresentation.ApplyTemplate Application.DefaultFilePath & "\file.potx"
'Set presentation to be 16x9
'AppActivate ("Microsoft PowerPoint")
With newPowerPoint.ActivePresentation.PageSetup
.SlideSize = ppSlideSizeOnScreen16x9
.FirstSlideNumber = 1
.SlideOrientation = msoOrientationHorizontal
.NotesOrientation = msoOrientationVertical
End With
'Create Slides 2-6 these are imported from the template
newPowerPoint.ActivePresentation.Slides.InsertFromFile Application.DefaultFilePath & "\File.potx", 0, 1
'Create Slide 7
newPowerPoint.ActivePresentation.Slides.Add newPowerPoint.ActivePresentation.Slides.Count + 1, ppLayoutBlank
newPowerPoint.ActivePresentation.Slides(7).CustomLayout = newPowerPoint.ActivePresentation.SlideMaster.CustomLayouts(33)
Set activeSlide = newPowerPoint.ActivePresentation.Slides(newPowerPoint.ActivePresentation.Slides.Count)
With newPowerPoint.ActivePresentation.Slides(7)
.Shapes("Title 1").TextFrame.TextRange.Text = "Title1"
End With
newPowerPoint.ActiveWindow.ViewType = ppViewSlide
‘Create Slide 8 – Quad Chart Slide
newPowerPoint.ActivePresentation.Slides.Add newPowerPoint.ActivePresentation.Slides.Count + 1, ppLayoutBlank
newPowerPoint.ActivePresentation.Slides(8).CustomLayout = newPowerPoint.ActivePresentation.SlideMaster.CustomLayouts(13)
newPowerPoint.ActiveWindow.View.GotoSlide newPowerPoint.ActivePresentation.Slides.Count
newPowerPoint.ActivePresentation.Slides(8).Shapes("Title 1").TextFrame.TextRange.Text = "Title 1"
Set activeSlide = newPowerPoint.ActivePresentation.Slides(newPowerPoint.ActivePresentation.Slides.Count)
newPowerPoint.ActiveWindow.ViewType = ppViewSlide
'Upper Left
Sheets("Charts").Select
ActiveSheet.ChartObjects("Chart 3").Select
ActiveChart.ChartArea.Copy
newPowerPoint.ActiveWindow.ViewType = ppViewSlide
activeSlide.Shapes.PasteSpecial(DataType:=fmt).Select
'Adjust the positioning of the Chart on Powerpoint Slide
newPowerPoint.ActiveWindow.Selection.ShapeRange.Left = 5
newPowerPoint.ActiveWindow.Selection.ShapeRange.Top = 75
newPowerPoint.ActiveWindow.Selection.ShapeRange.LockAspectRatio = msoFalse
newPowerPoint.ActiveWindow.Selection.ShapeRange.Height = 145
newPowerPoint.ActiveWindow.Selection.ShapeRange.Width = 345
'Upper Right
Sheets("Charts").Select
ActiveSheet.ChartObjects("Chart 2").Select
ActiveChart.ChartArea.Copy
newPowerPoint.ActiveWindow.ViewType = ppViewSlide
activeSlide.Shapes.PasteSpecial(DataType:=fmt).Select
newPowerPoint.ActiveWindow.Selection.ShapeRange.Left = 350
newPowerPoint.ActiveWindow.Selection.ShapeRange.Top = 75
newPowerPoint.ActiveWindow.Selection.ShapeRange.LockAspectRatio = msoFalse
newPowerPoint.ActiveWindow.Selection.ShapeRange.Height = 145
newPowerPoint.ActiveWindow.Selection.ShapeRange.Width = 345
'Lower Left
Sheets("Charts").Select
ActiveSheet.ChartObjects("Chart 4").Select
ActiveChart.ChartArea.Copy
newPowerPoint.ActiveWindow.ViewType = ppViewSlide
activeSlide.Shapes.PasteSpecial(DataType:=fmt).Select
newPowerPoint.ActiveWindow.Selection.ShapeRange.Left = 5
newPowerPoint.ActiveWindow.Selection.ShapeRange.Top = 230
newPowerPoint.ActiveWindow.Selection.ShapeRange.LockAspectRatio = msoFalse
newPowerPoint.ActiveWindow.Selection.ShapeRange.Height = 145
newPowerPoint.ActiveWindow.Selection.ShapeRange.Width = 690
‘More slides……
Application.EnableEvents = True
Application.ScreenUpdating = True
Set activeSlide = Nothing
Set newPowerPoint = Nothing
End Sub
This sounds like the dreaded code-runaway scenario I have faced in PowerPoint before where it takes more time to copy things to and paste things from the Windows clipboard than the VBA code execution and hence the VBA code runs ahead and fails as a result. 这听起来像是我在PowerPoint中遇到的可怕的代码失控场景,在此之前,要比Windows VBA代码执行花费更多的时间来复制和粘贴Windows剪贴板中的内容,因此VBA代码将继续运行并因此失败。 To confirm that this is the cause, put some break points on the .Copy, .ViewType and .PasteSpecial lines and see if it still fails for your full slide collection.
若要确认这是原因,请在.Copy,.ViewType和.PasteSpecial行上放置一些断点,然后查看完整幻灯片集合是否仍然失败。 If not, try adding some DoEvents lines after the .Copy and .ViewType lines and if that doesn't help, inject a Delay of one or two seconds instead of the DoEvents.
如果不是,请尝试在.Copy和.ViewType行之后添加一些DoEvents行,如果这样做没有帮助,请注入一秒或两秒的Delay而不是DoEvents。 That will at least confirm if the hypothesis is true or not.
这至少将证实该假设是否正确。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.