打开Dialog默认为指定的网络路径(不是驱动器)

时间:2013-01-07 20:50:48

标签: excel vba

我正在尝试将打开的对话框指定为默认为网络上的特定文件夹。让我们使用路径:

\\test\yes\no\

以下代码不起作用,但也不会出错。我无法弄清楚我做错了什么。

Private Declare Function SetCurrentDirectory _
Lib "kernel32" _
Alias "SetCurrentDirectoryA" ( _
ByVal lpPathName As String) _
As Long

SetCurrentDirectory "\\test\yes\no\"

我已经看到人们这样做的几种方式,但似乎没有什么对我有用。如果有帮助的话,我正在使用excel 2010。

我的目录代码:

With Application.FileDialog(msoFileDialogFolderPicker)
    SetCurrentDirectory "\\test\yes\no\"
    .AllowMultiSelect = False
        If .Show <> -1 Then MsgBox "No folder selected! Exiting script.": Exit Sub
    myDir = .SelectedItems(1)
        End With

    MsgBox "Please choose the folder."
    Application.DisplayAlerts = False
        '*********************************************************************************************
    'Check for .xls cutsheets; open one at a time with a loop until all file data has been copied
    '*********************************************************************************************
    folderPath = myDir
        If Right(folderPath, 1) <> "\" Then folderPath = folderPath + "\"
    fileName = Dir(folderPath & "*.xls")
        Do While fileName <> ""
    Application.ScreenUpdating = False
        Set wbkCS = Workbooks.Open(folderPath & fileName)

2 个答案:

答案 0 :(得分:7)

我没有使用Application.FileDialogSetCurrentDirectory让它工作。但是,使用InitialFileName属性并将其设置为"\\test\yes\no\"(即仅指定路径)确实有效。

With Application.FileDialog(msoFileDialogFolderPicker)
    .InitialFileName = "\\test\yes\no\"
    .AllowMultiSelect = False
    If .Show <> -1 Then MsgBox "No folder selected! Exiting script.": Exit Sub
        myDir = .SelectedItems(1)
End With

答案 1 :(得分:1)

我可以使用以下内容打开一个对话框:

Private Declare Function SetCurrentDirectory _
Lib "kernel32" _
Alias "SetCurrentDirectoryA" ( _
ByVal lpPathName As String) _
As Long


Sub OpenDialogInNetworkPath()
    SetCurrentDirectory "\\test\yes\no\"
    FileToOpen = Application.GetOpenFilename _
    (Title:="Please choose a file to import", _
    FileFilter:="Excel Files *.xls (*.xls),")
    Workbooks.Open Filename:=FileToOpen
End Sub

这有帮助吗?你可以发布完整的宏吗?

相关问题