【问题标题】:Moving files from one folder to another将文件从一个文件夹移动到另一个文件夹
【发布时间】:2013-01-24 15:02:57
【问题描述】:

我需要使用 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 "

我收到一条错误消息。

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

【问题讨论】:

  • 那个错误信息是……?
  • 错误消息 ID“运行时错误 438。对象不支持此属性”
  • 对于初学者,我没有看到 fso 在该代码的任何地方声明。其次,为什么要创建ffiles的数组,然后只移动最后一个文件?
  • 上面我只给出了我将 fso 声明为对象的程序的一部分 Dim fso As Object Set fso = CreateObject("scripting.filesystemobject") 我需要复制文件并合并为一个。所以我将文件名作为数组中的 ffiles(m) 。这样我就可以复制许多文件并合并为一个。

标签: vba file move


【解决方案1】:

我最喜欢的做法。使用SHFileOperation API

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

【讨论】:

  • 非常感谢@sidhhart rout
【解决方案2】:

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多