How to pick a folder in vba

How to pick a folder in vba

Anonymous
Not applicable
3,292 Views
3 Replies
Message 1 of 4

How to pick a folder in vba

Anonymous
Not applicable

Hello, 

 

I'm trying to open a folder dialog box to pick a folder in vba. 

I've tried with "msofiledialogfolderpicker" and also with "FolderBrowserDialog" but it doesn't work because I think they are not Inventor class. 

 

Could you please help me ? 

 

Thank you

Mathias

0 Likes
Accepted solutions (1)
3,293 Views
3 Replies
Replies (3)
Message 2 of 4

Lewis.Young
Collaborator
Collaborator
Accepted solution

Hello,

 

The below code will open a folder dialog box:

 

SyntaxEditor Code Snippet

Function BrowseForFolder(Optional OpenAt As Variant) As Variant
     'Function purpose:  To Browser for a user selected folder.
     'If the "OpenAt" path is provided, open the browser at that directory
     'NOTE:  If invalid, it will open at the Desktop level

    Dim ShellApp As Object

     'Create a file browser window at the default folder
    Set ShellApp = CreateObject("Shell.Application"). _
    BrowseForFolder(0, "Please choose a folder", 0, OpenAt)

     'Set the folder to that selected.  (On error in case cancelled)
    On Error Resume Next
    BrowseForFolder = ShellApp.self.Path
    On Error Goto 0

     'Destroy the Shell Application
    Set ShellApp = Nothing

     'Check for invalid or non-entries and send to the Invalid error
     'handler if found
     'Valid selections can begin L: (where L is a letter) or
     '\\ (as in \\servername\sharename.  All others are invalid
    Select Case Mid(BrowseForFolder, 2, 1)
    Case Is = ":"
        If Left(BrowseForFolder, 1) = ":" Then Goto Invalid
    Case Is = "\"
        If Not Left(BrowseForFolder, 1) = "\" Then Goto Invalid
    Case Else
        Goto Invalid
    End Select

    Exit Function

Invalid:
     'If it was determined that the selection was invalid, set to False
    BrowseForFolder = False
End Function

 

Lewis Young
Windows 7 x64 - 32GB Ram
Intel Xeon E5-1620 v2 @ 3.70GHz
nVidia Quadro M2000 - 4GB
Inventor Professional 2017.3
Vault Basic 2017
3ds Max 2018

Message 4 of 4

Anonymous
Not applicable

Thanks a lot Lewis, it works very good.

I wasn't good enough in VBA to think about shell.