64 Replies Latest reply on Nov 2, 2017 1:32 AM by Deepak Gupta

    Save As File name based on custom property

    Stephen Ball

      Hi there,

       

      Is there a way to create a save as macro that will automatically fill in the file name field with information from a custom property?

      One example of this would be for using an auto part number generator.  Let's say the number generator automatically sets the "Number" custom property based on the next available part number.  Then, when I save the part, it would be nice if the file name was automatically set to this number.

       

      I appreciate any advice or thoughts on this!

       

      Thanks in advance,

      Stephen

        • Re: Save As File name based on custom property
          Kevin Bouwman

          I would use this too.  Now to find an auto part number generator.

          • Re: Save As File name based on custom property
            Jacob Cordingley

            if you look in the Solidworks API it is simple to you the Save or SaveAs commands and simple to get info from the custom properties

             

            Dim swApp as Sldworks.SldWorks

            Dim SWmoddoc as SldWorks.ModDoc2

            Dim partnumber as string

             

             

            set swApp=SldWorks.SldWorks

            set SWmoddoc=swApp.ActiveDoc

             

               partnumber= SWmoddoc.GetCustomInfoValue(Configuration, FieldName)

            if(SWmoddoc.GetType==swDocASSEMBLY) then

                 SWmoddoc.Save(partnumber+".sldasm")

            else if(SWmoddoc.GetType==swDocPART) then

                 SWmoddoc.Save(partnumber+".sldprt")

            else if(SWmoddoc.GetType==swDocDRAWING) then

                 SWmoddoc.Save(partnumber+".slddrw")

            end if

             

            Do you have a database or excel of part numbers used?

              • Re: Save As File name based on custom property
                Filipe Venceslau

                Jacob,

                 

                How would you go about, having the SaveAs prompt window pop-up, with the dafault Save-As Text = partnumber string?

                This way the user would have the flexibility to editing the file name as well as changing the folder destination.

                 

                Regards,

                • Re: Save As File name based on custom property
                  Stephen Ball

                  Hi Jacob,

                   

                  Thanks for the details.  I've never written any macros before, and I'm having trouble with this code.  Is there anything I need to include in the macro other than what you posted?

                   

                  Thanks,

                  Stephen

                    • Re: Save As File name based on custom property
                      Jacob Cordingley

                      you need a sub

                       

                      Sub main()

                      place Code here

                      end sub

                      • Re: Save As File name based on custom property
                        Filipe Venceslau

                        Experiment, with recording macros, it is pretty straight forward.

                        To record Macros, either pull-up the "Macro" toolbar (right click in the toolbar area and select "Macro"), or go to Tools --> Macro.

                        Then you can record, edit and play macros.

                        Try this quick and easy example to record a macro opening and saving a file with a different name:

                         

                        -With all documents closed, in solidworks go  to Tools->Macros->Record (this will start recording your macro)

                        -Now open a document of your choice

                        -Now do a save as with a different name and close the document.

                        -Go back to Tools->Macros and select STOP.

                         

                        You will be asked for a location to store your solidworks macro file (*.swp), just save it on the desktop for easy reference.

                        Now, in the Macro menu, select EDIT. This will prompt you to locate the solidworks macro file to edit, pick the file you saved on your desktop.

                        Solidworks will automatically start VBA and open up your module, with the respective programming that was captured while recording your macro.

                         

                        Read each line, and try and figure out what's happening in each of them.

                        Keep in mind, that solidworks macro recorder doesn't always record stuff in the best programming way, you will see many calls to the selectionmanager, I prefer not to use this when programming.

                         

                        So, for this code sample that Jacob supplied you to work, just replace all the code inside "Sub Main"/"End" with Jacobs code.

                        Then to run, you go back to Tools-> Macro->Run and point to your file...

                         

                        I'm sure you know all this already, but just thought I'd throw it out there. lol

                         

                        In the meantime, if your code doesn't want to work, post it up, I'll be glad to help.

                         

                        Filipe

                          • Re: Save As File name based on custom property
                            Stephen Ball

                            Hi Filipe,

                             

                            I really appreciate your help!  Very kind of you.

                             

                            Below is a copy of the entire code in the macro that I have tried.  I have set a custom property called "Number" as a numerical part number.  I do not use configurations, and by default, the name of the lone configuration is "Default", so that's why I typed that (Is this even correct?).

                             

                            To run the macro, I have an open part file, and then I run the macro.  What I'm left with is hte original part with the original name.  Nothing has happened.  Is the macro doing a "save as copy"?  If so, where might the file go?  What happens if there already exists a file with the name that I have created from the "number" field?

                             

                            Thanks a million,

                             

                            Stephen

                             

                            __________________________________________

                            Sub main()

                             

                            Dim swApp As SldWorks.SldWorks
                            Dim SWmoddoc As SldWorks.ModelDoc2
                            Dim partnumber As String


                            Set swApp = Application.SldWorks
                            Set SWmoddoc = swApp.ActiveDoc


                            partnumber = SWmoddoc.GetCustomInfoValue(Default, Number)


                            If (SWmoddoc.GetType = swDocASSEMBLY) Then
                                SWmoddoc.SaveAs (partnumber + ".sldasm")
                               
                            ElseIf (SWmoddoc.GetType = swDocPART) Then
                                 SWmoddoc.SaveAs (partnumber + ".sldprt")

                             

                            End If


                            End Sub

                            __________________________________________

                              • Re: Save As File name based on custom property
                                Jacob Cordingley

                                if not in the config go

                                partnumber = SWmoddoc.GetCustomInfoValue("", Number)

                                • Re: Save As File name based on custom property
                                  Filipe Venceslau

                                  Here you go, try this out...see if it works for what you need.

                                  You were forgetting the double quotes (") when calling the custom property, plus the property in that code, was for configuration specific values, I'm assuming you only need values from the Custom tab.

                                  Also, the full file destination path is required with SaveAs.

                                   

                                  Cheers,

                                  Hope this helps

                                   

                                  _________________________________________________________________________

                                   

                                  Sub main()
                                  Dim swApp As SldWorks.SldWorks
                                  Dim SWmoddoc As SldWorks.ModelDoc2
                                  Dim partnumber As String
                                  Set swApp = Application.SldWorks
                                  Set SWmoddoc = swApp.ActiveDoc
                                  'Use this....To Get the value from the Properties in the CUSTOM tab
                                  partnumber = SWmoddoc.CustomInfo("Number")
                                  'Or this....To Get the value from the Configuration Specific Tab
                                  'partnumber = SWmoddoc.GetCustomInfoValue("Default", "Number")
                                  PathName = SWmoddoc.GetPathName     'this gets the full path of the current open document (including filename)
                                  Filepath = Left(PathName, InStrRev(PathName, "\"))  'uses to full pathname to get the folder path, to be added to the save as file name
                                  If (SWmoddoc.GetType = swDocASSEMBLY) Then
                                      SWmoddoc.SaveAs (Filepath + partnumber + ".sldasm")
                                     
                                  ElseIf (SWmoddoc.GetType = swDocPART) Then
                                       SWmoddoc.SaveAs (Filepath + partnumber + ".sldprt")
                                  End If
                                  End Sub
                                    • Re: Save As File name based on custom property
                                      Stephen Ball

                                      That works great!  Thank Jacob and Filipe for your help!

                                       

                                      Now, I can use SWit to generate the part number, and then run this macro to save the file with that part number.  Excellent!

                                        • Re: Save As File name based on custom property
                                          Filipe Venceslau

                                          Glad we could be of help! That's what the community is for!

                                          You could even add a shortcut button to your macro.

                                          With any document open, just go to Tools->Customize...and in the "Commands" tab, look under the "Macro" button list.

                                          There should be one called "New Macro Button" (hover over, to see the name), just drag it onto the toolbar you'd like to attach your macro shortcut button to. Once dropped, solidworks will ask you for the location of the macro to run, and the respective procedure to launch.

                                          Here's a picture in case you get lost...lol

                                           

                                          Cheers!

                                           

                                          1.bmp

                                            • Re: Save As File name based on custom property
                                              Stephen Ball

                                              Hi again,

                                               

                                              Now that I have the basic save as macro working, I thought I would try to get a version working in which it brings up the save as dialog box, auto-filled with the part number retreived from the custom properties field.  This is what Filipe posted about at one point.

                                               

                                              I have used the command that Jacob suggested:

                                               

                                              swApp.RunCommand SwCommands.swCommands_SaveAs, ""

                                               

                                              This command does indeed bring up the save dialog.  But I can't seem to insert the "partnumber" variable that I want to use as my file name.  Can you think of a way to do this?

                                               

                                              Thanks!

                                              Stephen

                                                • Re: Save As File name based on custom property
                                                  Stephen Ball

                                                  OK, I actually got the macro to work with my "partnumber" inserted into the save as diaplog box, but as Jacob said, this works only for the first time it is saved.  Is there a way to get it to work when it has been saved from an existing part?  I used the following code:

                                                   

                                                  SWmoddoc.SetTitle2 partnumber
                                                  swApp.RunCommand SwCommands.swCommands_SaveAs, ""

                                                   

                                                  Thanks

                                                    • Re: Save As File name based on custom property
                                                      Jacob Cordingley

                                                      I did some  Research and found this

                                                       

                                                      Dim CommonDialog1 As Object

                                                      Dim swapp As SldWorks.SldWorks
                                                      Dim swModdoc As SldWorks.ModelDoc2
                                                      Dim ext As String

                                                      Public Sub main()
                                                           Set swapp = Application.SldWorks
                                                           Set swModdoc = swapp.ActiveDoc

                                                           Set CommonDialog1 = CreateObject("MSComDlg.CommonDialog")


                                                           On Error Resume Next

                                                           ext = ".SLD"
                                                           If (swModdoc.GetType = SwConst.swDocumentTypes_e.swDocASSEMBLY) Then
                                                                    ext = ext + "ASM"
                                                           ElseIf (swModdoc.GetType = SwConst.swDocumentTypes_e.swDocPART) Then
                                                                  ext = ext + "PRT"
                                                           ElseIf (swModdoc.GetType = SwConst.swDocumentTypes_e.swDocDRAWING) Then
                                                                  ext = ext + "DRW"
                                                           Else
                                                                 ext = InputBox("enter extention")
                                                         End If
                                                         CommonDialog1.FileName = "partNumber" + ext
                                                         CommonDialog1.CancelError = True
                                                         CommonDialog1.ShowSave
                                                           If Err Then
                                                               MsgBox ("Save Canceled")
                                                               Exit Sub

                                                           End If
                                                           MsgBox ("Saving")
                                                          swModdoc.SaveAs (CommonDialog1.FileName)

                                                      End Sub


                                    • Re: Save As File name based on custom property
                                      Stephen Ball

                                      Hi all,

                                       

                                      I have been working on making the script a little more robust.  I have included checks to see if a file already exists before saving it (otherwise, the script would simply overwrite the existing file without notice!).  I have done this for the case of pulling a 5 digit part number stored in the "Number" custom property field.  Anyway, I hope that this is useful to some of you!  I had a lot of help making the base of this code, so I thought I'd share the result.

                                       

                                      Stephen

                                      _______________________________________________

                                       

                                      Dim CommonDialog1 As Object

                                      Dim swapp As SldWorks.SldWorks

                                      Dim swModdoc As SldWorks.ModelDoc2

                                      Dim ext As String

                                       

                                      Public Sub main()

                                          Set swapp = Application.SldWorks

                                          Set swModdoc = swapp.ActiveDoc

                                          Set CommonDialog1 = CreateObject("MSComDlg.CommonDialog")

                                          Set Checker = CreateObject("Scripting.FileSystemObject")

                                       

                                          On Error Resume Next

                                       

                                          'Define the file extension string based on the type of the current file

                                          If (swModdoc.GetType = SwConst.swDocumentTypes_e.swDocASSEMBLY) Then

                                              ext = ".SLDASM"

                                          ElseIf (swModdoc.GetType = SwConst.swDocumentTypes_e.swDocPART) Then

                                              ext = ".SLDPRT"

                                          ElseIf (swModdoc.GetType = SwConst.swDocumentTypes_e.swDocDRAWING) Then

                                              ext = ".SLDDRW"

                                          Else

                                              ext = InputBox("enter extention")

                                          End If

                                       

                                       

                                          'Use this....To Get the value from the Properties in the CUSTOM tab

                                          partnumber = swModdoc.CustomInfo("Number")

                                          'Or this....To Get the value from the Configuration Specific Tab

                                          'partnumber = SWmoddoc.GetCustomInfoValue("Default", "Number")

                                       

                                      SaveWindow:

                                       

                                          'Create the complete string to be used as the filename in the save dialog

                                          CommonDialog1.FileName = partnumber + ext

                                       

                                          'Check (in the current working directory) if a file (part, assembly or drawing) exists with the current part number

                                          If Checker.FileExists(partnumber + ".SLDPRT") = True Or Checker.FileExists(partnumber + ".SLDASM") = True Or Checker.FileExists(partnumber + ".SLDDRW") = True Then

                                              'Pose a message to the user noting that the part number is already in use

                                              MsgBox ("A file with the part number '" + partnumber + "' already exists.  Please enter a new part number in the Number custom property field and try again")

                                              'If this message occurs, it means that the Number cannot be used, so abort the save so that the user must supply a new part number.

                                              Exit Sub

                                          End If

                                       

                                          'Cause an error if the save is cancelled from the save dialog (otherwise the save will happen even if the cancel button is clicked)

                                          CommonDialog1.CancelError = True

                                       

                                          'Show the save dialog box

                                          CommonDialog1.ShowSave

                                       

                                          'Check if the cancel button is clicked

                                          If Err Then

                                              'If cancel button clicked, then abort the save (i.e. do nothing) and show a message saying that the save is cancelled

                                              MsgBox ("Save Canceled")

                                              Exit Sub

                                          End If

                                       

                                          'If the user enters a new part number in the field, remember which part number is entered (need to strip the path and extension from the filename)

                                          editedpartnumber = Left(Right(CommonDialog1.FileName, 12), 5)

                                       

                                          'Check (in the current working directory) if a file (part, assembly or drawing) exists with the part number entered into the save dialog

                                          If Checker.FileExists(editedpartnumber + ".SLDPRT") = True Or Checker.FileExists(editedpartnumber + ".SLDASM") = True Or Checker.FileExists(editedpartnumber + ".SLDDRW") = True Then

                                              'Pose a message to the user noting that the part number is already in use.  This step will not occur if the default file name is used (i.e. it will occur only if the user changes the file name).  Direct the user to enter a new part number or accept the default

                                              MsgBox ("A file with the part number  '" + editedpartnumber + "'  already exists.  You cannot rename the file to this part number.  Use the default number (obtained from the Number custom property field) or enter a number that does not exist.")

                                              'Go back to the default file name in the save dialog.

                                              GoTo SaveWindow

                                          End If

                                       

                                          'Save the file with the file name displayed in the save dialog

                                          swModdoc.SaveAs (CommonDialog1.FileName)

                                       

                                      End Sub

                                        • Re: Save As File name based on custom property
                                          Charles Culp

                                          I'm glad to see you got a good result!

                                          • Re: Save As File name based on custom property
                                            John Lindemann

                                            I'm not sure if this is what I am looking for or not.  I have done some programming in Excel VBA but never in SW.  All I am trying to do is when I save a file for the first time I would like it to fill in the file name with the custom property fields of "Part Number" then a space and then "Description" 

                                             

                                            This is getting picky but in our Custom Properties the text is all caps, I'd like the description to be lower case.  If that can't happen I'd prefer the file name to just be the part number and I'll fill in the description.

                                             

                                            I don't want it to save automatically but rather just to bring up the save window so I can make sure I don't overwrite any files and that the file name is how I want it.

                                             

                                             

                                            Thanks guys

                                            • Re: Save As File name based on custom property
                                              John Lindemann

                                              Stephen,

                                                I copied your code but I'm not able to get it to work.  I attached the macro as I am trying to use it.

                                               

                                              It is giving me the error: "Run-time error '429': ActiveX component can't create object"

                                              • Re: Save As File name based on custom property
                                                JOHN GEORGE

                                                I tried to run this macro and getting an error

                                                 

                                                Run-time error  '429:'

                                                ActiveX component can't create object

                                                 

                                                Am I doing something wrong.

                                                Do I need to create any custom properties prior to this?

                                                 

                                                Thanks,

                                                  • Re: Save As File name based on custom property
                                                    Deepak Gupta

                                                    Yes the property listed in the macro should be available in the file properties.

                                                      • Re: Save As File name based on custom property
                                                        JOHN GEORGE

                                                        Thanks Deepak,

                                                         

                                                        I am still getting the "Run-time error"

                                                        I am using SW2011 on Windows7x64

                                                         

                                                        I don't have problem running other macros

                                                         

                                                        Regards,

                                                          • Re: Save As File name based on custom property
                                                            Deepak Gupta

                                                            Which codes you're using?

                                                             

                                                            Open macro (via Edit macro option) and start pressing F8 (debugging). Then you can find out which line in the code is making trouble.

                                                              • Re: Save As File name based on custom property
                                                                JOHN GEORGE

                                                                Deepak,

                                                                 

                                                                Macro file attached.

                                                                 

                                                                Getting error on the following line when debugging.

                                                                 

                                                                Set CommonDialog1 = CreateObject("MSComDlg.CommonDialog")

                                                                 

                                                                Thanks,

                                                                  • Re: Save As File name based on custom property
                                                                    Deepak Gupta

                                                                    Can you post the macro code in a text file or simple copy paste the codes here (since I don't have SW installed to open the macro)

                                                                      • Re: Save As File name based on custom property
                                                                        JOHN GEORGE

                                                                        It's the same code from Stephen.

                                                                         

                                                                        Dim CommonDialog1 As Object

                                                                        Dim swapp As SldWorks.SldWorks

                                                                        Dim swModdoc As SldWorks.ModelDoc2

                                                                        Dim ext As String

                                                                         

                                                                        Public Sub main()

                                                                            Set swapp = Application.SldWorks

                                                                            Set swModdoc = swapp.ActiveDoc

                                                                            Set CommonDialog1 = CreateObject("MSComDlg.CommonDialog")

                                                                            Set Checker = CreateObject("Scripting.FileSystemObject")

                                                                         

                                                                            On Error Resume Next

                                                                         

                                                                            'Define the file extension string based on the type of the current file

                                                                            If (swModdoc.GetType = SwConst.swDocumentTypes_e.swDocASSEMBLY) Then

                                                                                ext = ".SLDASM"

                                                                            ElseIf (swModdoc.GetType = SwConst.swDocumentTypes_e.swDocPART) Then

                                                                                ext = ".SLDPRT"

                                                                            ElseIf (swModdoc.GetType = SwConst.swDocumentTypes_e.swDocDRAWING) Then

                                                                                ext = ".SLDDRW"

                                                                            Else

                                                                                ext = InputBox("enter extention")

                                                                            End If

                                                                         

                                                                         

                                                                            'Use this....To Get the value from the Properties in the CUSTOM tab

                                                                            partnumber = swModdoc.CustomInfo("Number")

                                                                            'Or this....To Get the value from the Configuration Specific Tab

                                                                            'partnumber = SWmoddoc.GetCustomInfoValue("Default", "Number")

                                                                         

                                                                        SaveWindow:

                                                                         

                                                                            'Create the complete string to be used as the filename in the save dialog

                                                                            CommonDialog1.FileName = partnumber + ext

                                                                         

                                                                            'Check (in the current working directory) if a file (part, assembly or drawing) exists with the current part number

                                                                            If Checker.FileExists(partnumber + ".SLDPRT") = True Or Checker.FileExists(partnumber + ".SLDASM") = True Or Checker.FileExists(partnumber + ".SLDDRW") = True Then

                                                                                'Pose a message to the user noting that the part number is already in use

                                                                                MsgBox ("A file with the part number '" + partnumber + "' already exists.  Please enter a new part number in the Number custom property field and try again")

                                                                                'If this message occurs, it means that the Number cannot be used, so abort the save so that the user must supply a new part number.

                                                                                Exit Sub

                                                                            End If

                                                                         

                                                                            'Cause an error if the save is cancelled from the save dialog (otherwise the save will happen even if the cancel button is clicked)

                                                                            CommonDialog1.CancelError = True

                                                                         

                                                                            'Show the save dialog box

                                                                            CommonDialog1.ShowSave

                                                                         

                                                                            'Check if the cancel button is clicked

                                                                            If Err Then

                                                                                'If cancel button clicked, then abort the save (i.e. do nothing) and show a message saying that the save is cancelled

                                                                                MsgBox ("Save Canceled")

                                                                                Exit Sub

                                                                            End If

                                                                         

                                                                            'If the user enters a new part number in the field, remember which part number is entered (need to strip the path and extension from the filename)

                                                                            editedpartnumber = Left(Right(CommonDialog1.FileName, 12), 5)

                                                                         

                                                                            'Check (in the current working directory) if a file (part, assembly or drawing) exists with the part number entered into the save dialog

                                                                            If Checker.FileExists(editedpartnumber + ".SLDPRT") = True Or Checker.FileExists(editedpartnumber + ".SLDASM") = True Or Checker.FileExists(editedpartnumber + ".SLDDRW") = True Then

                                                                                'Pose a message to the user noting that the part number is already in use.  This step will not occur if the default file name is used (i.e. it will occur only if the user changes the file name).  Direct the user to enter a new part number or accept the default

                                                                                MsgBox ("A file with the part number  '" + editedpartnumber + "'  already exists.  You cannot rename the file to this part number.  Use the default number (obtained from the Number custom property field) or enter a number that does not exist.")

                                                                                'Go back to the default file name in the save dialog.

                                                                                GoTo SaveWindow

                                                                            End If

                                                                         

                                                                            'Save the file with the file name displayed in the save dialog

                                                                            swModdoc.SaveAs (CommonDialog1.FileName)

                                                                         

                                                                        End Sub

                                                                         

                                                                        I opened a part in SW and gave property name "partnumber" and run this macro.

                                                              • Re: Save As File name based on custom property
                                                                Stephen Ball

                                                                Hi there,

                                                                 

                                                                I'm really no expert on programming anything, but this error message I've seen before on my colleague's machine when he tried to run my Macro.  If memory serves me correctly, the reason it worked fine for me and not him was that I had previously installed (for another reason) MS Visual Studio.  Once he installed this on his machine, it worked fine.  I guess I must have written this to include a library that came with Visual Studio.  Luckily, there is a free download of this.... I can't remember which one he installed, but all these downloads are available from the Microsoft website.  You need to choose the express version which is free.

                                                                 

                                                                Good Luck!

                                                                 

                                                                Stephen

                                                                  • Re: Save As File name based on custom property
                                                                    JOHN GEORGE

                                                                    Stephen,

                                                                     

                                                                    Thanks for the suggestion.

                                                                    I installed VB express 2008 and finally it is working now.

                                                                     

                                                                    I just have another question,

                                                                    I can make the "Number" property either in Custom or Configuration Specfic

                                                                    Can someone please help me to modify the code to pick the "Number" from one of these.

                                                                     

                                                                    Thanks,

                                                                      • Re: Save As File name based on custom property
                                                                        Stephen Ball

                                                                        Hi there,

                                                                         

                                                                        I posted a solution (I think) to this earlier on in this thread...

                                                                         

                                                                        While I have never used the configuration specific tab, I believe the code I posted should work.

                                                                         

                                                                        Stephen

                                                                          • Re: Save As File name based on custom property
                                                                            Mark Thomas

                                                                            Sorry to reopen an old thread....

                                                                             

                                                                            I have this macro working well with saving as custom property "Part No", but would like to know how to modify the code so it auto fills "Part No." then "_" and then another custom property "Description"?

                                                                             

                                                                            So then i would have for example "100526_large outer plate" in the save as dialog box.

                                                                             

                                                                            Glad of any help,my knowledge of vba is very limited!!

                                                                             

                                                                            Thanks,

                                                                            Mark

                                                                              • Re: Save As File name based on custom property
                                                                                Deepak Gupta

                                                                                Try this

                                                                                 

                                                                                Sub main()

                                                                                Dim swApp As SldWorks.SldWorks

                                                                                Dim SWmoddoc As SldWorks.ModelDoc2

                                                                                Dim partnumber As String

                                                                                Dim Description As String

                                                                                 

                                                                                Set swApp = Application.SldWorks

                                                                                Set SWmoddoc = swApp.ActiveDoc

                                                                                'Use this....To Get the value from the Properties in the CUSTOM tab

                                                                                partnumber = SWmoddoc.CustomInfo("Part No.")

                                                                                Description = SWmoddoc.CustomInfo("Description")

                                                                                 

                                                                                'Or this....To Get the value from the Configuration Specific Tab

                                                                                'partnumber = SWmoddoc.GetCustomInfoValue("Default", "Part No.")

                                                                                'Description = SWmoddoc.GetCustomInfoValue("Default", "Description")

                                                                                 

                                                                                PathName = SWmoddoc.GetPathName     'this gets the full path of the current open document (including filename)

                                                                                Filepath = Left(PathName, InStrRev(PathName, "\"))  'uses to full pathname to get the folder path, to be added to the save as file name

                                                                                If (SWmoddoc.GetType = swDocASSEMBLY) Then

                                                                                    SWmoddoc.SaveAs (Filepath + partnumber + "_" +  Description + ".sldasm")

                                                                                 

                                                                                ElseIf (SWmoddoc.GetType = swDocPART) Then

                                                                                     SWmoddoc.SaveAs (Filepath + partnumber + "_" +  Description +  ".sldprt")

                                                                                 

                                                                                End If

                                                                                End Sub

                                                                                  • Re: Save As File name based on custom property
                                                                                    Mark Thomas

                                                                                    Thanks for the reply Deepak,

                                                                                     

                                                                                    This is what i am currently using, can you modify it?

                                                                                     

                                                                                    Dim CommonDialog1 As Object

                                                                                    Dim swapp As SldWorks.SldWorks

                                                                                    Dim swModdoc As SldWorks.ModelDoc2

                                                                                    Dim ext As String


                                                                                    Public Sub main()

                                                                                        Set swapp = Application.SldWorks

                                                                                        Set swModdoc = swapp.ActiveDoc

                                                                                        Set CommonDialog1 = CreateObject("MSComDlg.CommonDialog")

                                                                                        Set Checker = CreateObject("Scripting.FileSystemObject")


                                                                                        On Error Resume Next


                                                                                        'Define the file extension string based on the type of the current file

                                                                                        If (swModdoc.GetType = SwConst.swDocumentTypes_e.swDocASSEMBLY) Then

                                                                                            ext = ".SLDASM"

                                                                                        ElseIf (swModdoc.GetType = SwConst.swDocumentTypes_e.swDocPART) Then

                                                                                            ext = ".SLDPRT"

                                                                                        ElseIf (swModdoc.GetType = SwConst.swDocumentTypes_e.swDocDRAWING) Then

                                                                                            ext = ".SLDDRW"

                                                                                        Else

                                                                                            ext = InputBox("enter extention")

                                                                                        End If



                                                                                        'Use this....To Get the value from the Properties in the CUSTOM tab

                                                                                        partnumber = swModdoc.GetCustomInfoValue("Default", "Part No")

                                                                                        'Or this....To Get the value from the Configuration Specific Tab

                                                                                        'partnumber = SWmoddoc.GetCustomInfoValue("Default", "Number")


                                                                                    SaveWindow:


                                                                                        'Create the complete string to be used as the filename in the save dialog

                                                                                        CommonDialog1.FileName = partnumber + ext


                                                                                        'Check (in the current working directory) if a file (part, assembly or drawing) exists with the current part number

                                                                                        If Checker.FileExists(partnumber + ".SLDPRT") = True Or Checker.FileExists(partnumber + ".SLDASM") = True Or Checker.FileExists(partnumber + ".SLDDRW") = True Then

                                                                                            'Pose a message to the user noting that the part number is already in use

                                                                                            MsgBox ("A file with the part number '" + partnumber + "' already exists.  Please enter a new part number in the Number custom property field and try again")

                                                                                            'If this message occurs, it means that the Number cannot be used, so abort the save so that the user must supply a new part number.

                                                                                            Exit Sub

                                                                                        End If


                                                                                        'Cause an error if the save is cancelled from the save dialog (otherwise the save will happen even if the cancel button is clicked)

                                                                                        CommonDialog1.CancelError = True


                                                                                        'Show the save dialog box

                                                                                        CommonDialog1.ShowSave


                                                                                        'Check if the cancel button is clicked

                                                                                        If Err Then

                                                                                            'If cancel button clicked, then abort the save (i.e. do nothing) and show a message saying that the save is cancelled

                                                                                            MsgBox ("Save Canceled")

                                                                                            Exit Sub

                                                                                        End If


                                                                                        'If the user enters a new part number in the field, remember which part number is entered (need to strip the path and extension from the filename)

                                                                                        editedpartnumber = Left(Right(CommonDialog1.FileName, 12), 5)


                                                                                        'Check (in the current working directory) if a file (part, assembly or drawing) exists with the part number entered into the save dialog

                                                                                        If Checker.FileExists(editedpartnumber + ".SLDPRT") = True Or Checker.FileExists(editedpartnumber + ".SLDASM") = True Or Checker.FileExists(editedpartnumber + ".SLDDRW") = True Then

                                                                                            'Pose a message to the user noting that the part number is already in use.  This step will not occur if the default file name is used (i.e. it will occur only if the user changes the file name).  Direct the user to enter a new part number or accept the default

                                                                                            MsgBox ("A file with the part number  '" + editedpartnumber + "'  already exists.  You cannot rename the file to this part number.  Use the default number (obtained from the Number custom property field) or enter a number that does not exist.")

                                                                                            'Go back to the default file name in the save dialog.

                                                                                            GoTo SaveWindow

                                                                                        End If


                                                                                        'Save the file with the file name displayed in the save dialog

                                                                                        swModdoc.SaveAs (CommonDialog1.FileName)


                                                                                    End Sub


                                                                                    Thanks,

                                                                                    Mark

                                                                                    • Re: Save As File name based on custom property
                                                                                      Shail Patel

                                                                                      Hello Deepak,

                                                                                       

                                                                                      Could you please modify the code for me to save the part in a assigned location or a dialog box to specify the location instead of reading the current location and saving it in the same directory?

                                                                                       

                                                                                      Thanks.

                                                                          • Re: Save As File name based on custom property
                                                                            Stephen Ball

                                                                            Hi everyone.  It has been a while since I have had to revisit this code, but we are upgrading to Soldiworks 2013 on Windows 7 (x64), and can no longer seem to run this macro.  It seems to get stuck on the line:

                                                                             

                                                                                Set CommonDialog1 = CreateObject("MSComDlg.CommonDialog")

                                                                             

                                                                            I seem to have read that this is not available in Windows 7.  However, I have installed the MS VB Express and the comdlg32.ocx file is present in the right place.  And I have registered the file as suggested by many.  It still will not run.

                                                                             

                                                                            I just need a way to access the save dialog and enter the appropriate info into the filename field.  I really am not a programmer, so I don't have any thoughts about how to fix this.

                                                                             

                                                                            Any thoughts on the best way to run this?

                                                                             

                                                                            Thanks!

                                                                          • Re: Save As File name based on custom property
                                                                            Markku Lehtola

                                                                            Hi,

                                                                             

                                                                            New version and new website for SWit:

                                                                             

                                                                            www.sloworks.fi

                                                                             

                                                                            Can now create file names with attributes and does the saving as well

                                                                            • Re: Save As File name based on custom property
                                                                              Samu Niveri

                                                                              How about using "SW-File Name" link in your custom property? In that way your partnumber will be updated to properties based on filename and you have to worry only about getting the number from some file or database before Save As.

                                                                               

                                                                              http://www.spn-technologies.fi