How do I replace a part with VBA?

Hello

I am looking to perform an automatic part replacement on SolidWorks using the macro tool. Concretely, I would like to change/replace one part per value bracket. I found the command below but it doesn't work;

boolstatus = Part.ReplaceComponents("\\xxxxxxxx.SLDPRT", "", True, 0, True)

Thank you for your help.

Hello

Have you seen the example in the SolidWorks help?

http://help.solidworks.com/2012/English/api/sldworksapi/Replace_Component_Example_VB.htm

Where does your macro get stuck?

What is the error message?

1 Like

The debugger message, I'll try by studying what you gave me:)

1 Like

Does it work now?

In fact I didn't understand everything about your code, I just need the order to replace one part with another, I know the way of both! I have to create such complicated code?

Try this code:

 

Dim swApp As Object 


Dim Part As Object 


Dim path as string


Dim boolstatus As Boolean 


Sub main() 


Set swApp = Application.SldWorks 


Set Part = swApp.ActiveDoc 


Part.ClearSelection2 True 


Replace here NOM_DE_TA_PIECE_DANS_SOLIDWORKS@NOM-DE-TON-ASSEMBLAGE with the name of the part to be replaced


boolstatus = Part.Extension.SelectByID2("NOM_DE_TA_PIECE_DANS_SOLIDWORKS@NOM-DE-TON-ASSEMBLY", "COMPONENT", 0, 0, 0, False, 0, Nothing, 0) 


path = "C:\folder\name of your new part . SLDPRT"


boolstatus = Part.ReplaceComponents(path, "", False, True) 


End Sub

1 Like

This command doesn't work, here's my code: (debug opens for the last line)

Sub main()

Set swApp = _
Application.SldWorks

Set Part = swApp.ActiveDoc


Part.ClearSelection2 True


boolstatus = Part.Extension.SelectByID2("Part Name-1@Assemblage", "COMPONENT", 0, 0, 0, False, 0, Nothing, 0)

boolstatus = Part.ReplaceComponents("C:\xxxxxxx\xxxx.sldprt", "", False, 0, True)


End Sub

 

Is it normal "C:\xxxxxxx\xxxx.sldprt"?

And are you sure you didn't make a mistake in "Name of the 1@Assemblage Piece"?

Yes, I just replaced my links with xxxx, and yes for the second question because I save it by the macro recording tool before...  

I just found the error..... A little 0 too many in my order: 

boolstatus = Part.ReplaceComponents("C:\xxxxxxx\xxxx.sldprt", "", False, 0, True)

Thank you very much:)

1 Like

And you made the mistake of copying what I wrote in addition, because what I wrote is right!

:) No worries, please choose my answer as the best please:)