vba 将文件从一个文件夹移动到另一个文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14504372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 19:19:56  来源:igfitidea点击:

Moving files from one folder to another

vbafilemove

提问by PASUMPON V N

I need to move a file from one folder to another folder using VBA.

我需要使用 VBA 将文件从一个文件夹移动到另一个文件夹。

For m = 1 To fnum
    MsgBox " Please Select " & m & "files"
    ffiles(m) = Application.GetOpenFilename
Next m

If Dir(outputfolder) = "" Then
    fso.createfolder (outputfolder)
End If

fso.Movefile ffiles(m), outputfolder  " getting error at this place "

I am getting an error message.

我收到一条错误消息。

Error message id "Runtime error 438 . Object doesnt support this property "

回答by Siddharth Rout

My favorite way of doing it. Using the SHFileOperationAPI

我最喜欢的做法。使用SHFileOperationAPI

Option Explicit

Private Declare Function SHFileOperation Lib "shell32.dll" _
Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long

Private Const FO_MOVE As Long = &H1
Private Const FOF_SIMPLEPROGRESS As Long = &H100

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 Long
End Type

Sub Sample()
    Dim fileToOpen As Variant
    Dim outputfolder As String
    Dim i As Long

    outputfolder = "C:\Temp\"

    fileToOpen = Application.GetOpenFilename(MultiSelect:=True)

    If IsArray(fileToOpen) Then
        If Dir(outputfolder) = "" Then MkDir outputfolder

        For i = LBound(fileToOpen) To UBound(fileToOpen)
            Call VBCopyFolder(fileToOpen(i), outputfolder)
        Next i
    Else
          MsgBox "No files were selected."
    End If
End Sub

Private Sub VBCopyFolder(ByRef strSource, ByRef strTarget As String)
    Dim op As SHFILEOPSTRUCT
    With op
        .wFunc = FO_MOVE
        .pTo = strTarget
        .pFrom = strSource
        .fFlags = FOF_SIMPLEPROGRESS
    End With
    '~~> Perform operation
    SHFileOperation op
End Sub

回答by aravind balaji

We can move files from one folder to another automatically using script https://seleniumautomations.blogspot.com/2020/05/less-than-5-sec-to-clean-and-organise.html?view=magazine

我们可以使用脚本将文件从一个文件夹自动移动到另一个文件夹 https://seleniumautomations.blogspot.com/2020/05/less-than-5-sec-to-clean-and-organise.html?view=magazine