31 Replies Latest reply on Sep 27, 2017 8:13 AM by Domagoj Brlosic

    How to create macro for replacing part of sentence in custom properties

    Damian Fifielski

      Hi all.

      I have a problem and all the time i am doing it manually. I am losing a lot of time for this so maybe there is faster way to do it. I have found similar subject but without final solution: Drawing Custom Properties Find & Replace. I have tried to edit this macro and run it but without any result.

       

      I need macro for finding sentence "-_" and replacing with "-XXX_" in custom properties (*.SLDPRT and *.SLDASM files). It would be nice to update the date ("Design Date" in custom property) also but it is not necessary. The XXX is always different for next assembly. I make few assemblies per day. I attached one file with custom proparties for tests.

       

      Here is an example:

       

      Before change:     Custom propertie name: "Part number", Value: "NC-G-05-_318"

      After change:        Custom propertie name: "Part number", Value: "NC-G-05-308_318"

       

      Is it possible? How to make it? And finally: how to use it for all files in folder (with subfolders)?

       

      PS. I am using SolidWorks Professional 2015 SP5.0

        • Re: How to create macro for replacing part of sentence in custom properties
          JOHN GEORGE

          Damian,

          There are few macros available in this forum, if you do a search

          Here is one of them to get started and you can modify to suit your requirement

          See this discussion

            • Re: How to create macro for replacing part of sentence in custom properties
              Damian Fifielski

              Thanks for answer.

               

              Unfortunately, as I understand, this macro is for deleting and adding new custom property. It is not what I need, because I have many parts in assembly and every one have different value of "Part number". For example:

               

              Part1 - "Part number"="NC-G-05-_318"

              Part2 - "Part number"="NC-G-05-_319"

              Part3 - "Part number"="NC-G-05-_320"

               

              I need to keep it because this is actually a part number. "NC-G-05" is a product type and between "-_" I need to put product number.

                • Re: How to create macro for replacing part of sentence in custom properties
                  Damian Fifielski

                  So far I have this macro but there are functions which I don't need (change file name) and I don't know what can I delete.  I am completely green on it. It doesn't work or maybe I am using it in wrong way (open part, TOOLS/MACRO/RUN)?

                   

                  Option Explicit

                  Dim swApp                     As SldWorks.SldWorks

                  Dim swModel                As ModelDoc2

                  Dim swModelDocExt      As ModelDocExtension

                  Dim swCustProp           As CustomPropertyManager

                  Dim val          As String

                  Dim val_temp           As String

                  Dim valout          As String

                  Dim bool            As Boolean

                  Dim sFileName       As String

                  Dim silent          As Boolean

                  Dim Path            As String

                  Dim swConfig        As String

                  Dim nErrors         As Long

                  Dim nWarnings       As Long

                  Dim Fold            As String

                  Dim FileName        As String

                  Dim FileType        As String

                  Dim old_number      As String

                  Dim new_number      As String

                  Sub main()

                  Set swApp = Application.SldWorks

                   

                  Fold = "C:\Users\daf\Desktop\"       'Directory where parts are

                  FileType = "*.sldprt*"                        'File extension of drawings

                  old_number = "-_"                             'Expression to replace in custom properties

                  new_number = "-XXX_"                   'Replace it with..

                   

                  FileName = Dir(Fold & FileType)

                   

                  Do While FileName <> ""

                  Debug.Print FileName

                      Set swModel = swApp.OpenDoc6(Fold + FileName, swDocPART, swOpenDocOptions_Silent, "", nErrors, nWarnings)

                      Set swModel = swApp.ActiveDoc

                      Set swModelDocExt = swModel.Extension

                   

                      Set swCustProp = swModelDocExt.CustomPropertyManager("")

                      bool = swCustProp.Get4("Part Number", False, val, valout)

                      val_temp = Replace(val, old_number, new_number)

                      swModel.AddCustomInfo3 "", "Part Number", swCustomInfoText, val_temp

                      swModel.ForceRebuild3 False

                      swModel.Save2 silent

                      swApp.CloseDoc swModel.GetTitle

                      Set swModel = Nothing

                      FileName = Dir

                   

                  Loop

                  End Sub

                    • Re: How to create macro for replacing part of sentence in custom properties
                      Deepak Gupta

                      Damian try these codes on active file.

                       

                      Option Explicit
                      Dim swApp           As SldWorks.SldWorks
                      Dim swModel         As ModelDoc2
                      Dim swCustProp      As CustomPropertyManager
                      Dim val             As String
                      Dim valout          As String
                      Const old_number    As String = "_"
                      Const new_number    As String = "308" 'Replace value here
                      
                      Sub main()
                      Set swApp = Application.SldWorks
                      Set swModel = swApp.ActiveDoc
                      
                      Set swCustProp = swModel.Extension.CustomPropertyManager("")
                      swCustProp.Get4 "Part Number", False, val, valout
                      swCustProp.Set "Part Number", Replace(val, old_number, new_number & "-")
                      
                      End Sub
                      

                       

                      And if they work then you can update them for batch running either using the codes you've posted above OR using #TASK

                       

                      Here are codes in case you want to do batch run

                       

                      Option Explicit
                      Dim swApp               As SldWorks.SldWorks
                      Dim swModel             As ModelDoc2
                      Dim swCustProp          As CustomPropertyManager
                      Dim val                 As String
                      Dim valout              As String
                      Dim sFileName           As String
                      Dim Path                As String
                      Dim nErrors             As Long
                      Dim nWarnings           As Long
                      Const old_number        As String = "_"
                      Const new_number        As String = "308" 'Repalce value here
                      Sub main()
                      
                      Set swApp = Application.SldWorks
                      Path = "C:\Users\daf\Desktop\" ' Change path here, Make sure you include \ at end of the path
                      
                      sFileName = Dir(Path & "*.sldprt")
                      Do Until sFileName = ""
                      Set swModel = swApp.OpenDoc6(Path + sFileName, swDocPART, swOpenDocOptions_Silent, "", nErrors, nWarnings)
                      Set swModel = swApp.ActiveDoc
                       Set swCustProp = swModel.Extension.CustomPropertyManager("")
                      
                      swCustProp.Get4 "Part Number", False, val, valout
                      swCustProp.Set "Part Number", Replace(val, old_number, new_number & "-")
                      
                      swModel.Save3 swSaveAsOptions_Silent, nErrors, nWarnings
                      swApp.CloseDoc swModel.GetTitle
                      Set swModel = Nothing
                      sFileName = Dir
                      Loop
                      End Sub
                      
                        • Re: How to create macro for replacing part of sentence in custom properties
                          Damian Fifielski

                          Yes, it work fine! Thank you!!!

                          I am in a half of way. Unfortunately I don't know what next.

                           

                          1) I would like to get it work with parts and assemblies. I tried to add & "*.sldasm" on the end of 18th line but then macro doesn't even start. I have also changed it a little bit to have "-_" instead of "_" (I have "_" in other parts where I don't want to change anything). Now code look like this (changes in red color):

                           

                          1. Option Explicit 
                          2. Dim swApp               As SldWorks.SldWorks 
                          3. Dim swModel             As ModelDoc2 
                          4. Dim swCustProp          As CustomPropertyManager 
                          5. Dim val                 As String 
                          6. Dim valout              As String 
                          7. Dim sFileName           As String 
                          8. Dim Path                As String 
                          9. Dim nErrors             As Long 
                          10. Dim nWarnings           As Long 
                          11. Const old_number        As String = "-_" 
                          12. Const new_number        As String = "308" 'Repalce value here 
                          13. Sub main() 
                          14.  
                          15. Set swApp = Application.SldWorks 
                          16. Path = "C:\Users\daf\Desktop\" ' Change path here, Make sure you include \ at end of the path 
                          17.  
                          18. sFileName = Dir(Path & "*.sldprt"
                          19. Do Until sFileName = "" 
                          20. Set swModel = swApp.OpenDoc6(Path + sFileName, swDocPART, swOpenDocOptions_Silent, "", nErrors, nWarnings) 
                          21. Set swModel = swApp.ActiveDoc 
                          22. Set swCustProp = swModel.Extension.CustomPropertyManager(""
                          23.  
                          24. swCustProp.Get4 "Part Number", False, val, valout 
                          25. swCustProp.Set "Part Number", Replace(val, old_number, "-" & new_number & "_"
                          26.  
                          27. swModel.Save3 swSaveAsOptions_Silent, nErrors, nWarnings 
                          28. swApp.CloseDoc swModel.GetTitle 
                          29. Set swModel = Nothing 
                          30. sFileName = Dir 
                          31. Loop 
                          32. End Sub

                           

                          2) As I have noticed it work only for folder. It doesn't search for other parts in subfolders. Can I put more paths in code or use some other trick to force it to work in subfolders to?

                           

                          Thank you very much one more time!

                          • Re: How to create macro for replacing part of sentence in custom properties
                            Domagoj Brlosic

                            Thank you for this macro, it helps me a lot to. I would just like to add one more thing. Can in this macro be put code so Configuration Specific properties are changed also. (all configuraions)?

                             

                            Thank you in advance

                             

                            EDIT.

                             

                            Now I see it is in the last post. Thank you once again

                    • Re: How to create macro for replacing part of sentence in custom properties
                      Damian Fifielski

                      I think Document Manager API is too complicated for me (macros too btw). I am simple Design engineer, not a programmer I think I will stay with manually changes. Thank you for help

                      • Re: How to create macro for replacing part of sentence in custom properties
                        Damian Fifielski

                        Hello.

                         

                        Does anybody have any idea how to make it? I mean: change values in unsuppressed subassemblies and parts of main assembly?

                         

                        I am really tired of doing it manually but I can't do the macro on myself - I don't know how. If anyone will be so kind and make a code, I will be very grateful. Thanks in advance.

                          • Re: How to create macro for replacing part of sentence in custom properties
                            Deepak Gupta

                            How would you control different number for different components? And how to make sure that value is correct?

                            • Re: How to create macro for replacing part of sentence in custom properties
                              Solid Air

                              Try the following with the upper level assembly open.  All the component files must not be read only and you will need to save when complete.

                               

                              Dim swApp As SldWorks.SldWorks

                              Dim swDoc As SldWorks.ModelDoc2

                              Dim swCustProp As SldWorks.CustomPropertyManager

                              Dim swAsy As SldWorks.AssemblyDoc

                              Dim swComp As SldWorks.Component2

                              Dim AsyComps As Variant

                              Dim AsyComp As Variant

                              Dim val As String

                              Dim valout As String

                               

                              Sub main()

                               

                                  Set swApp = Application.SldWorks

                                  Set swAsy = swApp.ActiveDoc

                                 

                                  AsyComps = swAsy.GetComponents(True)

                                 

                                  For Each AsyComp In AsyComps

                                 

                                      Set swComp = AsyComp

                                     

                                      If swComp.IsSuppressed = False Then

                                     

                                          Set swDoc = swComp.GetModelDoc2

                                         

                                          Set swCustProp = swDoc.Extension.CustomPropertyManager("")

                               

                                          swCustProp.Get4 "Part Number", False, val, valout

                                         

                                          swCustPropMgr.Set "Part Number", Replace(val, "-_", "-308_")

                                         

                                      End If

                                         

                                  Next

                                 

                              End Sub

                            • Re: How to create macro for replacing part of sentence in custom properties
                              Damian Fifielski

                              Deepak Gupta

                               

                              It is easy to control. All my parts have part number like this:

                              Part 1: "NC-G-05-308_301",

                              Part 2: "NC-G-05-308_302",

                              Part 3: "NC-G-05-308_303".

                               

                              As you see all parts have same value "-308_". After change of this value I make BOM list in excel so I will see is everything ok with numbers.

                               

                              @Solid Air

                               

                              Thanks for the code! Unfortunately it doesn't work. I get Microsoft Visual Basic error:

                               

                              "Run-time error '424':

                              Object required"

                               

                              When I click "Debug" button it mark as yellow this line:

                              swCustPropMgr.Set "Part number", Replace(val, "-_", "-308")

                               

                              Any idea why?