office交流網--QQ交流群號

Access培訓群:792054000         Excel免費交流群群:686050929          Outlook交流群:221378704    

Word交流群:218156588             PPT交流群:324131555

VBA使用FileSystemObject複製或移動文件

2017-08-08 16:50:00
zstmtony
原創
7496

以下VBA代碼 使用FileSystemObject複製或移動文件,適用於access excel等VBA環境



Public Sub Move_File()
    Dim strSource As Variant
    Dim strDestination As String
    Dim objFS As Object
    
    strSource = "C:\Temp_C\Test.txt"  '源路徑
    strDestination = "C:\Temp_Test\Test.txt"  '目標路徑
    Set objFS = CreateObject("Scripting.FileSystemObject")  '創建FileSystemObject fso對象
    With objFS
        If .FileExists(strDestination) Then
            .DeleteFile (strDestination)   ‘如果目標文件存在,先刪除目標文件
        End If
        .MoveFile strSource, strDestination   '移動文件
    End With
    Set objFS = Nothing
End Sub
分享