How to Recover File Name with Macro?

Hello

 

Having just discovered how macros work, I'd like a little help to finish my code. After doing different researches, I didn't manage to get my code to run as I wanted.

The purpose of the macro is simple, I would like to be able to retrieve the name of my file so that I can give this name to my configurations. Basically, for parts files that have only one configuration, I'd like to rename all configurations by the file name. This will allow me to be able to make nomenclatures by choosing the name of the configuration and to have all the names.

I already have a bit of macro that works but I would have to replace the XXXXX with the name of the file which I can't do otherwise when I run this code, it renames the configuration to XXXXX.

SW 15

Dim swApp As Object

Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long

Sub main()


Set swApp = Application.SldWorks

Set Part = swApp.ActiveDoc

boolstatus = Part.Extension.SelectByID2("Default", "CONFIGURATIONS", 0, 0, 0, False, 0, Nothing, 0)
boolstatus = Part.EditConfiguration3("Default", "XXXXX", "", "", 36)
longstatus = Part.SaveAs3("C:\Users\pdepuydt\Desktop\Test MAcro\40x40 M10 tip. SLDPRT", 0, 2)
End Sub

 

Thanks in advance

Hello

 

I'll send you a macro that allows you to record the camera view in JPEG. The macro retrieves the file name.

I'll let you look for it, because it's my computer scientist who modified this macro


camera_vers_jpeg.swp

I do find in your macro codes that I found on the internet but in fact I can't insert them in mine so that it works.

Hello 

Try

FileName = Part.GetPathName


(or FileName = Part.GetTitle I don't know anymore..)

1 Like

Where do I insert it?

It shows me errors every time I try to launch it

Hello

I added Martvy's solution to your code:

**********************

Dim swApp As Object

Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long

Sub main()


Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc

filename = Part.GetTitle


boolstatus = Part.Extension.SelectByID2("Default", "CONFIGURATIONS", 0, 0, 0, False, 0, Nothing, 0)
boolstatus = Part.EditConfiguration3("Default", Filename, "", "", 36)


End Sub

**********************

This solution works fine, but if in Windows Explorer you show file extensions, then your configuration will be renamed with the file name + sldprt

Mick

4 Likes

Thank you very much, it works!

By any chance, how do you remove filename extensions?

in the macro or in Windows?

If it's in the macro, I'm not good enough in vba, if it's in Windows, it's much simpler:)

Hello

It's my turn to add in the code.

To remove the extension:

**********************

Dim swApp As Object

Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long

Sub main()


Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc

filename = Part.GetTitle

filename = Strings.Left(Filename, Len(Filename) - 7)


boolstatus = Part.Extension.SelectByID2("Default", "CONFIGURATIONS", 0, 0, 0, False, 0, Nothing, 0)
boolstatus = Part.EditConfiguration3("Default", Filename, "", "", 36)


End Sub

**********************

 

3 Likes