vba 打开对话框默认为指定的网络路径(不是驱动器)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14203801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 14:42:01  来源:igfitidea点击:

Open Dialog defaulting to specified network path (not drive)

excelvba

提问by Mike

I'm trying to assign my open dialog box to default to a specific folder on the network. Let's use the path:

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

\test\yes\no\

The code below doesn't work, but also doesn't error out. I can't figure out what I'm doing wrong.

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

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

SetCurrentDirectory "\test\yes\no\"

I've seen a few ways people are doing this, but nothing seems to be working for me. I'm using excel 2010 if that helps.

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

My directory code:

我的目录代码:

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)

回答by Kim Hansson

I do not either get it to work using Application.FileDialogand SetCurrentDirectory. However, using the InitialFileNameproperty and setting it to "\\test\yes\no\"(i.e. only specifying a path) does work.

我也没有使用Application.FileDialogand让它工作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

回答by StoriKnow

I'm able to open a dialog box using the following:

我可以使用以下命令打开一个对话框:

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

Does this help? Can you please post the full macro?

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