0

我试图让用户选择一个文件并选择一个上传到另一个位置(如共享驱动器)。我正在使用名称功能,但我意识到我无法获取文件名并将其放入“toPath”,因为它取决于用户。以下是我完成的代码,请任何建议或建议都会有所帮助。

同时,我希望我的代码可以帮助正在尝试做同样事情的人。谢谢

选择要上传的文件:

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
4

1 回答 1

1

重构结束Command10_Click如下所示。用户可以选择新的文件名。

....

End With

Dim ToName as String
ToName = InputBox("Please Enter New File Name","New File Name")

Name FromPath As ToPath & "\" &  ToName

....

我不确定您要重新定位哪些文件类型,但您可以从中获取扩展名类型FromPath并添加到ToName

于 2017-02-06T17:20:39.123 回答