.Quit功能不起作用 - Excel VBA

时间:2018-04-24 08:23:55

标签: excel vba powerpoint

所以我运行这个代码(如下所示),它几乎操纵excel中的一些单元格,然后获取它们的图像并将其导出到powerpoint文件。然后它将保存该文件并关闭演示文稿,并希望是powerpoint应用程序。 'for'循环为我的excel文件(291列)中的每一列执行此操作,因此必须打开,操作,保存然后退出powerpoint。不幸的是,powerpoint没有退出,它仍然是开放的,因此在secnod循环中我得到自动化运行时错误(调用的对象已与其客户端断开连接)。我已经搜索了错误并发现它与powerpoint实例没有关闭有关,因此我的问题。请帮忙!

Sub Open_PowerPoint_Presentation()

Dim objPPT As Object
Dim PPTPrez As Object
Dim pSlide As Object
Dim myShape As Object
Dim fileNameString As String
Dim PicCount As Long
Dim i As Long
Dim fileN As String

Set objPPT = CreateObject("PowerPoint.Application")
objPPT.Visible = True

Application.ScreenUpdating = False
For i = 2 To 12
    Worksheets("Info").Cells(3, 2) = Worksheets("Temp").Cells(5, i)
    Worksheets("Info").Cells(4, 2) = Worksheets("Temp").Cells(6, i)
    Worksheets("Info").Cells(5, 2) = Worksheets("Temp").Cells(7, i)
    Worksheets("Info").Cells(6, 2) = Worksheets("Temp").Cells(8, i)
    Worksheets("Info").Cells(3, 3) = Worksheets("Temp").Cells(10, i)
    Worksheets("Info").Cells(4, 3) = Worksheets("Temp").Cells(11, i)
    Worksheets("Info").Cells(5, 3) = Worksheets("Temp").Cells(12, i)
    Worksheets("Info").Cells(6, 3) = Worksheets("Temp").Cells(13, i)

fileN = Worksheets("Temp").Cells(4, i)
Set PPTPrez = objPPT.Presentations.Open("C:\Site Reports\" & fileN & ".pptx")
Set rng = Worksheets("Info").Range("A2:C6")
Set pSlide = PPTPrez.Slides(6)

For PicCount = PPTPrez.Slides(6).Shapes.Count To 1 Step -1
    If PPTPrez.Slides(6).Shapes(PicCount).Type = msoPicture Then
        PPTPrez.Slides(6).Shapes(PicCount).Delete
    End If
Next

'Table
rng.Copy
pSlide.Shapes.PasteSpecial DataType:=2
Set myShape = pSlide.Shapes(pSlide.Shapes.Count)

'Set position:
    myShape.Left = 36
    myShape.Top = 175
    myShape.Height = 150.23
fileNameString = "C:\Site Reports\" & fileN & ".pptx"
PPTPrez.SaveAs fileNameString
PPTPrez.Close
objPPT.Quit

Next i
Application.ScreenUpdating = True

0 个答案:

没有答案