3

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

\\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)
4

2 回答 2

7

Application.FileDialog我也不能使用and让它工作SetCurrentDirectory。但是,使用该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
于 2013-01-07T22:45:44.333 回答
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

这有帮助吗?可以发一下完整的宏吗?

于 2013-01-07T21:45:20.770 回答