Solidworks Property Extractor


Introduction

Suppose that there are a multitude of parts, named with a non-descriptive code, all of which have some custom properties that you have assigned. You want to quickly extract all the relevant properties and export them to an Excel file.

Implementation

Solidworks API uses visual basic and therefore has methods to easily to manipulate text files (open/create, read/write). There are already built in functions within Excel that allow quick imports of comma separated value style text files, so you may import them there if necessary. The extraction text file will be created in the specified folder with the ".sldprt" files.

The basic flow chart describing the method is shown below.

Instructions

  1. Download the .swp file provided and open with Solidworks or copy paste code into a new macro
  2. Move all ".sldprt" files into one folder (the search does not operate on sub-folders)
  3. Change the "fold" variable to the path of the folder containing the files, make sure the string contains a "\" character at the end
  4. Run the macro and wait

Summary

The folder containing the parts should look something like the image below (left). The output of the operation is a text file (right), which when opened should contain the part number and the "Description" custom property.

Code

Dim swApp As Object
Sub main()

Dim myFile As String
Dim val As Variant
Dim i As Integer
Dim j As Integer
Dim fold As String
Dim doc As SldWorks.ModelDoc2
Dim NEW_FILE As String
Dim fileerror   As Long
Dim filewarning As Long
Dim itemval1 As String
Dim resolveditemval As String
Dim desc As String

Set swApp = Application.SldWorks

' Specify folder to search
fold = "C:\Solid_prt\extraction\"

' Find all ".sldprt" files
' Note that the search does not operate on sub folders
allfiles = GetFiles(fold)

' Open/Create "extract.txt"
myFile = fold & "extract.txt"

     
Open myFile For Output As #1


For r = 1 To UBound(allfiles)

    Dim partName As String
    
    ' get the ith file name
    NEW_FILE = fold & allfiles(r)

    
    ' Open new ".sldprt" file
    Set doc = swApp.OpenDoc6(NEW_FILE, swDocPART, swOpenDocOptions_Silent, "", fileerror, filewarning)
    
    ' Read name/description
    partName = allfiles(r)
    partName = Left(partName, Len(partName) - 7)
    
    ' if you want multiple custom properties, just add them to to the list and create new variables to hold them
    desc = doc.CustomInfo("Description")
    
    
    ' Print to text file
    Print #1, partName & ",",
    Print #1, desc
    
    ' Close ".sldprt" file
    swApp.CloseDoc doc.GetTitle
    
    ' purge the var
    Set doc = Nothing
    
Next r

' Close "extract.txt"
Close #1

End Sub

Function GetFiles(fold As String)

Dim files As Variant

ReDim files(1 To 1)

Dim count As Integer

Dim filetype As String

filetype = "*.SLDPRT*"

count = 1
Dim FileName As String
FileName = Dir(fold & filetype)

Do While FileName <> ""
Debug.Print FileName
ReDim Preserve files(1 To count)
files(count) = FileName
count = count + 1
FileName = Dir
Loop

GetFiles = files

End Function