Page 1 of 1

Browse Folder

Posted: Fri Feb 04, 2022 12:47 pm
by CarrieIves
Like many people here, I am a SolidWorks user who is stumbling around trying to make a couple of macros to help our workflow. Much of what I do is from others macros. I have some understanding of what I am doing, but it's still at the beginner level.

I'm using SolidWorks 2020 on Windows 10. I have been doing VBA macros.

I have a macro I am working on that calls a function "BrowseForFolder". I have figured out how to get it to accept a starting location for browsing rather than defaulting to the desktop. The only problem with that, if I set it for "C:\" for example, I can't browse higher to get to a network drive if we need that instead. If I don't pass it a starting location, it starts at the desktop, which has me navigating quite a ways to get where I want. Is there a different way to do this that would work better?

Also, since I have a few macros that use similar functions, should I figure out how to store them separately rather than essentially repeating the same code? I know how to call a macro from a macro. Do you do the same sort of thing to call a function?

I have attached my macro.

Thanks for your help.

Carrie

Re: Browse Folder

Posted: Fri Feb 04, 2022 5:05 pm
by mattpeneguy
Carrie,
It may not be possible without some advanced code or a standalone application. Here's a thread from the old forum:
https://r1132100503382-eu1-3dswym.3dexp ... b70yb4BJfw
I just saw @christian chu post here, he may know if there's a newer solution to this problem. Or maybe @josh or @artem have a solution?

Re: Browse Folder

Posted: Sat Feb 05, 2022 12:22 am
by gupta9665
The simple and easy solution would be to add an Input box (like shown below) in which user can add the starting position folder. If no path is added, the macro will default to desktop or a defined location.

startingfolder = InputBox("Enter Start Folder Path ", "")

Other option might be to use excel API to show the browse folder window which will behave the way you need.

Re: Browse Folder

Posted: Sat Feb 05, 2022 9:38 am
by JSculley

Code: Select all

Function GetFolder(strPath As String) As String
    Dim shellApp As Object
    Set shellApp = CreateObject("Shell.Application")
    Dim folder As Object
    Set folder = shellApp.BrowseForFolder(0, Title, 0)
    If Not folder Is Nothing Then
        BrowseForFolder = folder.Self.Path
    End If
End Function


Re: Browse Folder

Posted: Sat Feb 05, 2022 1:53 pm
by christian chu
CarrieIves wrote: Fri Feb 04, 2022 12:47 pm
I have a macro I am working on that calls a function "BrowseForFolder". I have figured out how to get it to accept a starting location for browsing rather than defaulting to the desktop. The only problem with that, if I set it for "C:\" for example, I can't browse higher to get to a network drive if we need that instead. If I don't pass it a starting location, it starts at the desktop, which has me navigating quite a ways to get where I want. Is there a different way to do this that would work better?

Also, since I have a few macros that use similar functions, should I figure out how to store them separately rather than essentially repeating the same code? I know how to call a macro from a macro. Do you do the same sort of thing to call a function?

I have attached my macro.

Thanks for your help.

Carrie
you can have option to use NET macro as it has FolderBrowserDialog built-in
image.png
image.png (4.11 KiB) Viewed 7238 times
However, if you decide to stay with VBA macro, then you need to include win32 API for folder browser (see att. image)

Re: Browse Folder

Posted: Sat Feb 05, 2022 11:57 pm
by artem

Re: Browse Folder

Posted: Mon Feb 07, 2022 12:44 pm
by CarrieIves
I think the simplest solution will be to follow @gupta9665 's suggestion and put in an input box.

I will plan on copying the path from a window's file explorer to make it shorter than navigating the whole way there.
Also, since I have a few macros that use similar functions, should I figure out how to store them separately rather than essentially repeating the same code? I know how to call a macro from a macro. Do you do the same sort of thing to call a function?
Any suggestions on how I should be handling functions like this? Should I have a separate macro that basically just calls a function and then call that macro from other macros as needed?

Re: Browse Folder

Posted: Tue Feb 08, 2022 1:09 pm
by gupta9665
CarrieIves wrote: Mon Feb 07, 2022 12:44 pm I think the simplest solution will be to follow @gupta9665 's suggestion and put in an input box.

I will plan on copying the path from a window's file explorer to make it shorter than navigating the whole way there.


Any suggestions on how I should be handling functions like this? Should I have a separate macro that basically just calls a function and then call that macro from other macros as needed?
Another option would be to read the current path GetCurrentWorkingDirectory and use that to start the macro into (if it helps).

Regarding using the browse function in multiple macros, I would prefer to have it into each macro instead of calling from another macro. Make it is easier and faster to run IMO.

Re: Browse Folder

Posted: Fri Feb 11, 2022 12:19 pm
by Jaylin Hochstetler
If I'm exporting a bunch of files (dxfs or pdfs) to the same folder I use an input box where I paste my folder address into. Much faster than browsing for it every time. I built in a dialog that asks if you want to browse or enter the address. This way you can choose.
Attached is an example you may copy off of.
Edit to add: the bulk of this macro comes from @artem. https://www.codestack.net/solidworks-ap ... se-folder/

Re: Browse Folder

Posted: Fri Feb 11, 2022 5:33 pm
by CarrieIves
@Jaylin Hochstetler Thanks for the example of asking if the user wants to paste or browse. I have put my macro out to the users for feedback. I may change to this. Or since it's something we won't run real often, I may just leave it as is if it is adequate.

Re: Browse Folder

Posted: Sat Feb 26, 2022 9:28 am
by DeDum
@CarrieIves
suggestions on how I should be handling functions like this? Should I have a separate macro that basically just calls a function
Create a module. You can reuse this module in each macro just import it when needed. The module file type is .bas and can be edited in any text editor (I suggest Notepad++).
This link explains most of it: https://stackoverflow.com/questions/253 ... -variables
I have put my macro out to the users for feedback
I will plan on copying the path from a window's file explorer
If you make a global default string with a default folder location the end user can change this to their liking. Use this in conjunction with gupta9665 input box. If the user hits ok then they pasted in their new folder location. If the user hits cancel (or blank input) then the code uses the global default folder location. Of course code in some error handling.

Re: Browse Folder

Posted: Wed Mar 27, 2024 12:49 pm
by DeDum
Finally made the module. Allows adding to other macros

Some changes:
1. Replaced the variant variables with strings
2. Returning folder path will have "\" appended at end
3. User profile desktop returned as default

Add this module to any macro
VBA -> File -> Import File -> choose the .bas file
Confirm module added in VBA -> View -> Project Explorer Window -> See name of file under Modules folder

Main macro: BrowseFolderExample.swp
Module: "BrowseFolderMod.bas"
Folder Browser Module.zip
(11.63 KiB) Downloaded 325 times

Re: Browse Folder

Posted: Wed May 15, 2024 5:57 am
by Tapani Sjöman
I have stored a chosen directory to system registery. When I run my macro, it gets this value from registery. Then there is a button to change directory if one wants to set a new location.
---

Public Const myRegKeyPath As String = "HKEY_CURRENT_USER\Software\MyProgram\Path"

----

Function RegKeyRead(i_RegKey As String) As String
Dim myWS As Object

On Error Resume Next
'access Windows scripting
Set myWS = CreateObject("WScript.Shell")
'read key from registry
RegKeyRead = myWS.RegRead(i_RegKey)
End Function

---

Sub RegKeySave(i_RegKey As String, _
i_Value As String, _
Optional i_Type As String = "REG_SZ")
Dim myWS As Object

'access Windows scripting
Set myWS = CreateObject("WScript.Shell")
'write registry key
myWS.RegWrite i_RegKey, i_Value, i_Type

End Sub

------

Function RegKeyExists(i_RegKey As String) As Boolean
Dim myWS As Object

On Error GoTo ErrorHandler
'access Windows scripting
Set myWS = CreateObject("WScript.Shell")
'try to read the registry key
myWS.RegRead i_RegKey
'key was found
RegKeyExists = True
Exit Function

ErrorHandler:
'key was not found
RegKeyExists = False
End Function

------

On Error GoTo ErrorHandler

If RegKeyExists(myRegKeyPath) = True Then

Path = RegKeyRead(myRegKeyPath)

' make dir if not exist
If Dir(Path, vbDirectory) = "" Then MkDir Path

Else

' Path = SelectFolder("Please, Select a SaveAs Folder!", "") + "\"
UserForm1.TextBox1.text = Path
RegKeySave myRegKeyPath, Path

If Dir(Path, vbDirectory) = "" Then MkDir Path

End If

Re: Browse Folder

Posted: Wed May 22, 2024 2:56 am
by mihkov
gupta9665 wrote: Sat Feb 05, 2022 12:22 am Other option might be to use excel API to show the browse folder window which will behave the way you need.
Option to select a folder in a convenient window using the Excel API in Solidworks VBA. I'll leave this here.
excelLib.jpg

Code: Select all

Dim xlApp As Excel.Application
'Dim xlWB    As Excel.Workbook
Dim FolderPath As String

Sub Main()
    ' Create a hidden instance of Excel
    On Error Resume Next
    Set xlApp = New Excel.Application
    xlApp.Visible = False
    xlApp.DisplayAlerts = False
    'open the folder selection dialog box
        With xlApp.FileDialog(4) 'msoFileDialogFolderPicker
            .Title = "Select Folder"
            .AllowMultiSelect = False
            .InitialView = msoFileDialogViewList
            'Make different settings here
            If .Show = 0 Then
                FolderPath = "" 'folder not selected 
            Else
                On Error Resume Next
                Err.Clear
                If Err.Number <> 0 Then
                    FolderPath = "" 'folder selected error
                End If
                FolderPath = .SelectedItems(1)
            End If
        End With
    If FolderPath <> "" Then
        MsgBox "folder selected: " & FolderPath, vbInformation
    Else
        MsgBox "folder not selected.", vbInformation
    End If
    ' Closing an app of Excel
   ' xlWB.Close
    'Set xlWB = Nothing
    xlApp.Quit
    Set xlApp = Nothing
End Sub