Browse Folder
- CarrieIves
- Posts: 163
- Joined: Fri Mar 19, 2021 11:19 am
- Location: Richardson, TX
- x 376
- x 136
Browse Folder
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
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
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 poststartingfolder = 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.
- mattpeneguy
- Posts: 1386
- Joined: Tue Mar 09, 2021 11:14 am
- x 2489
- x 1899
Re: Browse Folder
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?
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
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.
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
SOLIDWORKS Consultant/Blogger
Re: Browse Folder
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
- christian chu
- Posts: 64
- Joined: Wed Mar 31, 2021 3:00 pm
- x 31
- x 39
Re: Browse Folder
you can have option to use NET macro as it has FolderBrowserDialog built-in However, if you decide to stay with VBA macro, then you need to include win32 API for folder browser (see att. image)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
Re: Browse Folder
Check this: https://www.codestack.net/visual-basic/ ... se-folder/
And this (if you need to browse for file as well): https://www.codestack.net/visual-basic/ ... owse-file/
And this (if you need to browse for file as well): https://www.codestack.net/visual-basic/ ... owse-file/
Thanks,
Artem
xarial.com - making your CAD better
codestack.net - SOLIDWORKS API macros and tutorials
Artem
xarial.com - making your CAD better
codestack.net - SOLIDWORKS API macros and tutorials
- CarrieIves
- Posts: 163
- Joined: Fri Mar 19, 2021 11:19 am
- Location: Richardson, TX
- x 376
- x 136
Re: Browse Folder
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.
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?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?
Re: Browse Folder
Another option would be to read the current path GetCurrentWorkingDirectory and use that to start the macro into (if it helps).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?
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
SOLIDWORKS Consultant/Blogger
- Jaylin Hochstetler
- Posts: 387
- Joined: Sat Mar 13, 2021 8:47 pm
- Location: Michigan
- x 380
- x 355
- Contact:
Re: Browse Folder
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/
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.
- CarrieIves
- Posts: 163
- Joined: Fri Mar 19, 2021 11:19 am
- Location: Richardson, TX
- x 376
- x 136
Re: Browse Folder
@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
@CarrieIves
This link explains most of it: https://stackoverflow.com/questions/253 ... -variables
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++).suggestions on how I should be handling functions like this? Should I have a separate macro that basically just calls a function
This link explains most of it: https://stackoverflow.com/questions/253 ... -variables
I have put my macro out to the users for feedback
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.I will plan on copying the path from a window's file explorer
Re: Browse Folder
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"
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"
- Tapani Sjöman
- Posts: 42
- Joined: Mon May 03, 2021 9:53 am
- x 33
- x 14
Re: Browse Folder
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
---
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
Option to select a folder in a convenient window using the Excel API in Solidworks VBA. I'll leave this here.
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