【问题标题】:Using Application.FileDialog from a different VBA application使用来自不同 VBA 应用程序的 Application.FileDialog
【发布时间】:2017-12-04 21:37:04
【问题描述】:

我正在使用名为 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 更改为“Dim fldr As Object”,但代码仍然会在“Application.FileDialog”上跳闸。

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

感谢您的帮助!

【问题讨论】:

    标签: vba office365


    【解决方案1】:

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

    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
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多