Browse Folder

Programming and macros
User avatar
CarrieIves
Posts: 163
Joined: Fri Mar 19, 2021 11:19 am
Answers: 2
Location: Richardson, TX
x 376
x 136

Browse Folder

Unread post 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
Attachments
test-browse-folder.swp
(39.5 KiB) Downloaded 400 times
by gupta9665 » Sat Feb 05, 2022 12:22 am
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.
Go to full post
User avatar
mattpeneguy
Posts: 1386
Joined: Tue Mar 09, 2021 11:14 am
Answers: 4
x 2489
x 1899

Re: Browse Folder

Unread post 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?
User avatar
gupta9665
Posts: 405
Joined: Thu Mar 11, 2021 10:20 am
Answers: 25
Location: India
x 426
x 445

Re: Browse Folder

Unread post 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.
Deepak Gupta
SOLIDWORKS Consultant/Blogger
User avatar
JSculley
Posts: 638
Joined: Tue May 04, 2021 7:28 am
Answers: 55
x 9
x 868

Re: Browse Folder

Unread post 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

User avatar
christian chu
Posts: 64
Joined: Wed Mar 31, 2021 3:00 pm
Answers: 0
x 31
x 39

Re: Browse Folder

Unread post 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 6172 times
However, if you decide to stay with VBA macro, then you need to include win32 API for folder browser (see att. image)
Attachments
Capture.JPG
artem
Posts: 26
Joined: Thu Mar 18, 2021 1:31 pm
Answers: 3
x 9
x 74

Re: Browse Folder

Unread post by artem »

Thanks,
Artem
xarial.com - making your CAD better
codestack.net - SOLIDWORKS API macros and tutorials
User avatar
CarrieIves
Posts: 163
Joined: Fri Mar 19, 2021 11:19 am
Answers: 2
Location: Richardson, TX
x 376
x 136

Re: Browse Folder

Unread post 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?
User avatar
gupta9665
Posts: 405
Joined: Thu Mar 11, 2021 10:20 am
Answers: 25
Location: India
x 426
x 445

Re: Browse Folder

Unread post 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.
Deepak Gupta
SOLIDWORKS Consultant/Blogger
User avatar
Jaylin Hochstetler
Posts: 387
Joined: Sat Mar 13, 2021 8:47 pm
Answers: 4
Location: Michigan
x 380
x 355
Contact:

Re: Browse Folder

Unread post 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/
Attachments
Save All Sheets As PDF and Close REV01.swp
(66 KiB) Downloaded 208 times
A goal is only a wish until backed by a plan.
User avatar
CarrieIves
Posts: 163
Joined: Fri Mar 19, 2021 11:19 am
Answers: 2
Location: Richardson, TX
x 376
x 136

Re: Browse Folder

Unread post 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.
User avatar
DeDum
Posts: 5
Joined: Sat Feb 26, 2022 9:01 am
Answers: 1
x 3

Re: Browse Folder

Unread post 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.
User avatar
DeDum
Posts: 5
Joined: Sat Feb 26, 2022 9:01 am
Answers: 1
x 3

Re: Browse Folder

Unread post 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 131 times
User avatar
Tapani Sjöman
Posts: 42
Joined: Mon May 03, 2021 9:53 am
Answers: 0
x 33
x 14

Re: Browse Folder

Unread post 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
User avatar
mihkov
Posts: 44
Joined: Sun Feb 05, 2023 2:01 am
Answers: 0
x 17
x 24

Re: Browse Folder

Unread post 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
Post Reply