我最近开始尝试使用vba来自动执行Microsoft Word中的一些日常任务。我正在编写一个代码,允许用户选择目标文件夹以及要复制到所选目标文件夹的文件(.doc)。
以下代码运行时没有错误,但文件不会被复制粘贴到目标文件夹中。
我将非常感谢您解决这个问题的任何帮助。
问候,
德里克
max()
答案 0 :(得分:0)
有几个问题。
变量i未在任何地方声明。
您正在尝试在对话框返回之前保存文件夹路径。
Sub copydocs()
Dim i As Integer ' CHANGE: New declare.
Dim fs As Object ' CHANGE: Moved to top.
Dim items As Long
Dim file_path As Variant
Dim folder_path As Variant
'Ask user for input.
items = InputBox("Give me some input")
'Select Destination Folder
With Application.FileDialog(msoFileDialogFolderPicker)
' CHANGE: Switched order of next two lines.
.Show
folder_path = .SelectedItems(1)
End With
' Open the file dialog
For i = 1 To items
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = True
.Show
file_path = .SelectedItems(1)
End With
' Copy paste
Set fs = CreateObject("Scripting.FileSystemObject")
fs.CopyFile file_path, folder_path
Set fs = Nothing
Next i
End Sub