16 Replies Latest reply on Sep 26, 2018 2:58 PM by Paul Ambler

    Solidworks 2016 Open PDM Files for Property Update?

    Paul Ambler


      I'm fairly well versed at VBA with MSAccess but not with Solidworks. Our company has standard PDM. I need to create a macro I can run with Task Scheduler that will open all files in a specific folder (code attached shows just one file as I am starting small) one by one, check the custom properties, check out the file if necessary, update the custom properties, then check the file out. I have thousands of files to run this on. I have already checked the properties update code and it works fine if you already have a model/assembly open. I need to make it work when the model/assembly is not open now.


      I'm hitting a wall. Here's my code:


      Dim swApp As SldWorks.SldWorks

      Dim swDoc As ModelDoc2

      'Dim files As Variant

      Dim files As String

      Dim folder As String

      Dim fileerror As Long

      Dim filewarning As Long

      Dim part As Object

      Dim model As SldWorks.ModelDoc2



      folder = "C:\JanisVault\2-Top Level Assembly Drawings & Non-Inventory\Solid Parts\56000-56999\"



      Set swApp = Application.SldWorks

      files = folder & "56672.sldprt"



      Dim cpm2 As CustomPropertyManager

      Dim cpm3 As CustomPropertyManager

      Dim names2() As String

      Dim names3() As String

      Dim copy1config As String

      Dim copy2config As String

      Dim confignami As String

      Dim confignamj As String

      Dim configvali As String

      Dim configvalj As String

      Dim i As Integer

      Dim j As Integer

      Dim NewPropOk As Boolean

      Dim NumProps As Long

      Dim NumProps2 As Long

      Dim swDocPART As Long

      Dim Propname As Variant

      Dim Proptype As Variant

      Dim Propvalue As Variant

      Dim Propname1 As Variant

      Dim Proptype1 As Variant

      Dim Propvalue1 As Variant

      Dim longstatus As Long

      Dim longwarnings As Long



      copy1config = ""

      copy2config = "Default"


      Set part = swApp.OpenDoc6(files, swDocPART, swOpenDocOptions_Silent, "", fileerror, filewarning)

      swApp.SetCurrentWorkingDirectory (folder)

      Set part = swApp.ActivateDoc3(files, True, fileerror, filewarning)

      Set model = swApp.ActiveDoc

      If model Is Nothing Then

          MsgBox "There is no active Document"                     ' <--- Currently shows this msgbox

      End If

      Set cpm3 = model.Extension.CustomPropertyManager(copy1config)               '<--- This is where it stops with Run-time Error 91

      If IsEmpty(cpm3.GetNames) Then



          names3 = cpm3.GetNames

          NumProps = cpm3.GetAll(Propname, Proptype, Propvalue)

          For j = 0 To UBound(names3)

          configvalj = Propvalue(j)

          confignamj = Propname(j)

          Set cpm2 = model.Extension.CustomPropertyManager(copy2config)

          If IsEmpty(cpm2.GetNames) Then

              GoTo 30


              names2 = cpm2.GetNames

              NumProps2 = cpm2.GetAll(Propname1, Proptype1, Propvalue1)

              For i = 0 To UBound(names2)

              configvali = Propvalue1(i)

              confignami = Propname1(i)

              If confignami = confignamj Then

                  If configvali <> configvalj Then

                      If IsNull(configvalj) Or configvalj = "0" Then

                          GoTo 20


                          If IsNull(configvali) Or configvali = "0" Then

                              If model.IsLocked = False Then

                                  model.LockFile folder, 0, 0

                              End If

                              model.DeleteCustomInfo2 copy2config, confignami

                              NewPropOk = model.AddCustomInfo3(copy2config, Propname(j), Proptype(j), Propvalue(j))

                              model.LockFile folder, 0, 1

                              GoTo 20

                          End If

                      End If


                      GoTo 20

                  End If

              End If



          End If


          If model.IsLocked = False Then

              model.LockFile folder, 0, 0

          End If

          NewPropOk = model.AddCustomInfo3(copy2config, Propname(j), Proptype(j), Propvalue(j))




      End If



      model.LockFile folder, 0, 1






      End Sub