i dont need the confirmation wiondowto over write

User avatar
sal21
PlatinumLounger
Posts: 4355
Joined: 26 Apr 2010, 17:36

i dont need the confirmation wiondowto over write

Post by sal21 »

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

Code: Select all

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

User avatar
HansV
Administrator
Posts: 78475
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: i dont need the confirmation wiondowto over write

Post by HansV »

Why do you want to use an API function for this? Why not use the VBA instruction FileCopy?
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4355
Joined: 26 Apr 2010, 17:36

Re: i dont need the confirmation wiondowto over write

Post by sal21 »

HansV wrote:Why do you want to use an API function for this? Why not use the VBA instruction FileCopy?

In effect i need to run progress bar do until copy finised...

note:
The file to be copied in c:\, is on lan dir.
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 78475
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: i dont need the confirmation wiondowto over write

Post by HansV »

How does the API function help with that? :scratch:
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4355
Joined: 26 Apr 2010, 17:36

Re: i dont need the confirmation wiondowto over write

Post by sal21 »

HansV wrote:How does the API function help with that? :scratch:
Instead API have your code to shwo the progessbar while copyng?
Tks

User avatar
HansV
Administrator
Posts: 78475
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: i dont need the confirmation wiondowto over write

Post by HansV »

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.
Best wishes,
Hans

User avatar
HansV
Administrator
Posts: 78475
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: i dont need the confirmation wiondowto over write

Post by HansV »

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.
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4355
Joined: 26 Apr 2010, 17:36

Re: i dont need the confirmation wiondowto over write

Post by sal21 »

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.
Copy.xlsm

NICE!
But progressbar?

User avatar
HansV
Administrator
Posts: 78475
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: i dont need the confirmation wiondowto over write

Post by HansV »

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.
Best wishes,
Hans