Oh no! Where's the JavaScript?
Your Web browser does not have JavaScript enabled or does not support JavaScript. Please enable JavaScript on your Web browser to properly view this Web site, or upgrade to a Web browser that does support JavaScript.

Solidworks VBA to extract information from a model and save it to an Excel file.

Last updated on 1 month ago
C
caaSuper Admin
Posted 1 month ago
Here’s an example of how you can use VBA in SolidWorks to extract information from a model (e.g., dimensions or mass properties) and save it to an Excel file.
Scenario:

You have a SolidWorks part or assembly.
You want to extract certain properties (like dimensions or mass properties) and save them to an Excel file.

Step 1: Write VBA Code in SolidWorks

Open SolidWorks and go to the VBA Editor (Alt + F11).

Insert a new module (Right-click on VBAProject (YourDocument) > Insert > Module).

Copy and paste the following VBA code into the module:
C
caaSuper Admin
Posted 1 month ago
Sub SavePropertiesToExcel()
 ' Variables for SolidWorks
 Dim swApp As Object
 Dim swModel As ModelDoc2
 Dim swCustPropMgr As CustomPropertyManager
 Dim massProps As MassProperty
 Dim swDim As Dimension
 Dim i As Integer
 
 ' Variables for Excel
 Dim xlApp As Object
 Dim xlWorkbook As Object
 Dim xlSheet As Object
 Dim filePath As String
 
 ' Initialize SolidWorks application and model
 Set swApp = Application.SldWorks
 Set swModel = swApp.ActiveDoc
 
 ' Check if a model is open
 If swModel Is Nothing Then
 MsgBox "No model is open.", vbExclamation
 Exit Sub
 End If
 
C
caaSuper Admin
Posted 1 month ago
 ' Initialize Excel application
 Set xlApp = CreateObject("Excel.Application")
 Set xlWorkbook = xlApp.Workbooks.Add
 Set xlSheet = xlWorkbook.Sheets(1)
 
 ' Set the file path for Excel
 filePath = "C:PathToSaveSolidWorksProperties.xlsx"
 
 ' Write headers in Excel
 xlSheet.Cells(1, 1).Value = "Property Name"
 xlSheet.Cells(1, 2).Value = "Value"
 
 ' Get Custom Properties from the model
 Set swCustPropMgr = swModel.Extension.CustomPropertyManager("")
 Dim propNames As Variant
 Dim propValues As Variant
 Dim resolvedValues As Variant
 propNames = swCustPropMgr.GetNames
 ReDim propValues(UBound(propNames))
 ReDim resolvedValues(UBound(propNames))
 
 ' Retrieve and write custom properties to Excel
 For i = 0 To UBound(propNames)
 swCustPropMgr.Get4 propNames(i), False, propValues(i), resolvedValues(i)
 xlSheet.Cells(i + 2, 1).Value = propNames(i)
 xlSheet.Cells(i + 2, 2).Value = resolvedValues(i)
 Next i
 
 ' Get Mass Properties
 Set massProps = swModel.Extension.CreateMassProperty
 xlSheet.Cells(i + 3, 1).Value = "Mass"
 xlSheet.Cells(i + 3, 2).Value = massProps.Mass
 xlSheet.Cells(i + 4, 1).Value = "Volume"
 xlSheet.Cells(i + 4, 2).Value = massProps.Volume
 xlSheet.Cells(i + 5, 1).Value = "Surface Area"
 xlSheet.Cells(i + 5, 2).Value = massProps.SurfaceArea
 
 ' Save the Excel workbook
 xlWorkbook.SaveAs filePath
 xlWorkbook.Close False
 xlApp.Quit
 
 ' Release Excel objects
 Set xlSheet = Nothing
 Set xlWorkbook = Nothing
 Set xlApp = Nothing
 
 ' Notify user
 MsgBox "Properties have been saved to Excel.", vbInformation
End Sub
C
caaSuper Admin
Posted 1 month ago
Step 2: Run the VBA Macro

Go back to the SolidWorks Interface.
Run the macro by going to Tools > Macro > Run, select SavePropertiesToExcel, and click Run.

Explanation:

SolidWorks Interaction: The script interacts with the currently active model in SolidWorks. It retrieves custom properties and mass properties like mass, volume, and surface area.
Excel Interaction: The script creates a new Excel workbook, writes the retrieved properties to the workbook, and saves it to the specified location.
Saving the File: The Excel file is saved in the specified directory with the properties listed.
You can view all discussion threads in this forum.
You cannot start a new discussion thread in this forum.
You cannot reply in this discussion thread.
You cannot start on a poll in this forum.
You cannot upload attachments in this forum.
You cannot download attachments in this forum.
Sign In
Not a member yet? Click here to register.
Forgot Password?
Users Online Now
Guests Online 2
Members Online 0

Total Members: 10
Newest Member: rain