VBA 方式到 select 个文件,然后是目标文件夹

VBA way to select files and then destination folder

我正在尝试 select 一堆文件,使用:

Set fd = Application.FileDialog(msoFileDialogFilePicker)

然后我必须处理每个 selected 文件(我允许多个 selection)并将其写回目标目录。 我的问题是当我使用:

Set destFolder = Application.FileDialog(msoFileDialogFolderPicker)

选择目标文件夹,fd 被覆盖,因为 Application 对象只能实例化单个 FileDialog 对象。然后我从 fd.

松开 SelectedItems 列表

我应该怎么做才能保留列表以供迭代?

保存fd返回的SelectedItems列表即可

Sub test()
    Dim fd As FileDialog, destFolder As FileDialog
    Dim s As FileDialogSelectedItems
    Set fd = Application.FileDialog(msoFileDialogFilePicker)
    fd.AllowMultiSelect = True
    fd.Show
    Set s = fd.SelectedItems ' s will be kept after next line
    Set destFolder = Application.FileDialog(msoFileDialogFolderPicker)
End Sub