3 Replies Latest reply on Jul 5, 2014 1:36 AM by Deepak Gupta

    Extracting filename to custom properties

    Jeremy Harris

      I've spent a few hours on this forum trying to learn how to create a Macro to extract from the file name and import the info into the relevant custom properties. I have no knowledge with macros but have tried to pick up some things as I go along so I apologise if this is really basic.

       

      I've succeded in extracting the necessary info into the DWG No. and Description fields.

       

      Only issue I currently have is that for the description, it keeps including the ".SLDPRT" or ".SLDASM"

       

      I've tried a few methods to instruct it to replace it with nothing but I keep getting errors?

       

      On the Macro so far, the description section is:

       

          If swModel.CustomInfo("DWG No.") = "" Then

            swModel.AddCustomInfo2 "DWG No.", swCustomInfoText, Left(swModel.GetTitle, 11)

          Else

              swModel.CustomInfo("DWG No.") = Left(swModel.GetTitle, 11)

          End If

       

      What would I have to do to remove the extension from the new custom properties description?

       

      Thanks,

        • Re: Extracting filename to custom properties
          Sean Phillips

          this is crude because I would use system.io.fileinfo.

           

          str = Mid(myfilename, 1, Len(myfilename) - 7)

            • Re: Extracting filename to custom properties
              Sean Phillips

              Add Reference “Microsoft Scripting Runtime”

               

              ' ******************************************************************************

              ' C:\Users\SEAN\AppData\Local\Temp\swx5900\Macro1.swb - macro recorded on 06/22/14 by SEAN

              ' ******************************************************************************

              Dim swApp As Object

               

               

              Dim Part As ModelDoc2

               

               

               

               

              Sub main()

               

               

              Set swApp = Application.SldWorks

               

               

              Set Part = swApp.ActiveDoc

               

               

              Dim FSO As FileSystemObject

               

               

              Set FSO = New FileSystemObject

               

               

              Dim PathName As String

               

               

              PathName = Part.GetPathName

               

               

              Dim FileName As String

               

               

              FileName = FSO.GetFileName(PathName)

               

               

              Dim FileExtension As String

               

               

              FileExtension = "." & FSO.GetExtensionName(PathName)

               

               

              Dim Name As String

               

               

              Name = Mid(FileName, 1, Len(FileName) - Len(FileExtension))

               

               

              Debug.Print Name

               

               

              Debug.Print "or"

               

               

              Name = Replace(FileName, FileExtension, "")

               

               

              Debug.Print Name

               

               

              End Sub

            • Re: Extracting filename to custom properties
              Deepak Gupta

              Try this:

               

              Option Explicit

              Dim swApp                   As SldWorks.SldWorks

              Dim swModel                 As SldWorks.ModelDoc2

              Dim sFileName               As String


              Sub main()

               

              Set swApp = Application.SldWorks

              Set swModel = swApp.ActiveDoc

               

              'Get File Name without extension

              sFileName = Mid(swModel.GetPathName, InStrRev(swModel.GetPathName, "\") + 1)

              sFileName = Left(sFileName, Len(sFileName) - 7)

               

              'Add/Set the Custom Property 

                   If swModel.CustomInfo("DWG No.") = "" Then

                    swModel.AddCustomInfo2 "DWG No.", swCustomInfoText, Left(sFileName, 11)

                  Else

                      swModel.CustomInfo("DWG No.") = Left(sFileName, 11)

                  End If

                     

              End Sub