Folder Browser Needed for VBA 7 64 bit.

Folder Browser Needed for VBA 7 64 bit.

Anonymous
Not applicable
35,671 Views
23 Replies
Message 1 of 24

Folder Browser Needed for VBA 7 64 bit.

Anonymous
Not applicable

I need a folder browser for VBA 7 64 bit that I can set the initial Folder.

 

What are my options?

 

I cannot figure out how to convert my 32 bit Windows API code to 64 bit.

I have the declare and UDT written properly but not all the rest that does the work.

 

I tried the Window Shell Object, but could not get the initial directory to work.

 

Any Ideas?

0 Likes
Accepted solutions (1)
35,672 Views
23 Replies
Replies (23)
Message 21 of 24

Anonymous
Not applicable

This line is giving me an error:

If FolderExists(sInitFolder) Then bi.lpfnCallback = PtrToFunction(AddressOf BFFCallback)

It says "Invalid use of AddressOf operator". Why is this?

(I am using VBA for Solidworks 64bit)

0 Likes
Message 22 of 24

OceanaPolynom
Advocate
Advocate

This is really great. Thanks very much.

John

0 Likes
Message 23 of 24

NachoShaw
Advisor
Advisor

 

 

Here is a nice and simple one for you

 

Private Declare PtrSafe Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal Hwnd As Long, ByVal nIndex As Long, ByVal wNewLong As Long) As Long

Private Declare PtrSafe Function apiShellExecute 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

''Globale variable for DIR Browser
Private Type BrowseInfo
    hOwner     As Long
    pIDLRoot   As Long
    pszDisplayName As String
    lpszTitle  As String
    ulFlags    As Long
    lpfn       As Long
    lParam     As Long
    iImage     As Long
End Type

Private Const BIF_NEWDIALOGSTYLE = &H40
' show edit box
Private Const BIF_EDITBOX = &H10&
'Private Const BIF_BROWSEINCLUDEFILES = &H4000
'Private Const BIF_RETURNONLYFSDIRS = &H1
'Private Const MAX_PATH = 260

Private Const BIF_RETURNONLYFSDIRS As Long = &H1
Private Const BIF_DONTGOBELOWDOMAIN As Long = &H2
Private Const BIF_RETURNFSANCESTORS As Long = &H8
Private Const BIF_BROWSEFORCOMPUTER As Long = &H1000
Private Const BIF_BROWSEFORPRINTER As Long = &H2000
Private Const BIF_BROWSEINCLUDEFILES As Long = &H4000
Private Const MAX_PATH As Long = 260

'***App Window Constants***
Private Const WIN_NORMAL = 1         'Open Normal
Private Const WIN_MAX = 3            'Open Maximized
Private Const WIN_MIN = 2            'Open Minimized

'***Error Codes***
Private Const ERROR_SUCCESS = 32&
Private Const ERROR_NO_ASSOC = 31&
Private Const ERROR_OUT_OF_MEM = 0&
Private Const ERROR_FILE_NOT_FOUND = 2&
Private Const ERROR_PATH_NOT_FOUND = 3&
Private Const ERROR_BAD_FORMAT = 11&

Private Function BrowseFolder(Optional Caption As String, _
                      Optional InitialFolder As String) As String

    Dim sh     As Shell32.Shell
    Dim f      As Shell32.Folder

    Set sh = New Shell32.Shell
    Set f = sh.BrowseForFolder(0&, Caption, BIF_RETURNONLYFSDIRS, InitialFolder)
    If Not f Is Nothing Then
        BrowseFolder = f.items.Item.path
    End If

End Function



Function ShowFolderDialog() As String

 Dim InitFolder As String
 InitFolder = "C:/"

 Dim BrowserCaption As String
 BrowserCaption = "Browse for Output Folder"

 Dim FName  As String
    FName = BrowseFolder(Caption:=BrowserCaption, InitialFolder:=InitFolder)
    If FName = vbNullString Then
        Exit Sub
    Else
        ShowFolderDialog = FName & "\"
    End If
End Function

 

 

Giving a Browse Folder Dialog like this:

dialog.jpg

 

To use, you need something like this:

Me.MyTextbox.Value = ShowFolderDialog

You could also change the function slightly to pass the path you want to use dynamically so that you can reuse the same code for different places:

Function ShowFolderDialog(StartPath As String) As String

 Dim BrowserCaption As String
 BrowserCaption = "Browse for Output Folder"

 Dim FName  As String
    FName = BrowseFolder(Caption:=BrowserCaption, InitialFolder:=StartPath)
    If FName = vbNullString Then
        Exit Sub
    Else
        ShowFolderDialog = FName & "\"
    End If
End Function

You would then use it like this:

ShowFolderDialog("\\MyNetworkPath")

or

Dim UserName As String
UserName = (Environ$("Username"))
ShowFolderDialog("C:\Users\" & UserName & "\Documents")

etc etc

 

Hope that helps. The more code you have, the more memory used 😉

 

Nacho

Nacho
Automation & Design Engineer

Inventor automation Programmer (C#, VB.Net / iLogic)
Furniture, Sheet Metal, Structural, Metal fab, Tradeshow, Fabrication, CNC

EESignature


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.


Message 24 of 24

OceanaPolynom
Advocate
Advocate

Thanks very much.  The one written by Gruff is also working very well

John

0 Likes