我使用的是名为AlphaCAM的计算机辅助教学软件,它已将Visual Basic集成到其软件中。我试图在用户表单中运行一个按钮,利用文件对话框对象返回文件夹路径。但是,它无法识别FileDialog对象,我认为这是因为我在office应用程序之外工作。下面是我的代码:
Private Sub Command_FindFolder_Click()
Dim fldr As FileDialog
Dim foldername As String
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
.Title = "Select a Folder"
.AllowMultiSelect = False
.InitialFileName = Application.DefaultFilePath
If .Show <> -1 Then GoTo NextCode
foldername = .SelectedItems(1)
End With
NextCode:
Set fldr = Nothing
TB_FolderName.Value = foldername
End Sub我还尝试将变量fldr更改为"Dim fldr As Object“,但代码仍然在"Application.FileDialog”上出错。
这有没有什么漏洞?我可以打开一个excel窗口来运行文件对话框吗?
谢谢你的帮忙!
发布于 2017-07-01 05:52:45
“我可以打开一个excel窗口来运行文件对话框吗?”
是的,您可以:
Private Sub Command_FindFolder_Click()
Dim fldr, xlApp
Dim foldername
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = False
Set fldr = xlApp.FileDialog(4) 'msoFileDialogFolderPicker
With fldr
.Title = "Select a Folder"
.AllowMultiSelect = False
.InitialFileName = xlApp.DefaultFilePath
If .Show = -1 Then foldername = .SelectedItems(1)
End With
Set fldr = Nothing
xlApp.DisplayAlerts = False
xlApp.Quit
Set xlApp = Nothing
'TB_FolderName.Value = foldername
WScript.Echo foldername
End Sub发布于 2017-07-01 05:20:12
我认为你的意图是这样的:
Sub foo()
Dim shell: Set shell = CreateObject("Shell.Application")
Dim file: Set file = shell.BrowseForFolder(0, "Choose a file:", &H4000)
BrowseForFile = file.self.Pat
End Subhttps://stackoverflow.com/questions/44854709
复制相似问题