我遇到的问题是,当我尝试使用多个文件对话框实例时,第一个对话框中的选择始终会覆盖第一个信息。
我需要做的是:
第二次使用application.FileDialog会发生什么,fd中的所有信息都会丢失,并被fldr中的条目覆盖。
每个宏只能有一个对话框对象吗?
Dim fd As FileDialog
Dim FileChosen As Integer
Dim FileName As String
Dim fldr As FileDialog
Dim fldrSelect As String
Dim i As Integer
Set fd = Application.FileDialog(msoFileDialogFilePicker)
'use the standard title and filters, but change the
'initial folder
fd.InitialFileName = "H:\UpdatedSalesTemplates\"
fd.InitialView = msoFileDialogViewList
'allow multiple file selection
fd.AllowMultiSelect = True
FileChosen = fd.Show
If FileChosen = -1 Then
'Select the directory using a file dialog
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
fldr.InitialView = msoFileDialogViewList
fldr.Title = "Select Destination"
fldr.AllowMultiSelect = False
fldrSelected = fldr.Show
答案 0 :(得分:0)
Microsoft says可能只有一个:“......每个主机应用程序只能创建一个FileDialog对象的实例......”。
在任何情况下,只要您可以将所有相关信息(选定路径,初始目录等)存储在(字符串)变量中,这不应该是一个严重的问题。
答案 1 :(得分:0)
对于在一个宏/过程/用户表单中需要文件/文件夹选择器的情况,我使用自定义的用户表单。看看你是否喜欢它。放置命令按钮和文本框,如下所示
<强>截图强>
<强>代码强>
注意:文本框.Locked
属性在设计时设置为True
,因此用户无法手动修改文本框。
Option Explicit
Dim Ret
'~~> Browse File
Private Sub CommandButton1_Click()
Ret = Application.GetOpenFilename("Excel Files (*.xlsx), *.xlsx")
If Ret <> False Then TextBox1.Text = Ret
End Sub
'~~> Browse Folder
Private Sub CommandButton2_Click()
Ret = BrowseForFolder("C:\")
If Ret <> False Then TextBox2.Text = Ret
End Sub
Function BrowseForFolder(Optional OpenAt As Variant) As Variant
Dim ShellApp As Object
Set ShellApp = CreateObject("Shell.Application"). _
BrowseForFolder(0, "Please choose a folder", 0, OpenAt)
On Error Resume Next
BrowseForFolder = ShellApp.self.Path
On Error GoTo 0
Set ShellApp = Nothing
Select Case Mid(BrowseForFolder, 2, 1)
Case Is = ":"
If Left(BrowseForFolder, 1) = ":" Then GoTo Invalid
Case Is = "\"
If Not Left(BrowseForFolder, 1) = "\" Then GoTo Invalid
Case Else
GoTo Invalid
End Select
Exit Function
Invalid:
'~~> If it was determined that the selection was invalid, set to False
BrowseForFolder = False
End Function