4'ry sposoby kopiowania plików   strona główna:
A po co ten Excel ;-)
 
    Kiedyś miałem taki okres w którym wypisywałem wszystkie znane mi sposoby robienia czagoś. Tak powstała ta procedura :-)  
Jest tu więc kopiowanie plików przy pomocy:  
   - funkcji VBA.FileCopy  
   - metody CopyFile obiektu Scripting.FilesystemObject  
   - metodę Run obiektu Wscript.Shell  
   - API wykorzystując funkcję SHFileOperation  
 
 
Option Explicit  
 
Private Type SHFILEOPSTRUCT  
        hwnd As Long  
        wFunc As Long  
        pFrom As String  
        pTo As String  
        fFlags As Integer  
        fAnyOperationsAborted As Long  
        hNameMappings As Long  
        lpszProgressTitle As String  
End Type  
 
Private Declare Function SHFileOperation _  
    Lib "shell32.dll" _  
    Alias "SHFileOperationA" ( _  
        lpFileOp As SHFILEOPSTRUCT) _  
    As Long  
 
Private Const FOF_ALLOWUNDO = &H40  
Private Const FOF_NOCONFIRMATION = &H10  
Private Const FO_COPY = &H2  
 
Sub KopiowaniePlikow()  
      
    Const strXLSFilePath As String = "C:\Documents and Settings\jr\Pulpit\test.xls"  
    Const strDestination As String = "C:\Documents and Settings\jr\Pulpit\kopie\kopia.xls"  
    Const intSposob As Integer = 4  
 
    Select Case intSposob  
        Case 1 '------- sposób.1  VBA.FileCopy --------  
            VBA.FileCopy Source:=strXLSFilePath, _  
                         Destination:=strDestination  
          
        Case 2 '------- sposób.2  objFSO.CopyFile --------  
            Dim objFSO As Object 'Scripting.FileSystemObject  
            Set objFSO = CreateObject("Scripting.FileSystemObject")  
 
            objFSO.CopyFile Source:=strXLSFilePath, _  
                            Destination:=strDestination, _  
                            OverWriteFiles:=True  
 
            Set objFSO = Nothing  
              
        Case 3 '------- sposób.3  cmd copy --------  
            Dim objShell As Object  
            Set objShell = CreateObject("Wscript.Shell")  
              
            objShell.Run "cmd /c copy " & _  
                            Chr(34) & strXLSFilePath & Chr(34) & " " & _  
                            Chr(34) & strDestination & Chr(34) & _  
                            " /Y", _  
                         0, _  
                         False  
 
            Set objShell = Nothing  
              
        Case 4 '------- sposób.3  Shell API --------  
 
            ShellFileCopy src:=strXLSFilePath, _  
                          dest:=strDestination  
 
    End Select  
 
End Sub  
 
Public Function ShellFileCopy(src As String, _  
                              dest As String, _  
                              Optional NoConfirm As Boolean = False) As Boolean  
 
        'Copy Files Using the Shell API  
        'http://www.freevbcode.com/ShowCode.Asp?ID=499  
          
    Dim WinType_SFO As SHFILEOPSTRUCT  
    Dim lRet As Long  
    Dim lflags As Long  
      
    lflags = FOF_ALLOWUNDO  
    If NoConfirm Then lflags = lflags & FOF_NOCONFIRMATION  
    With WinType_SFO  
        .wFunc = FO_COPY  
        .pFrom = src  
        .pTo = dest  
        .fFlags = lflags  
    End With  
      
    lRet = SHFileOperation(WinType_SFO)  
    ShellFileCopy = (lRet = 0)  
 
End Function