2016-10-02 120 views
0

我想第一個幻燈片從PowerPoint中複製並在末尾插入,但我得到的ActiveX不能上線的ActiveX不能創建對象powerpont VBA

ActivePresentation.Slides(1).Copy 

創建對象這是我的完整的代碼和我已經添加以及

Option Explicit 

Dim myFile, Fileselected As String, Path As String, objPPT As Object 
Dim activeSlide As PowerPoint.Slide 

Sub Generate_PPTs() 

Application.ScreenUpdating = False 

Set myFile = Application.FileDialog(msoFileDialogOpen) 
With myFile 
    .Title = "Choose Template PPT File." 
    .AllowMultiSelect = False 
If .Show <> -1 Then 
    Exit Sub 
End If 
    Fileselected = .SelectedItems(1) 
End With 
Path = Fileselected 

Set objPPT = CreateObject("PowerPoint.Application") 
Set objPPT = objPPT.Presentations.Open(Path) 

Debug.Print objPPT.Name 

ActivePresentation.Slides(1).Copy 
ActivePresentation.Slides.Paste Index:=objPPT.Slides.Count + 1 

Set activeSlide = objPPT.Slides(objPPT.Slides.Count) 

Application.ScreenUpdating = True 
Set objPPT = Nothing 

End Sub 
+0

您是從PowerPoint還是Excel運行此代碼? –

+0

從Excel這就是爲什麼我添加對庫的引用 – newguy

回答

1

引用的Microsoft PowerPoint庫嘗試下面編輯的代碼,我有ppApp As PowerPoint.ApplicationDim ppPres As PowerPoint.Presentation

Option Explicit 

Dim myFile, Fileselected As String, Path As String, objPPT As Object 
Dim ppApp As PowerPoint.Application 
Dim ppPres As PowerPoint.Presentation 

Dim activeSlide As PowerPoint.Slide 

Sub Generate_PPTs() 

Application.ScreenUpdating = False 

Set myFile = Application.FileDialog(msoFileDialogOpen) 
With myFile 
    .Title = "Choose Template PPT File." 
    .AllowMultiSelect = False 
If .Show <> -1 Then 
    Exit Sub 
End If 
    Fileselected = .SelectedItems(1) 
End With 
Path = Fileselected 

Dim i As Integer 

Set ppApp = New PowerPoint.Application 
i = 1 

ppApp.Presentations.Open Filename:=Path ' 'PowerPointFile = "C:\Test.pptx" 
Set ppPres = ppApp.Presentations.Item(i) 

' for debug 
Debug.Print ppPres.Name 

ppPres.Slides(1).Copy 
ppPres.Slides.Paste Index:=ppPres.Slides.Count + 1 

Set activeSlide = ppPres.Slides(ppPres.Slides.Count) 

Application.ScreenUpdating = True 
Set ppPres = Nothing 
Set ppApp = Nothing 

End Sub 
+0

非常感謝你.. – newguy

相關問題