通过下面的功能,您可以确定文件是否正在被另一个进程使用。

如果您无法完全访问文件,该函数将返回True。

Function FileAlreadyOpen(FullFileName As String) As Boolean

' returns True if FullFileName is currently in use by another process

' example: If FileAlreadyOpen("C:\FolderName\FileName.xls") Then...

Dim f As Integer

f = FreeFile

On Error Resume Next

Open FullFileName For Binary Access Read Write Lock Read Write As #f

Close #f

' If an error occurs, the document is currently open.

If Err.Number <> 0 Then

FileAlreadyOpen = True

Err.Clear

'MsgBox "Error #" & Str(Err.Number) & " - " & Err.Description

Else

FileAlreadyOpen = False

End If

On Error GoTo 0

End Function