12 Replies Latest reply on Apr 29, 2015 10:19 AM by James Kashuba

    How to remove specific custom properties

    James Kashuba

      Good Morning Folks,

      I am trying to remove several custom properties from a number of files with in a folder. I do not want all the custom props removed only specific ones so the macro would have to be edited by me to fill in the blanks. Could any help me out with this?

       

      Thank you all,

      Jim

        • Re: How to remove specific custom properties
          Steven Richmond

          Hi James

           

          The one I use to delete custom properties goes like this

           

           

          Option Explicit

              Dim swApp              As SldWorks.SldWorks

              Dim swModel            As SldWorks.ModelDoc2

              Dim swCustPropMgr      As SldWorks.CustomPropertyManager

              Dim strVal              As String

              Dim Value              As String

            

          Private Sub Start1()

           

              Set swApp = Application.SldWorks

              Set swModel = swApp.ActiveDoc

           

              End If

           

          End Sub

           

          Sub main()

           

              Set swApp = Application.SldWorks

              Set swModel = swApp.ActiveDoc

           

           

              If swModel Is Nothing Then MsgBox "A Part or Assembly file must be open", vbExclamation: End

              Set swCustPropMgr = swModel.Extension.CustomPropertyManager(Empty)

          '------------------------------------------------------------------------------------------------------------------

          Set swCustPropMgr = swModel.Extension.CustomPropertyManager("")

           

           

                

          swCustPropMgr.Delete "Finish"

                        

          MsgBox " Custom Property data updated."

          End

           

          End Sub

           

           

          The " swCustPropMgr.Delete "Finish" " section is the bit where the custom property is removed, so change "Finish" for whatever custom property you have. Copy and paste below for as many properties you want to change. You could then run this through task scheduler to run over a bunch of files located in a folder rather than individually doing it.

           

          Hope this helps

           

          Steven

            • Re: How to remove specific custom properties
              James Kashuba

              Steven,

              thank you so much! I wish I understood all that the code was doing, but regardless - it works just the way I wanted it to.

               

              Thanks again and have a great day!

               

              Jim

              • Re: How to remove specific custom properties
                James Kashuba

                Steven,

                The Task Scheduler is not as easy as I thought. I figured I'd set the program (SW), choose the macro, and then choose the folder location that contains all the files that need updating. The Scheduler doesn't seem to provide for a folder location. That being the case, how does the macro know what files to update? Obviously I'm missing something, can you please point out what it is that I'm missing?

                 

                Thanks again

                    • Re: How to remove specific custom properties
                      James Kashuba

                      Steven,

                      Would you mind using your code injunction with the code that you would have to write to open all files in a given folder to be used in Task Scheduler? I have been trying since early this morning to make this work and it's become clear to me that I'm just wasting my time. I read the tutorial but I need more tutoring. So what I'm asking is can you provide the exact code you would use to do this task in TS, and I'll manipulate the parameters to suite my machine?

                       

                      Thanks again,

                        • Re: How to remove specific custom properties
                          Steven Richmond

                          James

                           

                          Try running this macro from within Solidworks to bypass Task Schedular as Deepak Gupta recommends in his response.

                           

                          Dim swApp As SldWorks.SldWorks

                          Dim swModel As SldWorks.ModelDoc2

                          Const strFolderPath As String = "C:\Users\srichmond\Desktop\Test\"

                          Dim strFileName As String

                          Sub main()

                              Set swApp = Application.SldWorks

                              strFileName = Dir(strFolderPath & "*.SLDPRT")

                              While strFileName <> ""

                                  Set swModel = swApp.OpenDoc6(strFolderPath & strFileName, _

                                      swDocPART, 1, Empty, Empty, Empty)

                                  Set swCustPropMgr = swModel.Extension.CustomPropertyManager("")

                                  swCustPropMgr.Delete "NewPartNo"

                                  swModel.Save3 1, Empty, Empty

                                  swApp.QuitDoc swModel.GetTitle

                                  strFileName = Dir

                              Wend

                              swApp.ExitApp

                          End Sub

                          When you change the file location (3rd line down) to where you want it to be, remember and add the \ to the end of the path.

                           

                          Change "NewPartNo" to the property of your choice. I've trialled it on 3 parts that had part numbers and now they don't so it works for me.

                           

                          Hope this helps

                          Steven

                           

                           

                            • Re: How to remove specific custom properties
                              James Kashuba

                              That worked Steven! Thank you for all your help, and Deepak is always a great resource for me. I wish I had a fraction of the API knowledge as he does.

                               

                              Because this is such a powerful tool (the gears are turning) I have to ask one more question. Can you add properties the same way? And if you can, can they be added in such a way that they wouldn't override an existing property. In other words, if I have a 1000 files and 300 of those have the property I want to add already present, would it override its value?

                                • Re: How to remove specific custom properties
                                  Steven Richmond

                                  James

                                   

                                  Try this

                                  Dim swApp As SldWorks.SldWorks

                                  Dim swModel As SldWorks.ModelDoc2

                                  Const strFolderPath As String = "C:\Users\srichmond\Desktop\Test\"

                                  Dim strFileName As String

                                   

                                   

                                  Sub main()

                                      Set swApp = Application.SldWorks

                                      strFileName = Dir(strFolderPath & "*.SLDPRT")

                                      While strFileName <> ""

                                          Set swModel = swApp.OpenDoc6(strFolderPath & strFileName, _

                                              swDocPART, 1, Empty, Empty, Empty)

                                            

                                                     

                                          Set swCustPropMgr = swModel.Extension.CustomPropertyManager("")

                                          swCustPropMgr.Delete "NewPartNo"

                                          swCustPropMgr.Add2 "ProjectName", swCustomInfoText, "TESTING"

                                   

                                          swModel.Save3 1, Empty, Empty

                                          swApp.QuitDoc swModel.GetTitle

                                          strFileName = Dir

                                      Wend

                                      'swApp.ExitApp

                                  End Sub

                                   

                                  This is the line that has been added

                                  swCustPropMgr.Add2 "ProjectName", swCustomInfoText, "TESTING"

                                  This adds a custom property called "ProjectName" if it doesn't already exist.

                                   

                                  Change "ProjectName" to the custom property you want and "TESTING" to the value you want wrote to the custom property. I've tested it on 3 files, the first of which had "TEST" rather than "TESTING" for a value and it never over wrote it.

                                   

                                  regards

                                  Steven

                                    • Re: How to remove specific custom properties
                                      James Kashuba

                                      Steven, that was great! I didn't even think of adding and deleting in the same macro. This little macro is a game changer for some of the tasks I'm up against. I did notice one thing that I didn't catch the first time and that is it didn't update assembly files only part files. Does the strFileName control this?

                                        • Re: How to remove specific custom properties
                                          Steven Richmond

                                          Hi James

                                           

                                          Probably not the cleanest way to do it but I added a strFileName 2 for assemblies. The macro goes through the folder for parts first then goes back through for assemblies.

                                           

                                          Dim swApp As SldWorks.SldWorks

                                          Dim swModel As SldWorks.ModelDoc2

                                          Const strFolderPath As String = "C:\Users\srichmond\Desktop\Test\"

                                          Dim strFileName As String

                                          Dim strFileName2 As String

                                           

                                           

                                          Sub main()

                                              Set swApp = Application.SldWorks

                                              strFileName = Dir(strFolderPath & "*.SLDPRT")

                                                 

                                              While strFileName <> ""

                                                  Set swModel = swApp.OpenDoc6(strFolderPath & strFileName, _

                                                      swDocPART, 1, Empty, Empty, Empty)

                                                     

                                               

                                                  Set swCustPropMgr = swModel.Extension.CustomPropertyManager("")

                                                  swCustPropMgr.Delete "NewPartNo"

                                                  swCustPropMgr.Add2 "ProjectName", swCustomInfoText, "TESTING"

                                                                  

                                                

                                                  swModel.Save3 1, Empty, Empty

                                                  swApp.QuitDoc swModel.GetTitle

                                                  strFileName = Dir

                                              Wend

                                                

                                              Set swApp = Application.SldWorks

                                             

                                              strFileName2 = Dir(strFolderPath & "*.SLDASM")

                                             

                                              While strFileName2 <> ""

                                                        

                                                  Set swModel = swApp.OpenDoc6(strFolderPath & strFileName2, _

                                                      swDocASSEMBLY, 1, Empty, Empty, Empty)

                                                     

                                                  Set swCustPropMgr = swModel.Extension.CustomPropertyManager("")

                                                  swCustPropMgr.Delete "NewPartNo"

                                                  swCustPropMgr.Add2 "ProjectName", swCustomInfoText, "TESTING"

                                                                  

                                                

                                                  swModel.Save3 1, Empty, Empty

                                                  swApp.QuitDoc swModel.GetTitle

                                                  strFileName2 = Dir

                                              Wend

                                              

                                          End Sub

                                           

                                          As I say it works but probably not the cleanest way of doing it.

                                           

                                          regards

                                           

                                          Steven

                              • Re: How to remove specific custom properties
                                Deepak Gupta

                                Using TS is bit tricky but is simple once you understand it. Though you can update the macro using bunch of  batch file macros available on this forum.