VBA - Show folder selection dialog with path

Hello everyone,

First time for me that I post on the forum (and I don't know too much about SD)

 

Unfortunately for me I can't find the solution for what I'm looking for.

I would like to display a folder selection dialog like this: 

 

Dim SH as Shell32.Shell

Dim F as Shell32.Folder

Set SH = New Shell32.Shell

Set F = SH. BrowseForFolder(&H0&, "Message", &H1,"InitialFolder")

 

It works, but if I fill "InitialFolder" with a path it blocks my selection to the underlying folder :/

When I just want it to point or unstack the directories to the directory that I entered.

By having the possibility to return to parent folders.

 

I hope I was clear

Thank you for reading my post so far

1 Like

Hi, is it for PDM or for SolidWorks?

In any case, on the BrowseForFolder help page, it says that it's obsolete and that you should use:

IEdmVault11::BrowseForFolder2.

See here:

http://help.solidworks.com/2015/English/api/epdmapi/EPDM.Interop.epdm~EPDM.Interop.epdm.IEdmVault11~BrowseForFolder2.html

1 Like

The question is not very clear
it would be necessary to specify more

Hello and thank you for answering me,

Yes sorry for the unclear question^^

So, already it's VBA code for Solidworks

Then basically I don't know how to :

-With the dialog box, point to a directory that I indicate as a parameter

-With the dialog box, display all directories, not just the ones that are underlying the parameter I entered)

 

I hope it's clearer

As I said above, try using BrowseForFolder2 rather than BrowseForFolder which is deprecated:

http://help.solidworks.com/2015/English/api/epdmapi/EPDM.Interop.epdm~EPDM.Interop.epdm.IEdmVault11~BrowseForFolder2.html

Hello

Okay, thanks for the information.

 

But I don't really understand how to make this function work in fact 

If you could help me please!

Because I'm struggling here :/

Here are some examples of functional codes:

http://www.ammara.com/access_image_faq/browse_for_folder_dialog.html

https://forum.solidworks.com/thread/25843

https://forum.solidworks.com/thread/89758

It will be easier to download code already done than to try to write a full macro if you don't know too much about VBA programming.

Hello

 

Thank you for your help but unfortunately it's not what I'm looking for.

The codes you provided me use the simple BrowseForFolder function.

They just put an additional Userform, but that's not what I'm looking for.

 

In fact I would like exactly the same function: BrowseForFolder

But if we pass it a parameter: initialFolder, it doesn't forbid us to go where we want anyway.

 

That is, if initialFolder = "C:\Users\Public\Music\Sample Music"

I want to be able to go to: "C:\Users\Public"

 

Sorry I don't have solidworks to test, you can try to watch this tutorial:

 http://www.cadsharp.com/videos/lesson-1-10-vba/

 

Hello

 

I looked at your links unfortunately I don't want to use references or components that are not managed by all the stations!

 

Thank you anyway for your help!! It's very nice

 

I'll leave the post open until I find the solution

Hello 

it's okay I found a solution!!

After multiple attempts, I'll leave you the code 

The solutions for these two positions:

http://codes-sources.commentcamarche.net/forum/affich-15102-selectionner-un-repertoire-avec-une-boite-de-dialogue-en-vba

http://www.cadsharp.com/blog/solidworks-macro-compatible-64-vba7/

 

to be modified as you wish:

Private Type BROWSEINFO ' used by the function GetFolderName

hOwner As LongPtr

pidlRoot As LongPtr

pszDisplayName As String

lpszTitle As String

ulFlags As LongPtr

lpfn As LongPtr

lParam as LongPtr

iImage As LongPtr

End Type

#If VBA7 Then

    Private Declare PtrSafe Function SHGetPathFromIDList Lib "shell32.dll" aka "SHGetPathFromIDListA" (ByVal pidl as LongPtr, ByVal pszPath as String) as LongPtr

    Private Declare PtrSafe Function SHBrowseForFolder Lib "shell32.dll" aka "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As LongPtr

#Else

    'Private Declare Function SHGetPathFromIDList lib "shell32.dll" aka "SHGetPathFromIDListA" (ByVal pidl as LongPtr, ByVal pszPath as String) as long

    'Private Declare Function SHBrowseForFolder Lib "shell32.dll" aka "SHBrowseForFolderA" (lpBrowseInfo as BROWSEINFO) as long

#End Yew

 

Function GetFolderName(Msg As String) As String

' returns the name of the folder selected by the user

Dim bInfo As BROWSEINFO, path As String, r As LongPtr, x As LongPtr, pos As Integer

'bInfo.pidlRoot 0& ' Root folder Desktop

If IsMissing(Msg) Then

bInfo.lpszTitle = "Select a working directory" ' the dialog title

Else

bInfo.lpszTitle = Msg ' the dialog title

End If

bInfo.ulFlags = &H1 ' Type of directory to return

x = SHBrowseForFolder(bInfo) ' display the dialog

' Parse the result

path = Space$(512)

r = SHGetPathFromIDList(ByVal x, ByVal path)

If r Then

pos = InStr(path, Chr$(0))

GetFolderName = Left(path, pos - 1)

Else

GetFolderName = ""

End If

End Function

 

Private Sub CommandButton2_Click()

Dim Rep0 As String

Rep0 = GetFolderName("Choose a working directory")

FrmE2S.TextBox1.Text = Rep0

End Sub

:)

Hello

 

A little recap!!

If you find the correct answer: 

http://www.lynkoa.com/forum/solidworks/bouton-parcourir-macro-vba

Sadden^^

Good luck to all!

1 Like