Excel 当我尝试运行下面的宏时,Active X error 429无法创建对象
因此,我尝试创建一个宏,它将从excel工作表(在本例中为工作表“区域”)复制数据,然后将粘贴复制到现有powerpoint模板幻灯片4中 请注意,powerpoint和excel文件都保存在dropbox文件夹中。(如果这改变了什么) 我不是VBA方面的专家,因此无法理解为什么它会向我显示此错误 代码如下:Excel 当我尝试运行下面的宏时,Active X error 429无法创建对象,excel,vba,powerpoint,activexobject,Excel,Vba,Powerpoint,Activexobject,因此,我尝试创建一个宏,它将从excel工作表(在本例中为工作表“区域”)复制数据,然后将粘贴复制到现有powerpoint模板幻灯片4中 请注意,powerpoint和excel文件都保存在dropbox文件夹中。(如果这改变了什么) 我不是VBA方面的专家,因此无法理解为什么它会向我显示此错误 代码如下: Sub excelrangetopowerpoint() Dim rng As Range Dim powerpointapp As Object Dim mypresentation
Sub excelrangetopowerpoint()
Dim rng As Range
Dim powerpointapp As Object
Dim mypresentation As Object
Dim destinationPPT As String
Dim myshape As Object
Dim myslide As Object
Set rng = Worksheets("regions").Range("B1:N18")
On Error Resume Next
Set powerpointapp = CreateObject("powerpoint.application")
detinationppt = ("C:\Users\OLX-Admin\Dropbox (Corporate Finance)\Naspers Monthly Reporting\Prep for call\From teams\FY2019\OLX Group Monthly Report_Sep'18_Macro.pptx")
PowerPoint.Presentations.Open (destinationPPT)
On Error GoTo 0
Application.ScreenUpdating = False
Set mypresentation = PowerPoint.ActivePresentation
Set myslide = mypresentation.Slides(4)
rng.Copy
myslide.Shapes.PasteSpecial DataType:=2 '2 = enhanced metafile
Set myshape = myslide.Shapes(myslide.Shapes.Count)
myshape.Left = 152
myshape.Top = 152
powerpointapp.Visible = True
powerpointapp.Activate
Application.CutCopyMode = False
End Sub
以下是运行正常的代码:
Sub ExcelRangeToPowerPoint()
Dim rng As Range
Dim PowerPointApp As Object
Dim myPresentation As Object
Dim mySlide As Object
Dim myShape As Object
'Range to copy
Set rng = Worksheets("regions").Range("B1:N18")
On Error Resume Next
Set PowerPointApp = GetObject(class:="PowerPoint.Application")
Err.Clear
'If PowerPoint is not already open then open PowerPoint
If PowerPointApp Is Nothing Then Set PowerPointApp = CreateObject(class:="PowerPoint.Application")
'Handle if the PowerPoint Application is not found
If Err.Number = 429 Then
MsgBox "PowerPoint could not be found, aborting."
Exit Sub
End If
On Error GoTo 0
Application.ScreenUpdating = False
'To create new presentation
Set myPresentation = PowerPointApp.Presentations.Add
'to add new slide to the Presentation
Set mySlide = myPresentation.Slides.Add(1, 11) '11 = ppLayoutTitleOnly
rng.Copy
'Paste to PowerPoint and position
mySlide.Shapes.PasteSpecial DataType:=2 '2 = ppPasteEnhancedMetafile
Set myShape = mySlide.Shapes(mySlide.Shapes.Count)
myShape.Left = 152
myShape.Top = 152
PowerPointApp.Visible = True
PowerPointApp.Activate
Application.CutCopyMode = False
End Sub
代码中有两个未定义的变量
detinationppt
而不是destinationppt
您将PowerPoint应用程序对象分配给
powerpointapp
,但两行之后您将访问一个(未定义的)对象PowerPoint
通过将Option Explicit
放在代码顶部,可以轻松避免此类错误
接下来,您可以分配打开的演示文稿,而不是访问ActivePresentation
。我做了一个测试,对我来说,访问ActivePresentation
失败了
另外,除非您确切知道自己在做什么,否则请不要在下一步的错误恢复中添加。如果您希望避免由于Powerpoint无法启动而导致运行时错误,则必须自己处理错误案例(正如您的“运行良好的代码”所做的那样)。首先,只需将其移除
这段代码对我来说很有用(当然使用了不同的文件名)
首先,将这一行添加到模块顶部,作为整个模块的第一个文本:
Option Explicit
然后,在菜单栏中单击“调试”和“编译VBA项目”
您将收到一系列错误消息,如下所示:
编译错误:
变量未定义
VBA将为您选择尚未定义的变量。其中大多数似乎都是打字错误,例如
detinationppt=(“C:
而不是destinationPPT=(“C:
PowerPointApp.Presentations.Open(destinationPPT)
而不是PowerPointApp.Presentations.Open(destinationPPT)
Set mypresentation=PowerPoint.ActivePresentation
而不是Set mypresentation=PowerPointApp.ActivePresentation
基本上,看起来您复制并粘贴了两个不同的代码块,但忘记检查变量名是否匹配(还有一个似乎匹配)
如果您转到“工具”>“选项…”>“编辑器”,则会出现一个勾选框,“需要变量声明”。打开此选项,保持打开状态,并定期使用“编译VBA项目”选项检查键入错误和类似错误。您在指定Powerpoint文件名时出现键入错误(detinationppt
而不是destinationppt
)。始终使用Option Explicit以避免此类错误。谢谢,但我仍然会遇到相同的错误。“activeX无法创建对象”。我应该更改我的代码吗?我有一个代码工作正常,它正在从excel复制数据区域并将其粘贴到新的powerpoint文件中。我需要帮助将其更改为将其复制粘贴到现有的powerpoint文件中,而不是创建新的powerpoint文件。我希望将此区域粘贴到现有的ppt幻灯片4中。以下是代码:(如果有帮助),这工作正常,但这会创建一个新的excel工作表,我想将其创建到现有工作表中。请参见下面的答案:请不要将问题更改为答案,而是编辑原始问题
Option Explicit