CodeItBetter Programming Another VB Programming Blog

How to open a folder in explorer in particular folder view

Posted on January 15, 2012
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
'System & API - How to open a folder in explorer in particular folder view
Option Explicit
 
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, _
    ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, _
    ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, _
    ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, _
    ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, _
    ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
 
Private Enum FolderView
    viewDEFAULT = 0
    viewICON = &H7029
    viewLIST = &H702B
    viewREPORT = &H702C
    viewTHUMBNAIL = &H702D
    viewTILE = &H702E
End Enum
 
Private Enum ViewType
    viewFolder = 0
    viewExplorer = 1
End Enum
 
Private Const SW_SHOWNORMAL As Long = 1
Private Const WM_COMMAND = &H111
 
Sub Main()
    OpenFolder "C:\", viewREPORT, viewExplorer
End Sub
 
Private Sub OpenFolder(ByVal sFolderPath As String, Optional ByVal eView As FolderView = _
    viewDEFAULT, Optional ByVal eViewType As ViewType = viewFolder)
    Dim N As Long, lhWnd As Long, lPrevhWnd As Long
    If Len(Dir(sFolderPath, vbDirectory)) = 0 Then Exit Sub
 
    If eViewType = viewFolder Then
        lPrevhWnd = FindWindow("CabinetWClass", vbNullString)
        ShellExecute 0&, "Open", sFolderPath, vbNullString, vbNullString, SW_SHOWNORMAL
    Else
        lPrevhWnd = FindWindow("ExploreWClass", vbNullString)
        Shell "Explorer.exe /e, C:\"
    End If
 
    If eView Then
        Do
            DoEvents: N = N + 1
            If eViewType = viewFolder Then
                lhWnd = FindWindow("CabinetWClass", vbNullString)
            Else
                lhWnd = FindWindow("ExploreWClass", vbNullString)
            End If
        Loop Until Not (lPrevhWnd = lhWnd Or lhWnd = 0) Or N = 100000
 
        If N = 100000 Or lhWnd = 0 Then Exit Sub
        Call Sleep(100)
 
        lhWnd = FindWindowEx(lhWnd, 0&, "SHELLDLL_DefView", vbNullString)
        SendMessage lhWnd, WM_COMMAND, ByVal eView, 0&
    End If
End Sub
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


 

No trackbacks yet.