【VBA】Copy file

You can copy file!

PR

VBA Code

As an example, copy the file.

file
file
Option Explicit

Sub sampleProc()
    
    Dim fso As Object
    Dim fileFullPath As String
    Dim folderPath As String
    
    'Set "File path"
    fileFullPath = "C:\Users\user\Desktop\hogehoge.txt"
    
    'Set "Folder path"
    folderPath = "C:\Users\user\Desktop\folder_001\"
    
    Set fso = CreateObject("Scripting.FileSystemObject")

    'Copy file
    Call fso.CopyFile(Source:=fileFullPath, _
                      Destination:=folderPath, _
                      OverWriteFiles:=False)
    
    Set fso = Nothing
    
End Sub

Set “File path”(Line 10).

Set “Folder path”(Line 13).

Copy File(Line 18).

PR

Result

You copied file!

Result
Result
PR

FYI No.1

Also, you can copy Multiple files by Wildcard!

Option Explicit

Sub sampleProc()
    
    Dim fso As Object
    Dim fileFullPath As String
    Dim folderPath As String
    
    'Set "File path". Use "Wildcard"
    fileFullPath = "C:\Users\user\Desktop\*.txt"
    
    'Set "Folder path"
    folderPath = "C:\Users\user\Desktop\folder_001\"
    
    Set fso = CreateObject("Scripting.FileSystemObject")

    'Copy file
    Call fso.CopyFile(Source:=fileFullPath, _
                      Destination:=folderPath, _
                      OverWriteFiles:=False)
    
    Set fso = Nothing
    
End Sub

Use “Wildcard”(Line 10).

PR

FYI No.2

For more information about following, see the following official online manual:

CopyFile method” of “FileSystemObject”

タイトルとURLをコピーしました