Hello all,
how can I load FileDialog from UserForm? I need load folder path to TextBox and I need open FileDialog via button. How can I set it? If I use this code:
<p> Dim fDialog As FileDialog, result As Integer</p><p> Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)</p><p></p><p> 'Optional: FileDialog properties</p><p> fDialog.Title = "Select a folder"</p><p> fDialog.InitialFileName = "C:\\"</p><p> </p><p> If fDialog.Show = -1 Then</p><p> Debug.Print fDialog.SelectedItems(1)</p><p> End If</p>
macro end with error: Object doesn't support this property or method.
Do I have to set some object as SldWorks? In my opinion this is process in Windows, not in Solidworks. I haven't got allow some library (Tools -> References).
Thnaks a lot for your answers.
SolidworksApi macros
Please read this
Thnaks, this is for VB.NET. I nedd this for VBA. I'm sorry, A was lost :-)
Then you need some Window API call to get a file dialog box
Add a module and copy/paste this code
Use DotNetTools for VBA – CADHERO consulting SUARL . Save yourself the hassle of Windows API calls.
It has the .Net BrowseToFileDialog class exposed.
Cheers,
Amen
CADHERO consulting SUARL – SOLIDWORKS API/PDM Design Automation & Training – SOLIDWORKS VBA MACROS, SOLIDWORKS Addins an…
Wow, I've waited some easier solution.
But... How do I call this function? Call ModuleName.GetFolder? It's not function...
Just call:
GetFolder("Select a folder or create a new one:")
or use tool created by Amen
It's totally cool - Thx for sharing !!!
yeah. do this.
abuse s code he created.
OK, thanks a lot.
no problem
Hello,
siplier code maybe:
<p>Sub FolderSelection()</p><p> Dim MyPath As String</p><p> MyPath = SelectFolder("Select Folder", "")</p><p> If Len(MyPath) Then</p><p> MsgBox MyPath</p><p> Else</p><p> MsgBox "Cancel was pressed"</p><p> End If</p><p>End Sub</p><p></p><p>'Both arguements are optional. The first is the dialog caption and</p><p>'the second is is to specify the top-most visible folder in the</p><p>'hierarchy. The default is "My Computer."</p><p></p><p>Function SelectFolder(Optional Title As String, Optional TopFolder _</p><p> As String) As String</p><p> Dim objShell As New Shell32.Shell</p><p> Dim objFolder As Shell32.Folder</p><p></p><p>'If you use 16384 instead of 1 on the next line,</p><p>'files are also displayed</p><p> Set objFolder = objShell.BrowseForFolder _</p><p> (0, Title, 1, TopFolder)</p><p> If Not objFolder Is Nothing Then</p><p> SelectFolder = objFolder.Items.Item.Path</p><p> End If</p><p>End Function</p>Can it be modified to see network drives also?
My answer does network drives.
Indeed. I needed to map network drive
This is working in Excel -
I can't get it working in SW - Does somebody know how to solve this?
//Niels
'https://software-solutions-online.com/excel-vba-application-filedialog-object/
Sub GetFile()
Dim SelectedItem As String
With Application.FileDialog(msoFileDialogFilePicker)
If .Show = -1 Then
'if user clicks OK
SelectedItem = .SelectedItems(1)
MsgBox "You have selected: " & SelectedItem
Else
'if user clicks Cancel
End If
End With
End Sub
'Found this from Deepak
'https://forum.solidworks.com/thread/219602
Option Explicit
Dim swapp As SldWorks.SldWorks
Dim Filter As String
Dim fileName As String
Dim fileConfig As String
Dim fileDispName As String
Dim fileOptions As Long
Sub main()
Set swapp = Application.SldWorks
Filter = "SOLIDWORKS Drawing Files (*.slddrw)|*.slddrw|Excel(*.xls)|*.xls|"
fileName = swapp.GetOpenFileName("Select File", "", Filter, fileOptions, fileConfig, fileDispName)
MsgBox fileName
End Sub
So... When you are running VBA from Excel, "Application" is Excel. Excel has a .FileDialog function.
When you're running VBA from SolidWorks, "Application" is SolidWorks, which doesn't have a .FileDialog function.
You can create an instance of Excel and use that application sort of in stealth mode. There's an example of doing that here:
How can you use a text editor to determin SolidWorks File Version - DASSAULT: SOLIDWORKS 3D Design - Eng-Tips
Hi Josh
Thank you for the answer.
I got an error when doing that..
Sub GetFile()
Dim swApp As SldWorks.SldWorks
Dim myxlApp As Excel.Application
Dim PickedFiles As Variant
Dim SelectedItem As String
Set myxlApp = CreateObject("Excel.Application")
With Application.FileDialog(msoFileDialogFilePicker)
If .Show = -1 Then
'if user clicks OK
SelectedItem = .SelectedItems(1)
PickedFiles = SelectedItem
Else
'if user clicks Cancel
End If
End With
Set myxlApp = Nothing
MsgBox PickedFiles
End Sub
Is GetOpenFileName method not working for SW files?
You are STILL running the code from inside SolidWorks. Therefore, "Application" is SolidWorks. Application.FileDialog will not work unless you are RUNNING THE CODE IN EXCEL.
myxlApp.FileDialog should work in your code above.
Hi Deepak
It does, thank you for sharing code
I found the excel object smart - there is a lot of functions.
But it seams not possible to use in SW.
'https://software-solutions-online.com/excel-vba-application-filedialog-object/
BR Niels
is there a way to set a default path for your folder dialog box to open to? (Nice bit of work by the way). It would save me numerous repeated clicks if I could start out in a specific folder within my My Documents area.
Thanks,
Daen
You mean using the path you select the last time as a default? if yes, then you can use SaveSetting and GetSetting
Something like this:
The first time running the macro, you'd get "C\Temp" as your default
but after selecting a new path, it'll be saved in the registry in "NewPathName" for your use next time
in the form, add textbox1 and commandbutton1 and add these lines:
Private Sub UserForm_Activate()
Dim getPathString As String
getPathString = GetSetting("MyMacro", "MacroName", "PathNameStr", "C:\Temp")
TextBox1.Text = getPathString
End Sub
Private Sub CommandButton1_Click()
TextBox1.Text = GetFolder("Select a destination folder or create a new one:")
SaveSetting "MyMacro", "MacroName", "PathNameStr", TextBox1.Text
End Sub
Thanks . I'll work with this.
I would like to be able to set the default for the first time the dialog opens.
Daen
,
After reviewing your suggestion I see two issues:
When I click my CommandButton1 I want to launch your dialog box and have that dialog box open to the folder already specified in Textbox1.
Thanks for your patience.
Daen
Daen,
if you don't have permission to write to registry then it's fine. Use ini file instead to save the last selected default folder
As Josh and others mentioned, File dialog is not included in the SW API; however, you can run it on Excel App
Here is the code you can try
all you need to change in the code is to retrieve the Selected Default Folder from your .ini file and save in saveDefaultFolderStr
Private Sub CommandButton1_Click()
' TextBox1.Text = GetFolder("Select a destination folder or create a new one:")
' SaveSetting "MyMacro", "MacroName", "PathNameStr", TextBox1.Text
Dim saveDefaultFolderStr As String
saveDefaultFolderStr = "C:\Temp"
newBrowseForFolder (saveDefaultFolderStr)
End Sub
Function newBrowseForFolder(saveDefaultFolderStr As String) As String
Dim xlApp As Excel.Application
Set xlApp = CreateObject("Excel.Application")
Dim selectedFolderString As String
With xlApp.FileDialog(msoFileDialogFolderPicker)
.Title = "Select folder"
.ButtonName = "Select"
.InitialFileName = saveDefaultFolderStr
If .Show = -1 Then
selectedFolderString = .SelectedItems(1)
MsgBox Chr(34) & selectedFolderString & Chr(34) & " is now your default selected folder - Please save it to the ini file"
Else
MsgBox Chr(34) & saveDefaultFolderStr & Chr(34) & " is still Default Folder"
End If
End With
Set xlApp = Nothing
End Function
,
Worked perfectly! Thanks for the helping hand.
Daen
Glad it works out for you
Just a little note: If you only need to read 1 line of folder string of characters from a text file, why don't you use simple text file instead of ini
see this thread of how to use it
https://forum.solidworks.com/thread/202095
,
I had the ini file already in place to handle other things so reading in a key for the default start folder was minimal additional effort.
thanks,
Daen
Ok, cool!
Late to the party but ...https://www.codestack.net/solidworks-api/import-export/export-pdf-browse-folder/