ACC: FileCopy Statement May Not Copy Open FilesID: Q172711
|
Advanced: Requires expert coding, interoperability, and multiuser skills.
When you programmatically copy a file with the FileCopy statement in Visual
Basic for Applications, you may receive the following error message:
Run-time error '70'
Permission denied
The file is currently open, which prevents the FileCopy statement from copying the file.
Instead of using the FileCopy statement, use one of the following methods
to programmatically copy the file.
WARNING: The following functions enable you to copy an open file. If the
source file is changed while the copy operation is in process, the
destination file may be incomplete or may become corrupted.
Option Explicit
Declare Function apiCopyFile Lib "kernel32" Alias "CopyFileA" _
(ByVal lpExistingFileName As String, _
ByVal lpNewFileName As String, _
ByVal bFailIfExists As Long) As Long
Sub CopyFile(SourceFile As String, DestFile As String)
'---------------------------------------------------------------
' PURPOSE: Copy a file on disk from one location to another.
' ACCEPTS: The name of the source file and destination file.
' RETURNS: Nothing
'---------------------------------------------------------------
Dim Result As Long
If Dir(SourceFile) = "" Then
MsgBox Chr(34) & SourceFile & Chr(34) & _
" is not valid file name."
Else
Result = apiCopyFile(SourceFile, DestFile, False)
End If
End Sub
Option Explicit
Sub CopyFile(SourceFile As String, DestFile As String)
'---------------------------------------------------------------
' PURPOSE: Copy a file on disk from one location to another.
' ACCEPTS: The name of the source file and destination file.
' RETURNS: Nothing
'---------------------------------------------------------------
Dim CopyString As String
If Dir(SourceFile) = "" Then
MsgBox Chr(34) & SourceFile & Chr(34) & _
" is not a valid file name."
Else
SourceFile = Chr(34) & SourceFile & Chr(34)
DestFile = Chr(34) & DestFile & Chr(34)
CopyString = "COMMAND.COM /C COPY " & SourceFile & _
" " & DestFile
Call Shell(CopyString, 0)
End If
End Sub
If you are using Microsoft Windows NT, use the same procedure, but
change the line
CopyString = "COMMAND.COM /C COPY " & SourceFile & _
to:
CopyString = "CMD.EXE /C COPY " & SourceFile & _
This behavior is by design.
Option Explicit
Sub CopyFile(SourceFile As String, DestFile As String)
FileCopy SourceFile, DestFile
End Sub
For more information about the FileCopy statement, search the Help Index for "FileCopy statement."
Additional query words: file copy
Keywords : kbprg MdlGnrl
Version : WINDOWS:7.0,97
Platform : WINDOWS
Issue type : kbprb
Last Reviewed: April 19, 1999