【发布时间】:2018-10-15 13:16:45
【问题描述】:
我正在尝试基于 Excel 文件和用户输入创建 PPT 生成器。到目前为止,我设法创建了用户表单,用户正在定义他想在演示文稿中看到的 Excel 报告(图表加表格)。为了定义选择了哪个报告,我使用了全局变量。现在,当我尝试生成演示文稿时,出现错误:“运行时错误'-2147023170(800706b3)':自动化错误。远程过程调用失败。”调试显示行newPowerPoint.ActivePresentation.Slides.Add newPowerPoint.ActivePresentation.Slides.Count + 1, ppLayoutTitleOnly
因为我使用函数 For 检查是否选择了报告(基于我的全局变量),所以我有多行这样的行,如果是,则为每个报告重复代码。
下面是代码本身。我不确定我做错了什么。
Sub CreatePowerPoint()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
'declare the variables
Dim newPowerPoint As PowerPoint.Application
Dim activeSlide As PowerPoint.Slide
Dim cht As Excel.ChartObject
Dim This As Workbook
Set This = ActiveWorkbook
'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
newPowerPoint.Presentations.Add
newPowerPoint.Visible = True
'TBA Starting Slides/Agenda
*Code here*
'Check if report was selected, if yes perform addition of new slides with graphs and tables
If CB1 = 1 Then
This.Worksheets("Coverage Summary").Select
For Each cht In ActiveSheet.ChartObjects
'Add a new slide
newPowerPoint.ActivePresentation.Slides.Add newPowerPoint.ActivePresentation.Slides.Count + 1, ppLayoutTitleOnly
newPowerPoint.ActiveWindow.View.GotoSlide newPowerPoint.ActivePresentation.Slides.Count
Set activeSlide = newPowerPoint.ActivePresentation.Slides(newPowerPoint.ActivePresentation.Slides.Count)
'Copy the chart and paste it into the PP
cht.Select
ActiveChart.ChartArea.Copy
activeSlide.Shapes.PasteSpecial(DataType:=ppPasteChartObject).Select
'Set the title of the slide
activeSlide.Shapes(1).TextFrame.TextRange.Text = "Coverage Summary"
'Adjust the positioning
newPowerPoint.ActiveWindow.Selection.ShapeRange.Left = 15
newPowerPoint.ActiveWindow.Selection.ShapeRange.Top = 125
Next
Set activeSlide = Nothing
End If
If CB2 = 1 Then
This.Worksheets("Additions Report").Select
For Each cht In ActiveSheet.ChartObjects
'Add a new slide
newPowerPoint.ActivePresentation.Slides.Add newPowerPoint.ActivePresentation.Slides.Count + 1, ppLayoutTitleOnly
newPowerPoint.ActiveWindow.View.GotoSlide newPowerPoint.ActivePresentation.Slides.Count
Set activeSlide = newPowerPoint.ActivePresentation.Slides(newPowerPoint.ActivePresentation.Slides.Count)
'Copy the chart and paste it into the PP
cht.Select
ActiveChart.ChartArea.Copy
activeSlide.Shapes.PasteSpecial(DataType:=ppPasteChartObject).Select
'Set the title of the slide
activeSlide.Shapes(1).TextFrame.TextRange.Text = "Additions summary"
'Adjust the positioning
newPowerPoint.ActiveWindow.Selection.ShapeRange.Left = 15
newPowerPoint.ActiveWindow.Selection.ShapeRange.Top = 125
Next
Set activeSlide = Nothing
End If
If CB3 = 1 Then
This.Worksheets("End of Coverage Report").Select
*Same code as above*
Set activeSlide = Nothing
End If
If CB4 = 1 Then
This.Worksheets("LDoS Summary").Select
*Same code as above*
End If
If CB5 ... * and so on
我的想法在这里用完了。我不知道如何更正代码。有人可以帮忙吗?
【问题讨论】:
-
好吧,从我测试的结果来看,问题出在
activeSlide.Shapes.PasteSpecial(DataType:=ppPasteChartObject).Select这行似乎当我使用DataType:=ppPasteMetafilePicture或ppPasteJPG时,一切都像一个魅力。但是将其更改为ppPasteChartObject或基本的ppPasteDefault会导致所有问题。不幸的是,它必须是 ChartObject。由于格式原因,JPG 和 Metafile 不是所需的选项 -
我的枚举列表中没有
ppPasteChartObject数据类型。见PpPasteDataType Enumeration。 -
我将其更改为
ppPasteDefault,但现在有一次它正在工作,有一次我在网上遇到错误activeSlide.Shapes.PasteSpecial(DataType:=ppPasteDefault).Select我现在迷路了
标签: excel vba powerpoint