Excel VBA打开文件夹

19

使用2010年的Excel VBA - 我只是试图通过一个子程序打开一个文件夹。我在这里做错了什么?

VBA

Sub openFolder()  
  Dim preFolder As String, theFolder As String, fullPath as String

    theFolder = Left(Range("T12").Value, 8)
    preFolder = Left(Range("T12").Value, 5) & "xxx"
    fullPath = "P:\Engineering\031 Electronic Job Folders\" & preFolder & "\" & theFolder

    Shell(theFolder, "P:\Engineering\031 Electronic Job Folders\" & preFolder, vbNormalFocus)

End Sub

1
一旦你打开文件夹,你想对它做什么? - Bathsheba
Shell 只接受两个参数。正如所提到的,你正在做什么并不是很清楚。 - JosieP
我希望用户能够点击一个按钮,然后文件夹就会在屏幕上打开 - 没有其他的东西。 - Sanya
2个回答

52

如果您想打开Windows文件资源管理器,您应该调用explorer.exe

Call Shell("explorer.exe" & " " & "P:\Engineering", vbNormalFocus)

等价语法

Shell "explorer.exe" & " " & "P:\Engineering", vbNormalFocus

@d-stroyer,你接近了。这行代码看起来应该是 shell "explorer.exe /e,z:\MyPath\To\Folder" - SeanC
1
如果路径中可能包含空格,我认为您还需要将路径用引号括起来。 - Tim Williams
2
这可以通过 Shell("explorer.exe P:\Engineering", vbNormalFocus) 来简化。但是,如果有空格,就需要像Tim提到的那样用两个双引号进行双重引用:Shell("explorer.exe ""P:\Engineering\031 Electronic Job Folders""", vbNormalFocus) - Andy G

0

我使用这个来打开一个工作簿,然后将该工作簿的数据复制到模板中。

Private Sub CommandButton24_Click()
Set Template = ActiveWorkbook
 With Application.FileDialog(msoFileDialogOpen)
    .InitialFileName = "I:\Group - Finance" ' Yu can select any folder you want
    .Filters.Clear
    .Title = "Your Title"
    If Not .Show Then
        MsgBox "No file selected.": Exit Sub
    End If
    Workbooks.OpenText .SelectedItems(1)

以下是将文件复制到工作簿的新工作表中,并将这些值粘贴到第1个工作表中。
    Set myfile = ActiveWorkbook
    ActiveWorkbook.Sheets(1).Copy after:=ThisWorkbook.Sheets(1)
    myfile.Close
    Template.Activate
    ActiveSheet.Cells.Select
    Selection.Copy
    Sheets("Sheet1").Select
    Cells.Select
    ActiveSheet.Paste

End With

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接