添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am trying to create a new PPT based on an Excel sheet. The macro is located in the Excel file.
I receive

Run time 429 error: ActiveX component can't create object.

I went through MicroSoft help. Someone also mentioned the reference library so I added the Microsoft Powerpoint Reference to my Library.

Do I need to add an ActiveX Reference as well?

Sub PPT()
Dim r As Range
Dim powerpointapp As Object
Dim mypresentation As Object
Dim myslide As Object
Dim myshape As Object
Dim Cell As Range
Dim Rng As Range
Dim wsDestination As Worksheet, wsSource As Worksheet
'set worksheets
With ThisWorkbook
    Set wsSource = .Worksheets(Sheets.Count)
    Set wsDestination = .Worksheets("Sheet 1")
End With
LastRow1 = wsDestination.Cells(Rows.Count, "A").End(xlUp).Row
LastColumn1 = wsDestination.Cells(4, wsDestination.Columns.Count).End(xlToLeft).Column
Range(wsDestination.Cells(LastRow1, 1), wsDestination.Cells(LastRow1, LastColumn1)).Select
'assigning range into variable
Set r = ThisWorkbook.Worksheets("Sheet 1").Range(Cells(4, 1), Cells(LastRow1, LastColumn1))
'if we already opened powerpoint
Set powerpointapp = GetObject(class:="PowerPoint.Application")
'if powerpoint is not opened
'this is the line that causes the error 
If powerpointapp Is Nothing Then Set powerpointapp = CreateObject(class:="PowerPoint.Application")
'to create a new presentation
Set mypresentation = powerpointapp.Presentations.Add
Set myslide = mypresentation.slides.Add(1, 11)
r.Copy
'to paste range
myslide.Shapes.PasteSpecial DataType:=2
Set myshape = myslide.Shapes(myslide.Shapes.Count)
myshape.Left = 250
myshape.Top = 150
powerpointapp.Visible = True
powerpointapp.Activate
'to clear the cutcopymode from clipboard
Application.CutCopyMode = False
End Sub

You need a comma when using GetObject. You may want to read up on GetObject and CreateObject behavior of Office automation servers. There will also see how to handle the trappable Run-time error '429':

For example

Set powerpointapp = GetObject(,"PowerPoint.Application")`

Also you need to handle the error if no existing powerpoint instance is found. For example

'~~> Establish an PowerPoint application object
On Error Resume Next
'~~> Check if there is already an existing instance
Set powerpointapp = GetObject(, "PowerPoint.Application")
If Err.Number <> 0 Then
    '~~> If not found then create one
    Set powerpointapp = CreateObject("PowerPoint.Application")
End If
Err.Clear
On Error GoTo 0
Set mypresentation = powerpointapp.Presentations.Add
'~~> Rest of your code

I tested your code,

For me the same error was thrown on Set powerpointapp = GetObject(class:="PowerPoint.Application")

That happend because there was no powerpoint app created, that's the expected behavior, To overcome this you can do:

On Error Resume Next
Set powerpointapp = GetObject(class:="PowerPoint.Application")
On Error GoTo 0

On your code you mention that the line with the error is this one:

If powerpointapp Is Nothing Then Set powerpointapp = CreateObject(class:="PowerPoint.Application")

Well this line seems fine, you can change it for:

If powerpointapp Is Nothing Then Set powerpointapp = New PowerPoint.Application

in this case I still reccomend changing the variable type Object to PowerPoint.Application you will get intellisense tips

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.