Here is the code, its part of a batch utilities program that I have developed over time. There is some "filtering" at the first to make sure its only working on sldprt files then a silent save at the end. The code for adding the Weight custom property is near the end, look for SetSuccess = ...
Dim selcol As New Collection Dim longerror As Long Dim DocOptions As Long Dim thisDoc As Variant Dim i As Integer Dim c As Integer Dim retval As Variant Dim vConfNameArr As Variant Dim sConfigName As String Dim ci As Long Dim bShowConfig As Boolean Dim boolstatus As Boolean Dim swSelMgr As SldWorks.SelectionMgr
Set swApp = Application.SldWorks DocOptions = swOpenDocOptions_Silent
For Each thisDoc In doclist
Dim DocType As Long Dim DocExt As String Dim SkipFile As Long
SkipFile = InStr(1, thisDoc, "~", vbTextCompare) 'Do not process temp files
If SkipFile = 0 Then DocExt = UCase(Right(thisDoc, 3)) Select Case DocExt Case "PRT" DocType = swDocPART Case "DRW" DocType = swDocDRAWING Case "ASM" DocType = swDocASSEMBLY Case Else DocType = swDocNONE 'MsgBox "Unexpected extension encountered: " & thisDoc End Select
'*** Filter doc types to act on*** If DocType = swDocPART Then 'Act on slddrw files only Dim DocErrors As Long Dim DocWarnings As Long Dim ModelDoc As SldWorks.ModelDoc2 Dim nretval As Long Dim SetSuccess As Boolean Dim swCustPropMgr As SldWorks.CustomPropertyManager
Set ModelDoc = swApp.OpenDoc6(thisDoc, DocType, DocOptions, "", DocErrors, DocWarnings) If ModelDoc Is Nothing Then MsgBox "Couldn't open document: " & thisDoc Else
SetSuccess = ModelDoc.AddCustomInfo3("", "Weight", swCustomInfoText, Chr(34) & " snipped-for-privacy@Part1.SLDPRT" & Chr(34))
Dim SaveOptions As Long SaveOptions = swSaveAsOptions_Silent Dim SaveSuccess As Boolean SaveSuccess = ModelDoc.Save3(SaveOptions, DocErrors, DocWarnings) If SaveSuccess = False Then MsgBox "Couldn't save document: " & thisDoc End If End If swApp.CloseDoc thisDoc End If End If Next End Sub