如何检查文件是否打开然后在电源点vba关闭?

时间:2013-08-13 13:30:08

标签: file vba powerpoint powerpoint-vba

我在PowerPoint中创建一个宏来以编程方式替换图片。 我试图在PowerPoint宏的末尾创建一个excel表。 首先,我想检查summery文件是否存在。如果是,则代码应检查它是否打开。 如果它是打开的,代码应该关闭它。最后代码应删除它。 到目前为止,我已成功检查文件并删除文件。 但如果文件打开则关闭文件似乎有点困难。 任何帮助将不胜感激。 提前谢谢。

以下是我的代码的一部分。

'Get the active presentation name
pressName = ActivePresentation.Name
Pos = InStrRev(pressName, ".")
Pos = Pos - 1
pressName = Left(pressName, Pos)
excelFileName = pressName & "_Summery.xlsx"
'Create path from active presentation
excelFilePath = ActivePresentation.Path & "\" & excelFileName

'Check if excel exists
If Dir(excelFilePath) <> "" Then
''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''Here code to check if file is open if yes then close''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''
SetAttr excelFilePath, vbNormal
Kill excelFilePath
End If

1 个答案:

答案 0 :(得分:1)

我在if statement

中合并了可能的解决方案
If Dir(excelFilePath) <> "" Then
'--------------new section
    On Error Resume Next
    Dim xlsFile As Object
    Set xlsFile = GetObject(excelFileName)

    If Err.Number = 432 Then
        'file is not open, do nothing
    Else
        xlsFile .Close False
        'set true above is you need to save file when closing
    End If
    On Error GoTo 0
'--------------new section    
SetAttr excelFilePath, vbNormal
Kill excelFilePath
End If

此代码适用于我的简单测试演示。我希望它也适合你。

相关问题