Results 1 to 6 of 6

Thread: Selecting Directory Dialog box

  1. #1

    Thread Starter
    Hyperactive Member billwagnon's Avatar
    Join Date
    Jul 1999
    Location
    St. Louis, Missouri, Mississippi Valley
    Posts
    290

    Question

    I know how to get the saveas filename, but how do you get the directory to save a file in?

    Right now my workaround consists of getting a saveas filename, then cycling throught the string to get rid of the file selected. This leaves the path only.

    The problem is that if there is not already a file in the directory, I can't get the saveas filename and therefore can't determine the directory.

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Code:
    Option Explicit
    
    Private Const MAX_PATH = 260
    
    Public Enum browseInfoFlags
      BIF_NONE = 0 ' No Flags
      BIF_RETURNONLYFSDIRS = &H1&      ' For finding a folder to start document searching
      BIF_DONTGOBELOWDOMAIN = &H2&     ' For starting the Find Computer
      BIF_STATUSTEXT = &H4&
      BIF_RETURNFSANCESTORS = &H8&
      BIF_EDITBOX = &H10&
      BIF_VALIDATE = &H20&              ' insist on valid result (or CANCEL)
    
      BIF_BROWSEFORCOMPUTER = &H1000&  ' Browsing for Computers.
      BIF_BROWSEFORPRINTER = &H2000&   ' Browsing for Printers
      BIF_BROWSEINCLUDEFILES = &H4000& ' Browsing for Everything
    End Enum
    
    Private Type BROWSEINFO
       hWndOwner As Long
       pidlRoot As Long
       pszDisplayName As String
       lpszTitle As String
       ulFlags As browseInfoFlags
       lpfn As Long
       lParam As Long
       iImage As Long
    End Type
    
    ' for SHGetPathFromIDList, pszString must be at least MAX_PATH (260) chars
    
    Private Declare Function SHBrowseForFolder Lib "shell32" Alias "SHBrowseForFolderA" (lpbi As BROWSEINFO) As Long
    Private Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidl As Long, ByVal pszPath As String) As Long
    Private Declare Sub CoTaskMemFree Lib "ole32" (ByVal ptr As Long)
    
    ' BrowseForFolder() returns a path if the user picks a good one, or "" if they don't.
    Public Function BrowseForFolder(ByVal hWndOwner As Long, Optional ByVal dlgTitle$ = "Please select a folder.", Optional ByVal flags As browseInfoFlags = BIF_RETURNONLYFSDIRS) As String
      Dim bif As BROWSEINFO, pidl As Long, buf$
      
      With bif
        .hWndOwner = hWndOwner
        .pidlRoot = 0          ' desktawp
        .pszDisplayName = Space$(MAX_PATH)
        .lpszTitle = dlgTitle$
        .ulFlags = flags
        .lpfn = 0&
        .lParam = 0&
        .iImage = 0&
      End With
      pidl = SHBrowseForFolder(bif)
      If (pidl = 0) Then Exit Function
      buf$ = Space$(MAX_PATH)
      If (SHGetPathFromIDList(pidl, buf$) = 0) Then
        buf$ = ""
      Else
        buf$ = Left$(buf$, InStr(1, buf$, vbNullChar) - 1)
      End If
      CoTaskMemFree pidl
      BrowseForFolder = buf$
    End Function
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  3. #3

    Thread Starter
    Hyperactive Member billwagnon's Avatar
    Join Date
    Jul 1999
    Location
    St. Louis, Missouri, Mississippi Valley
    Posts
    290
    Thank you for the code!

    VBA doesn't recognize "Enum" (neither do I!). Is there another way around this? Is there someplace I can find an explanation of the Enum concept?

    Thanks again for your help.

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Yes, there are two ways since it's just a way to make programming easier.

    1. Replace the enum values with constants
    2. Use the values directly

    For turning the hex values into dec just paste them into immediate, put a ? before it and press enter
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  5. #5

    Thread Starter
    Hyperactive Member billwagnon's Avatar
    Join Date
    Jul 1999
    Location
    St. Louis, Missouri, Mississippi Valley
    Posts
    290
    "Const" works. Now the code hangs on

    ulFlags As browseInfoFlags

    and says "User-defined type not defined"

    is "browsInfoFlags" a standard type in VB?

    Thanks again for all your help.

  6. #6
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Well if it says userdefined, then there's isn't any standard type matching it. Try search for it and declare it
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width