VBA Excel to Solidworks

Hi all
I would like to control a solidworks function via an Excel file with a VBA macro


Attached is the code for the EditSuppress2() function I'm interested in.
'Dim instance As IModelDoc
'Dim value As System.Boolean
'value = instance. EditSuppress2()

 

Attached is the macro record via the Solidworks software
'Sub main()

'Set swApp = _
'Application.SldWorks

'Set Part = swApp.ActiveDoc
'boolstatus = Part.Extension.SelectByID2("Removed mat.-Extru.1@PièceX.part", "BODYFEATURE", 0, 0, 0, False, 0, Nothing, 0)
'Part.EditSuppress2
'Part.ClearSelection2 True
'End Sub

Has anyone ever worked on this type of VBA? How do you reconcile the two?

I want to control a deletion of a function via excel of a part?

Thank you for your help in advance.

Hello

What you seem to want to do is akin to a family of rooms. Just as efficient and simple, excel already drives natively SW in this context.

2 Likes

Yes absolutely, but I would like to proceed with an Excel macro, is it possible?

As I have several functions to delete, I would like to use an Excel macro that drives the deletion of Solidworks functions (EditSuppress2())

Hello

Here is an example of a macro in Excel that allows you to retrieve the name of the part loaded in Solidworks:

Sub Macro1()
    
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2

Set swApp = CreateObject("SldWorks.Application")
swApp.Visible = True
        
Set swModel = swApp.ActiveDoc

MsgBox swModel.GetTitle

End Sub

Don't forget to add the references to Solidworks in the Excel macro editor:

The rest is just standard vba macro writing.

Kind regards

Hello, thank you for your answer, 

I've already made a macro similar to yours to modify the dimensions of a coin with the attached code:

Part.Parameter("D1@Esquisse1@PièceX.Part").SystemValue = Excel.Range("B2") / 1000

 

But I'm stuck on deleting a function from the part, it seems to me that I should use the EditSuppress2() function but I can't format it in VBA to remove the removal of material from my part ("Removing mat.-Extru.1@PièceX.Part") 

I have my sample piece at the post.

Thank you for your help.

 


piecex.sldprt

Hello

You have an example of how to use the EditSuppress2() function HERE.

Kind regards

I hadn't tried this but I still have the same result "it doesn't work"

I put this code:

Model.SelectByID("Enlèv. mat.-Extru.1@PièceX.Part", "BODYFEATURE", 0, 0, 0, False, 0, Nothing, 0) = Model.EditSuppress()

 

I have an error of execution "424" Required Item

 

It's that you have an object that is not defined, probably "Model".

Why do you put "@piecex. Part" after the "Enlevèv. mat.-Extru.1" in the "SelectByID" which should be "SelectByID2", as well as the "EditSuppress()" which should be "EditSuppress2()"...

Set Model = swApp.ActiveDoc
boolstatus = Model.Extension.SelectByID2("Removed mat.-Extru.1", "BODYFEATURE", 0, 0, 0, False, 0, Nothing, 0)
boolstatus = swModel.EditSuppress2()

Kind regards

thank you for your answer but I have the same result 

I'm attaching my entire code

Dim swApp As Object


Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long

Private Sub CommandButton1_Click()

Set swApp = CreateObject("SldWorks.application")

Set Model = swApp.ActiveDoc
boolstatus = Model.Extension.SelectByID2("Enlèv. mat.-Extru.1", "BODYFEATURE", 0, 0, 0, False, 0, Nothing, 0)
boolstatus = swModel.EditSuppress2()

Part.ClearSelection2 True


End Sub

 

and I still have the same error in the end  "Runtime error "424": Object required

Error on my part in a copy paste, replace boolstatus = swModel.EditSuppress2() with boolstatus = Model.EditSuppress2()

great function I thank you very much, I struggled all week on this code thank you very much