Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
File 如何在power point vba中检查文件是否打开然后关闭?_File_Vba_Powerpoint - Fatal编程技术网

File 如何在power point vba中检查文件是否打开然后关闭?

File 如何在power point vba中检查文件是否打开然后关闭?,file,vba,powerpoint,File,Vba,Powerpoint,我正在PowerPoint中创建宏,以编程方式替换图片。 我正在尝试在PowerPoint宏的末尾用excel创建一个汇总表。 首先,我想检查summy文件是否存在。如果是,则代码应检查其是否打开。 如果它是打开的,代码应该关闭它。最后,代码应该删除它。 到目前为止,我已经成功地检查了文件并删除了文件。 但是如果文件是打开的,关闭它似乎有点困难。 任何帮助都将不胜感激。 先谢谢你 下面是我代码的一部分 'Get the active presentation name pressName = A

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

下面是我代码的一部分

'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
“获取活动的演示文稿名称
按Name=ActivePresentation.Name
Pos=InStrRev(按名称“.”)
位置=位置-1
按名称=左(按名称,位置)
excelFileName=按Name&“\u summy.xlsx”
'从活动演示文稿创建路径
excelFilePath=ActivePresentation.Path&“\”excelFileName
'检查excel是否存在
如果目录(excelFilePath)“,则
''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''此处的代码用于检查文件是否打开如果是,则关闭''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''
SetAttr excelFilePath,vbNormal
杀死excelFilePath
如果结束

我在您的
if语句中结合了可能的解决方案

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
如果Dir(excelFilePath)“,则
“---------新章节
出错时继续下一步
将xlsFile设置为对象
设置xlsFile=GetObject(excelFileName)
如果错误编号=432,则
'文件未打开,请不要执行任何操作
其他的
xlsFile.Close为False
'上面的设置为true表示关闭时需要保存文件
如果结束
错误转到0
“---------新章节
SetAttr excelFilePath,vbNormal
杀死excelFilePath
如果结束

这段代码适用于我的简单测试演示。我希望它也能对你有用。

@mehow,这是一个有点不同的问题。这里我们有PP Excel互操作,但在您的评论中提到的问题中没有类似的内容。@KazJaw什么?这与OP的要求完全相同for@mehow,在我看来,这不是同一个问题,因为这个问题的重要部分是应用程序互操作。但幸运的是,现在我可以承认,最后一个答案可能是下面的另一个答案。哇。。它起作用了。。非常感谢你。。