使用来自其他VBA应用程序的Application.FileDialog

时间:2017-06-30 20:56:02

标签: vba office365

我正在使用名为AlphaCAM的CAM软件,它将Visual Basic集成到其软件中。我试图在用户窗体中运行一个按钮,利用文件对话框对象返回文件夹路径。但是,它无法识别FileDialog对象,我相信这是因为我在办公室应用程序之外工作。这是我的代码:

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更改为&#34; Dim fldr As Object&#34;但是代码仍然会在&#34; Application.FileDialog&#34;上出现。

这周围有环洞吗?我可以打开一个Excel窗口来运行文件对话框吗?

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

我认为你的意图是这样的:

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 Sub

答案 1 :(得分:0)

“我可以打开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