This is a code to copy with API a files.
But i dont need the dialog to everwrite if exists the file with same name...possible to suppress the message?
Tks
Option Explicit
Const FO_COPY = &H2
Const FO_DELETE = &H3
Const FO_MOVE = &H1
Const FO_RENAME = &H4
Const FOF_ALLOWUNDO = &H40
Const FOF_SILENT = &H4
Const FOF_NOCONFIRMATION = &H10
Const FOF_RENAMEONCOLLISION = &H8
Const FOF_NOCONFIRMMKDIR = &H200
Const FOF_FILESONLY = &H80
Private Type SHFILEOPSTRUCT
hwnd As Long
wFunc As Long
pFrom As String
pTo As String
fFlags As Integer
fAborted As Boolean
hNameMaps As Long
sProgress As String
End Type
Private Declare Function SHFileOperation Lib "shell32.dll" _
Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
Private Sub cmdFileOp_Click(Index As Integer)
Dim lFileOp As Long
Dim lresult As Long
Dim lFlags As Long
Dim SHFileOp As SHFILEOPSTRUCT
Screen.MousePointer = vbHourglass
Select Case Index
Case 0
lFileOp = FO_COPY
Case 1
lFileOp = FO_MOVE
Case 2
lFileOp = FO_RENAME
Case 3
lFileOp = FO_DELETE
End Select
If chkSilent Then lFlags = lFlags Or FOF_SILENT
If chkYesToAll Then lFlags = lFlags Or FOF_NOCONFIRMATION
If chkRename Then lFlags = lFlags Or FOF_RENAMEONCOLLISION
If chkDir Then lFlags = lFlags Or FOF_NOCONFIRMMKDIR
If chkFilesOnly Then lFlags = lFlags Or FOF_FILESONLY
'
' NOTE: By adding the FOF_ALLOWUNDO flag you can move
' a file to the Recycle Bin instead of deleting it.
'
With SHFileOp
.wFunc = lFileOp
.pFrom = "C:\TEST\GI.MDB"
.pTo = "C:\DATABASE\GI.MDB"
.fFlags = lFlags
End With
lresult = SHFileOperation(SHFileOp)
'
' If User hit Cancel button while operation is in progress,
' the fAborted parameter will be true
'
Screen.MousePointer = vbDefault
If lresult <> 0 Or SHFileOp.fAborted Then Exit Sub
MsgBox ("COPIA COMPLETATA!"), vbInformation
End Sub
Private Sub cmdQuit_Click()
Unload Me
End Sub
The sample userform that you posted does not contain any code, so I don't understand what it has to do with the API function, nor how to specify the file(s) to be copied. You will have to make some effort to clarify what you're doing.
Does this do what you want? I have checked that the code overwrites the target file without asking, but I haven't tested with files large enough to display the progress dialog during the copy.
Copy.xlsm
You do not have the required permissions to view the files attached to this post.
HansV wrote:Does this do what you want? I have checked that the code overwrites the target file without asking, but I haven't tested with files large enough to display the progress dialog during the copy.
The API function should show the progress automatically, but only if the file you're copying is very large.
The API function does not provide any way to control a progress bar on your userform.