测试给定文件夹中的任何文件是否已打开

时间:2015-03-03 06:58:37

标签: excel-vba vba excel

使用Name语句重命名文件夹,我得到'运行时错误75'如果文件夹或其子文件夹中有任何文件打开。

如何测试一个或多个文件是否在给定文件夹中打开?

1 个答案:

答案 0 :(得分:0)

另一种方法是在错误发生时处理错误,例如:通过向用户解释正在进行的操作并重复名称更改尝试直到成功为止。

Dim errNo As Lon

Do
    On Error Resume Next   ' Turn off error reporting
    Name oldName As newName
    errNo = Err    ' Save the error number (if any)
    On Error GoTo 0  ' Turn error reporting back on

    'Handle the error
    Select Case errNo
    'Deal with the various cases:
    Case 0
        'No error. All done.
        Exit Do
    Case 75
        'File/path access
        'Explain what's going on and repeat
        MsgBox "Couldn't rename folder. Make sure all files in folder are closed."
    Case Else
        'Some other error occurred.
        'Deal with it here.
    End Select
Loop 

还可以为用户提供使用MsgBox("Explanatory message",vbOKCancel)完全取消的选项。