Common Dialog - InitDir

Posted on 2005-04-20
Last Modified: 2013-12-25
How do you set the InitDir property of the common dialog to "My Computer"?

Question by:northeyk
    LVL 10

    Expert Comment

    Hi, northeyk.

    Am not finding this code anywhere online.  In case no one else finds it, you can open My Computer as follows:

         Set objShell = CreateObject("Shell.Application") objShell.NameSpace(&H11&)
    LVL 27

    Expert Comment

    Private Type OPENFILENAME 'Open & Save Dialog
        lStructSize As Long
        hwndOwner As Long
        hInstance As Long
        lpstrFilter As String
        lpstrCustomFilter As String
        nMaxCustFilter As Long
        nFilterIndex As Long
        lpstrFile As String
        nMaxFile As Long
        lpstrFileTitle As String
        nMaxFileTitle As Long
        lpstrInitialDir As String
        lpstrTitle As String
        flags As Long
        nFileOffset As Integer
        nFileExtension As Integer
        lpstrDefExt As String
        lCustData As Long
        lpfnHook As Long
        lpTemplateName As String
    End Type

    Private Const OFN_OVERWRITEPROMPT = &H2
    Private Const OFN_HIDEREADONLY = &H4
    Private Const OFN_HELPBUTTON = &H10
    Private Const OFN_ENABLEHOOK = &H20
    Private Const OFN_ENABLETEMPLATE = &H40
    Private Const OFN_PATHMUSTEXIST = &H800
    Private Const OFN_FILEMUSTEXISTS = &H1000
    Private Const OFN_EXPLORER = &H80000
    Private Const OFN_OPENFLAGS = &H81000
    Private Const OFN_SAVEFLAGS = &H80002

    Public Const MAX_PATH = 260
    Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
    Private Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As Long

    Public Function GetFileName(Optional ByVal sFileName As String, _
                                             Optional ByVal sInitDir As String, _
                                             Optional ByVal sFilter As String, _
                                             Optional ByVal sTitle As String, _
                                             Optional bOpen As Boolean = True) As String
       Dim ret As Long
       Dim sExt As String
       If sInitDir = "" Then sInitDir = App.Path
       With OFN
           .lStructSize = Len(OFN)
            For i = 1 To Len(sFilter)
                If Mid(sFilter, i, 1) = "|" Then
                   Mid(sFilter, i, 1) = vbNullChar
                End If
            sFilter = sFilter & String$(2, 0)
            .lpstrFilter = sFilter
            .lpstrTitle = sTitle
            .lpstrInitialDir = sInitDir
            .hInstance = App.hInstance
            .lpstrFile = sFileName & String(MAX_PATH - Len(sFileName), 0)
            .lpstrFileTitle = String(MAX_PATH, 0)
            .nMaxFile = MAX_PATH
       End With
       If bOpen Then
          OFN.flags = OFN.flags Or OFN_OPENFLAGS
          ret = GetOpenFileName(OFN)
          OFN.flags = OFN.flags Or OFN_SAVEFLAGS
          ret = GetSaveFileName(OFN)
       End If
       If ret Then GetFileName = TrimNull(OFN.lpstrFile)
    End Function

    Public Function TrimNull(startstr As String) As String
       Dim pos As Integer
       pos = InStr(startstr, Chr$(0))
       If pos Then
          TrimNull = Left$(startstr, pos - 1)
          Exit Function
       End If
       TrimNull = startstr
    End Function

    LVL 28

    Accepted Solution

    Private Sub Command1_Click()
    CommonDialog1.InitDir = "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}"
    End Sub
    LVL 10

    Expert Comment

    Sweet solution, vinnyd79:)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Suggested Solutions

    Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
    The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now