Browse Folder

Programming and macros
User avatar
CarrieIves
Posts: 133
Joined: Fri Mar 19, 2021 11:19 am
Answers: 2
Location: Richardson, TX
x 312
x 112

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 281 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: 1380
Joined: Tue Mar 09, 2021 11:14 am
Answers: 4
x 2487
x 1888

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: 359
Joined: Thu Mar 11, 2021 10:20 am
Answers: 20
Location: India
x 383
x 414

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: 574
Joined: Tue May 04, 2021 7:28 am
Answers: 52
x 7
x 806

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 3446 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 73

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: 133
Joined: Fri Mar 19, 2021 11:19 am
Answers: 2
Location: Richardson, TX
x 312
x 112

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: 359
Joined: Thu Mar 11, 2021 10:20 am
Answers: 20
Location: India
x 383
x 414

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: 383
Joined: Sat Mar 13, 2021 8:47 pm
Answers: 4
Location: Michigan
x 375
x 353
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 135 times
A goal is only a wish until backed by a plan.
User avatar
CarrieIves
Posts: 133
Joined: Fri Mar 19, 2021 11:19 am
Answers: 2
Location: Richardson, TX
x 312
x 112

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: 2
Joined: Sat Feb 26, 2022 9:01 am
Answers: 0
x 1

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: 2
Joined: Sat Feb 26, 2022 9:01 am
Answers: 0
x 1

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 19 times
Post Reply