MACRO LIBRARY

Export model's custom properties to Excel file

Free public access. No account or subscription required.

Files

Export model’s custom properties to Excel file

Code

Module: mMain

'Demonstrates how to export all custom properties to an Excel spreadsheet.
    
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
    
'Preconditions: model is open.
'Results: Excel is open with new workbook containing custom property data.
    
Option Explicit
    
#If VBA7 Then
Declare PtrSafe Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As LongPtr, ByVal wMsg As Long, ByVal wParam As LongPtr, ByVal lParam As LongPtr) As Long
#Else
Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
#End If
Const WM_QUIT = &H12
    
Sub main()
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swCustPropMgr As SldWorks.CustomPropertyManager
    Dim strName As String
    Dim strValue As String
    Dim strResValue As String
    Dim colNames As New Collection
    Dim colValues As New Collection
    Dim colResValues As New Collection
    Dim vCustPropNames As Variant
    Dim i As Integer
    Dim xlApp As Excel.Application
    Dim xlWB As Excel.Workbook
    
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    
    If swModel Is Nothing Then Exit Sub
    
    Set swCustPropMgr = swModel.Extension.CustomPropertyManager(Empty)
    
    vCustPropNames = swCustPropMgr.GetNames
    If IsEmpty(vCustPropNames) = False Then
        For i = 0 To UBound(vCustPropNames)
            swCustPropMgr.Get3 vCustPropNames(i), False, strValue, strResValue
            colNames.Add vCustPropNames(i)
            colValues.Add strValue
            colResValues.Add strResValue
        Next i
    End If
    
    'Put custom prop data into excel
    Set xlApp = CreateObject("Excel.Application")
    xlApp.Visible = True
    Set xlWB = xlApp.Workbooks.Add
    With xlWB.Worksheets(1)
        For i = 1 To colNames.Count
            .Cells(i, 1).Value = colNames.Item(i)
            .Cells(i, 2).Value = colValues.Item(i)
            .Cells(i, 3).Value = colResValues.Item(i)
        Next i
    End With
    
    Set xlWB = Nothing
    Set xlApp = Nothing
End Sub