Add custom property to all assembly components using macro

I'm a beginner macro/VB user and I've managed to piece together a macro that will parse the filename of a .sldprt and deposit some of it's characters into a custom property of "Description".  How can I make this work at the touch of a button to loop through an entire assembly including components located in subassemblies?  I tried looking the VB help under traversing assemblies at component levels but was quickly lost - way over my head regarding what does what.  I see the counters in there and some other recognizable things there which make it seem like this can be done - but the view is cloudy.  The macro code I have for a single component is below.  I've attached a dummy assembly and drawing I've been using to try and get this to work to no avail.  Any help is appreciated.

---------------------------------------

Option Explicit

Dim swApp As SldWorks.SldWorks
Dim swModel As ModelDoc2
Dim cpm As CustomPropertyManager

Sub main()

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set cpm = swModel.Extension.CustomPropertyManager("")

Dim path As String, filename As String, Description As String

path = swModel.GetPathName
filename = Mid\\\$(path, InStrRev(path, "\\") + 1) ' With extension
filename = Left\\\$(filename, InStrRev(filename, ".") - 1) ' Remove extension

If InStr(Left(filename, 1), "t") = True Then
    Description = UCase(Right(filename, Len(filename) - 7))
Else
    Description = UCase(Right(filename, Len(filename) - 6))
End If

Description = Replace(Description, "_", " ")

cpm.Delete "Description"
cpm.Add2 "Description", swCustomInfoText, Description

End Sub

------------------------------------------

SolidworksApi macros
Answers 22
IK
IK 2009-11-18 Best Answer

Option Explicit


Dim swApp As SldWorks.SldWorks

Sub main()
Dim swModel As ModelDoc2

Dim vComps As Variant
Dim swComp As SldWorks.Component2
Dim swAssy As SldWorks.AssemblyDoc
Dim i As Integer
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc

updateProperty swModel
If swModel.GetType = swDocASSEMBLY Then
    Set swAssy = swModel
    vComps = swAssy.GetComponents(False)
    For i = 0 To UBound(vComps)
        Set swComp = vComps(i)
        Set swModel = swComp.GetModelDoc2
        updateProperty swModel
    Next i
End If


End Sub

Function updateProperty(swModel As SldWorks.ModelDoc2) As Boolean
Dim cpm As CustomPropertyManager


Dim path As String, filename As String, Description As String

Set cpm = swModel.Extension.CustomPropertyManager("")

path = swModel.GetPathName
filename = Mid$(path, InStrRev(path, "\") + 1) ' With extension
filename = Left$(filename, InStrRev(filename, ".") - 1) ' Remove extension


If InStr(Left(filename, 1), "t") = True Then
    Description = UCase(Right(filename, Len(filename) - 7))
Else
    Description = UCase(Right(filename, Len(filename) - 6))
End If


Description = Replace(Description, "_", " ")

cpm.Delete "Description"
cpm.Add2 "Description", swCustomInfoText, Description

End Function

SF
SF 2009-11-18
If you just want to do this quickly, use a trial version of BatchProcess
SF
SF 2009-11-18
Worked beautifully!  Thanks!
SF
SF 2009-11-19

Alright - I swear it worked yesterday.  Today.... not so much.  I'm getting the following error:

Run-time error '91':

Object variable or With block variable not set

Upon clicking debug the following line of code is highlighted:

Set cpm = swModel.Extension.CustomPropertyManager("")

I assume that this line is "opening" the custom property manager for the current model in the loop?

SF
SF 2009-11-19

Ralph,

Try Set cpm = swModel.Extension.CustomPropertyManager("","")

SF
SF 2009-11-19

No luck there either.  It moves the same error down to this line:

cpm.Delete "Description"

SF
SF 2009-11-19

Does "Description" reside in the Custom Properties?

SF
SF 2009-11-19

I've downloaded the free trial and am attempting to figure it out.  Would I would have to use the macro function on here to do this wouldn't I?  I'd use working macro that I have for a single part file and that could be applied to all components of an assembly?

SF
SF 2009-11-19

It may or may not.  On the code that I have to do this on a single part file it works either way. 

SF
SF 2009-11-19
That's because you are calling updateProperty Function before you check to see if it's an assembly!  It took my a minute to find that.
SF
SF 2009-11-19
As I mentioned - I'm a beginner with this so bear with me please.  The update property function is performed before it checks to see if the active model is an assembly?  So then the update property needs to be in the intitial loop somewhere?
IK
IK 2009-11-19
set lightweight to resolved before you start macro
SF
SF 2009-11-19

DING DING DING

And there it is.  Can I give you two correct answers in one thread??  Perhaps I'll have to add some lines in there to do that so as to avoid this question from users.

Thanks a ton.

IK
IK 2009-11-19
Option Explicit


Dim swApp As SldWorks.SldWorks

Sub main()
Dim swModel As ModelDoc2

Dim vComps As Variant
Dim swComp As SldWorks.Component2
Dim swAssy As SldWorks.AssemblyDoc
Dim i As Integer
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc

updateProperty swModel

If swModel.GetType = swDocASSEMBLY Then
    Set swAssy = swModel
    vComps = swAssy.GetComponents(False)
    For i = 0 To UBound(vComps)
        Set swComp = vComps(i)
        If swComp.GetSuppression = swComponentFullyResolved Then
            Set swModel = swComp.GetModelDoc2
            updateProperty swModel
        Else
            MsgBox "warn user lightweight or suppressed"
        End If
    Next i
End If


End Sub

Function updateProperty(swModel As SldWorks.ModelDoc2) As Boolean
Dim cpm As CustomPropertyManager


Dim path As String, filename As String, Description As String

Set cpm = swModel.Extension.CustomPropertyManager("")

path = swModel.GetPathName
filename = Mid$(path, InStrRev(path, "\") + 1) ' With extension
filename = Left$(filename, InStrRev(filename, ".") - 1) ' Remove extension


If InStr(Left(filename, 1), "t") = True Then
    Description = UCase(Right(filename, Len(filename) - 7))
Else
    Description = UCase(Right(filename, Len(filename) - 6))
End If


Description = Replace(Description, "_", " ")

cpm.Delete "Description"
cpm.Add2 "Description", swCustomInfoText, Description

End Function
SF
SF 2009-11-19

Sorry.  I wasn't yelling at you.  I thought I had something for a moment.  I'm glad you got it working.

I don't think I've had that problem before of making sure the parts were resolved.  I'll have to go back and take a look.

SF
SF 2009-11-20
Again - thank you
SF
SF 2009-11-24

There is a help manual with it. If that doesnt help drop me an email.

Basically you want to first add the macro from the Tools->Macros menu. The "method" is the class name, and the method. For exampe Macro1.main

The Act On specifies what to run the macro on, parts, assemblies, drawings or all of them, so you can isolate the macro to be specific for certain types.

The Run Before specifies at what point to run the macro if you are running other jobs like printing and things, but if you aren't then just check the Close to run it before the file is closed.

Once added go to Settings->Other and make sure "Macros act on forces file open" is checked.

Now open your Assembly in SolidWorks, click Refresh in BP, and all files will be loaded in. Check all the files you want to run the macro on in the list (by default all are selected so effectively that will run on all files (components) in the assembly, and the assembly itself (so you can uncheck that file if you wish). Then click the Run Macro function button and click Run. You will be asked to select macros to run, check the one you added and hit OK, job done.

Test it on a sample assembly first and usually with a macro that just pops up a message box to show it is running when you want it to and on the files you want it to.

SF
SF 2009-12-01

Luke - that worked awesome.  I was on the right track but didn't have the method name correct.  I couldn't get the raw macro to run on my assembly as I've got some suppressed components for some configurations.  The macro as it is won't accept that and I don't know how to correct it.  Batchprocess though did exactly what I wanted using my single part level macro with great ease.

Thanks!

SF
SF 2009-12-04
If I want this to run on suppressed components wouldn't I remove the If-Else block in Sub main()?  I tried this and got it to partially run once - but then it hangs up on the line Set cpm = swModel.Extension.CustomPropertyManager("") with a "Object variable or With block variable not set" error message.
MK
MK 2015-02-26

Hi Ivana,

i'm trying to use this macro for me but it doesen't work. I'm getting the following error:

Run-time error '5':

Invalid procedure call or argument

and It highlite this line:

Description = UCase(Right(filename, Len(filename) - 6))

Please help

SF
SF 2015-02-28

swp add macro

add macro

SF
SF 2015-02-28

did not work

How does it work

Add video

comment imagecomment image