在VBA

时间:2017-02-06 16:28:02

标签: vba excel-vba access-vba movefileex excel

我正在尝试让用户选择一个文件并选择上传到其他位置(如共享驱动器)。我正在使用名称功能但我意识到我无法获取文件名并放入“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

1 个答案:

答案 0 :(得分: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

的末尾