正在从 Powerpoint 关闭 Excel

Closing Excel from Powerpoint

我有以下代码,它打开 Excel sheet,执行我们的一些程序,然后关闭 sheet。

我无法关闭 sheet!

 Dim xlApp As Object
 Dim xlWorkBook As Object
 Dim path As String
 Dim osh As Shape
 Dim filename As String
 Set xlApp = CreateObject("Excel.Application")


 path = "path"
 filename = "Name.xlsx"
 xlApp.Visible = True
 Set xlWorkBook = xlApp.Workbooks.Open(path & filename)
 Set positionsheet = xlWorkBook.Sheets("Sheet1")
 Set PPApp = GetObject(, "Powerpoint.Application")
 Set PPPres = PPApp.ActivePresentation

 'does stuff here

 ActiveWindow.Visible = False

  With xlApp          ' I think the error is here!!!
    .xlWorkBook.Save
    .xlWorkBook.Close
 End With

出于某种原因 sheet 没有关闭!有任何想法吗??

试试这个:

xlworkbook.close

With-Block 抛出错误,因为 xlWorkBook 不是 xlApp 的参数。

这在我的测试中有效:

' ...
'does stuff here
'NoWith Block!
xlWorkBook.Save
xlWorkBook.Close
xlApp.Quit

例子

Option Explicit
Public Sub Example()
    Dim xlApp As Object
    Dim xlWorkBook As Object
    Dim Sht As Object
    Dim xlStarted As Boolean
    Dim Path As String
    Dim FileName As String

    Path = "C:\Temp\"
    FileName = "Book1.xlsx"

    Set xlApp = CreateObject("Excel.Application")
        xlStarted = True
        xlApp.Visible = True

    Set xlWorkBook = xlApp.Workbooks.Open(Path & FileName)
    Set Sht = xlWorkBook.Sheets("Sheet1")

    'does stuff here

    '// Close & SaveChanges
    xlWorkBook.Close SaveChanges:=True

    If xlStarted Then
        xlApp.Quit
    End If

End Sub