How to prompt user to select a folder directory for saving files in VBA

How to prompt user to select a folder directory for saving files in VBA

jake_egley
Advocate Advocate
332 Views
7 Replies
Message 1 of 8

How to prompt user to select a folder directory for saving files in VBA

jake_egley
Advocate
Advocate

I have my export dxf code and I want the user to be prompted to select the directory for where to save the dxf. 

 

This is what I got now, but it only works when you type a file name and I don't want the user to have to type a file name, Im not going to use the typed file name anyways.

 

 

Dim oFileDirMan As String

Dim oFileDlg As FileDialog
Call ThisApplication.CreateFileDialog(oFileDlg)
oFileDlg.MultiSelectEnabled = False
oFileDlg.CancelError = False
oFileDlg.DialogTitle = "Save Where"
oFileDlg.FileName = "Please choose a directory..."
oFileDlg.ShowSave

oFileDirMan = oFileDlg.FileName & "\"

MsgBox (oFileDirMan)

 

 

Appreciate any help.

Jake Egley
Inventor 2022
0 Likes
Accepted solutions (1)
333 Views
7 Replies
Replies (7)
Message 2 of 8

C_Haines_ENG
Collaborator
Collaborator
0 Likes
Message 3 of 8

C_Haines_ENG
Collaborator
Collaborator

If that doesn't work, you can try this:

Dim objShell As Object
objShell = CreateObject(“Shell.Application”)
objFolder = objShell.BrowseForFolder(0, "", 0, “C:\”) 

If objFolder Is Nothing Then Exit Sub

Dim oPath As String
oPath = objFolder.Self.Path

MsgBox(oPath)

 I don't do Inventor code in Visual Studio so this may not work. 

0 Likes
Message 4 of 8

jake_egley
Advocate
Advocate

I'm using visual basic for my code instead of iLogic. Sadly none of that code works nor can I adapt it. I found this but cant get it to work since shell and folder are not objects in inventor visual basic.

 

This is the visual basic code from the article but it doesn't work due to the objects not existing:

 

    Dim objShell   As Shell
    Dim ssfWINDOWS As Long
    Dim objFolder  As Folder
    
    ssfWINDOWS = 36
    Set objShell = New Shell
        Set objFolder = objShell.BrowseForFolder(0, "Example", 0, ssfWINDOWS)
            If (Not objFolder Is Nothing) Then
                'Add code here
            End If
        Set objFolder = Nothing
    Set objShell = Nothing
Jake Egley
Inventor 2022
0 Likes
Message 5 of 8

C_Haines_ENG
Collaborator
Collaborator

You will need to import Shell.

 

Maybe this video will help. 

0 Likes
Message 6 of 8

jake_egley
Advocate
Advocate

Tried that but when I go to tools>references it is grayed out, so I can't import shell.

 

jake_egley_0-1736963717724.png

 

Jake Egley
Inventor 2022
0 Likes
Message 7 of 8

C_Haines_ENG
Collaborator
Collaborator
Accepted solution

How about this code. I got this working in VBA. I assume you're doing this from Excel.

 

Dim objFolder As Object
Dim strFolderFullPath As String

Sub FolderBrowser()

    'Sub-routine to open a file browser
    'The starting point of the browser is hard-coded.
    Dim objShell As Object, OpenAt As Variant
 
    'The file browser starting point can be changed in the line below
    OpenAt = "C:\Documents and Settings\tcbealdk\My Documents\"
 
    Set objFolder = Nothing
    Set objShell = CreateObject("Shell.Application")
 
    Set objFolder = objShell.BrowseForFolder(0, "Please browse to or create a folder", 0, "C:\")
    
    '~~> If user presses cancel
    If objFolder Is Nothing Then
        MsgBox "User Pressed Cancel"
    Else
        strFolderFullPath = objFolder.Items.Item.Path
        MsgBox ("the folder is: " & strFolderFullPath)
    End If
 
    Set objShell = Nothing


End Sub

 

0 Likes
Message 8 of 8

jake_egley
Advocate
Advocate

Works perfect, thank you

Jake Egley
Inventor 2022
0 Likes