在VBA中,你可以使用Scripting.FileSystemObject对象来复制文件。以下是复制文件的基本步骤:
Sub CopyFile()
    Dim fs As Object
    Dim sourceFilePath As String
    Dim destinationFilePath As String
    
    ' 源文件路径
    sourceFilePath = "C:\Path\To\Your\Source\File.txt"
    
    ' 目标文件路径
    destinationFilePath = "C:\Path\To\Your\Destination\File.txt"
    
    ' 创建FileSystemObject
    Set fs = CreateObject("Scripting.FileSystemObject")
    
    ' 检查源文件是否存在
    If fs.FileExists(sourceFilePath) Then
        ' 复制文件
        fs.CopyFile sourceFilePath, destinationFilePath, True ' 第三个参数表示覆盖已存在的文件
        MsgBox "File copied successfully!"
    Else
        MsgBox "Source file does not exist!"
    End If
End Sub

在这个示例中,CopyFile方法用于复制文件。第一个参数是源文件的路径,第二个参数是目标文件的路径,第三个参数是一个布尔值,表示是否允许覆盖已存在的文件。设置为True表示允许覆盖。

确保在使用这个代码之前替换sourceFilePath和destinationFilePath为你实际的文件路径。同时,小心谨慎地处理文件覆盖操作,以防止误操作。




转载请注明出处:http://www.pingtaimeng.com/article/detail/6700/VBA