Excel 在VBA中重新定位动态文件
我试图让用户选择一个文件,然后选择一个文件上传到另一个位置(比如共享驱动器)。我正在使用name函数,但我意识到我在获取文件名并将其放入“toPath”时遇到了问题,因为这取决于用户。以下是我完整的代码,请提供任何建议或建议 同时,我希望我的代码可以帮助尝试做同样事情的人。谢谢 要选择要上载的文件,请执行以下操作:Excel 在VBA中重新定位动态文件,excel,ms-access,vba,movefileex,Excel,Ms Access,Vba,Movefileex,我试图让用户选择一个文件,然后选择一个文件上传到另一个位置(比如共享驱动器)。我正在使用name函数,但我意识到我在获取文件名并将其放入“toPath”时遇到了问题,因为这取决于用户。以下是我完整的代码,请提供任何建议或建议 同时,我希望我的代码可以帮助尝试做同样事情的人。谢谢 要选择要上载的文件,请执行以下操作: Private Sub Command2_Click() Dim fDialog As Variant ' Clear listbox contents. ' Me.
Private Sub Command2_Click()
Dim fDialog As Variant
' Clear listbox contents. '
Me.Path1.Value = ""
' Set up the File Dialog. '
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
With fDialog
' Allow user to make multiple selections in dialog box '
.AllowMultiSelect = False
' Set the title of the dialog box. '
.Title = "Please select one file"
' Clear out the current filters, and add our own.'
.Filters.Clear
.Filters.Add "All Files", "*.*"
' Show the dialog box. If the .Show method returns True, the '
' user picked at least one file. If the .Show method returns '
' False, the user clicked Cancel. '
If .Show = True Then
'add selected path to text box
Me.Path1.Value = .SelectedItems(1)
Else
MsgBox "No File Selected."
End If
End With
End Sub
要选择上载文件的上载路径,请执行以下操作:
Private Sub Command10_Click()
Dim FromPath As String
Dim ToPath As String
Dim fDialog2 As Variant
' Clear listbox contents. '
Me.Path2.Value = ""
FromPath = Me.Path1
ToPath = Me.Path2
' Set up the File Dialog. '
Set fDialog2 = Application.FileDialog(msoFileDialogFolderPicker)
With fDialog2
If .Show = True Then
'add selected path to text box
Me.Path2.Value = .SelectedItems(1)
Else
MsgBox "No file uploaded."
End If
End With
Name FromPath As ToPath & "\" & 'ummmmmmmmmmm I am stucked :(
MsgBox "You can find the files and subfolders from " & FromPath & " in " & ToPath
End Sub
重构
命令10的结尾,单击,如下所示。用户可以选择新的文件名
....
End With
Dim ToName as String
ToName = InputBox("Please Enter New File Name","New File Name")
Name FromPath As ToPath & "\" & ToName
....
我不确定您要重新定位的文件类型,但您可以从FromPath
中获取扩展名类型,并将其添加到ToName
的末尾。看起来Me.Path2.Value
应该包含您需要的值。是否希望用户选择移动文件的名称?或者您希望它与旧文件同名?谢谢。我也这么做了。但不是让用户输入它。我对文件名做了一些分析,这样它就可以自动读取并获取文件名。谢谢斯科特!