首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将文件从一个文件夹移动到另一个文件夹

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

Stack Overflow用户
提问于 2013-01-24 23:02:57
回答 2查看 9.5K关注 0票数 0

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

代码语言:javascript
复制
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 "

我收到一条错误消息。

代码语言:javascript
复制
Error message id "Runtime error 438 . Object doesnt support this property "
EN

Stack Overflow用户

回答已采纳

发布于 2013-01-24 23:24:04

我最喜欢的方式。使用SHFileOperation应用编程接口

代码语言:javascript
复制
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
票数 1
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14504372

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档