Macro equivalent to (File/open on specific folder)

Hello

I'm looking to create a solidworks macro to do the equivalent of file/open and pointing to a specific folder to then open the file of my choice.

Thank you for your help.

Lionel

Hello

An answer here:

Function SelectFolder(Optional Title As String, Optional TopFolder As String) As String

    Dim objShell As New Shell32.Shell   

Dim objFolder As Shell32.Folder

'If you use 16384 instead of 1 on the next line, files are also displayed

    Set objFolder = objShell.BrowseForFolder (0, Title, 1, TopFolder)  

  If Not objFolder Is Nothing Then

       SelectFolder = objFolder.Items.Item.Path

    End If

End Function

 

http://www.your-save-time-and-improve-quality-technologies-online-resource.com/vba-select-folder.html

 

Thank you for the responsiveness but small problem:

It should be missing the corresponding reference, you can try to check something like  

"Microsoft Shell Controls ... "

In the Tools > VBA References menu

1 Like

Excact .PL well done... That said, it's only a folder selection, impossible to open a file.

The idea is to open a file having "pre-selected" the folder in the macro.

See perhaps here:

http://www.eng-tips.com/viewthread.cfm?qid=165464

Or here:

http://www.eng-tips.com/faqs.cfm?fid=1164

 

Something like 'swApp.GetOpenFileName(.....) ` ?

 

http://help.solidworks.com/2013/english/api/sldworksapi/SolidWorks.Interop.sldworks~SolidWorks.Interop.sldworks.ISldWorks~GetOpenFileName.html

 

Its opens a window similar to the "Open" button, you can select the filters (File types to display), the initial file, etc.
The function returns the path of the chosen file, if the user clicks on the cross, the return path is empty.

1 Like

Thank you .PL we're getting closer...

How do I declare a specific path?

Indeed, with get it could work, we have an example here:

http://help.solidworks.com/2013/english/api/sldworksapi/Open_File_Example_VB.htm

fileName = swApp.GetOpenFileName("File to Attach", "", Filter, fileOptions, fileConfig, fileDispName)

With the statements :
value = instance. GetOpenFileName(DialogTitle, InitialFileName, FileFilter, OpenOptions, ConfigName, DisplayName)
InitialFileName
Path and filename of the file to open
1 Like

Indeed I hadn't seen the quotes ....

A big thank you to you.PL

Oops... answered too quickly...

Why won't the file selected for opening open?

I don't know! I'm running out of ideas, maybe it's not possible!

swApp.GetOpenFileName(...)

 

returns the path of the file as a 'String'. 

To open the file you have to use the function 'swApp.OpenDoc6'
http://help.solidworks.com/2012/English/api/sldworksapi/SolidWorks.Interop.sldworks~SolidWorks.Interop.sldworks.ISldWorks~OpenDoc6.html

If you want more help, use the SolidWorks Macro Recorder and open a file. You'll see how to use 'swApp.OpenDoc6(...) `

 

Ps: If you have solved your problem, add a "Best answer"