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
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
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?
Ralph,
Try Set cpm = swModel.Extension.CustomPropertyManager("","")
No luck there either. It moves the same error down to this line:
cpm.Delete "Description"
Does "Description" reside in the Custom Properties?
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?
It may or may not. On the code that I have to do this on a single part file it works either way.
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.
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
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.
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.
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!
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
swp add macro
add macro
did not work
How does it work
Add video