VBA Macron

 

I think that since I can't deal with macro files, I have a ready-made code via artificial intelligence, but I couldn't run it. I need help on how to enter a ready-made code into SolidWorks 2023

' Declare variables

Dim swApp As SldWorks.SldWorks

Dim swModel As SldWorks.ModelDoc2

Dim swPart As SldWorks.PartDoc

Dim swSketch As SldWorks.Sketch

Dim swFeature As SldWorks.Feature

' Create SolidWorks object

Set swApp = Application.SldWorks

Set swModel = swApp.NewDocument(swDocTypePart)

Set swPart = swModel

' Create sketch for the screwdriver head

Set swSketch = swPart.CreateSketch

swSketch.AddPolygon 6, 2, 0, 0, 0.004 ' Create hexagon (6 sides, center at 0,0, radius 4mm)

' Define the points for the angled cuts to create the hex head shape

Dim pt1 As Variant, pt2 As Variant, pt3 As Variant, pt4 As Variant

pt1 = Array(0, 0.004) ' Top point

pt2 = Array(0.002, 0.00693) ' Top right point

pt3 = Array(0.004, 0.004) ' Middle right point

pt4 = Array(0.002, 0.00107) ' Bottom right point

' Add the angled lines to the sketch

swSketch.AddLine pt1(0), pt1(1), pt2(0), pt2(1)

swSketch.AddLine pt2(0), pt2(1), pt3(0), pt3(1)

swSketch.AddLine pt3(0), pt3(1), pt4(0), pt4(1)

swSketch.AddLine pt4(0), pt4(1), 0, 0 ' Close the profile

' Create the extruded feature for the head

Set swFeature = swPart.CreateExtrusionFeature(swSketch, 0.004) ' Extrude 4mm

' Create sketch for the screwdriver body (shaft)

Set swSketch = swPart.CreateSketch

swSketch.AddCircle 0, 0, 0.002 ' Create circle (center at 0,0, radius 2mm)

' Create the extruded feature for the body

Set swFeature = swPart.CreateExtrusionFeature(swSketch, 0.12) ' Extrude 120mm

' Add more details (optional)

' You can add details such as a grip pattern on the handle or a company logo

' Save the file

swModel.SaveAs2 "C:\\Users\\Public\\Documents\\Screwdriver.sldprt" ' Change path as needed

' Clean up objects

Set swFeature = Nothing

Set swSketch = Nothing

Set swPart = Nothing

Set swModel = Nothing

Set swApp = Nothing

 

Answers 5
JB
JB 2025-02-18 Best Answer

If you did indeed get this code from a friend, the friend got it from an AI.  It is utter garbage and full of AI hallucinations of functions that do not exist, missing arguments, etc.  

There is no way to make this code work.  

comment imagecomment image
AL
AL 2025-02-18

AI is not good at programming. You'll have to 'hire' someone

QM
QM 2025-02-18

How do I make the code work

 

KC
KC 2025-02-18

To the right of your post's title is a menu. Select Edit to edit your original post and:

  1. Add some detail as to what you would like the macro to do, both in the body of the post and briefly in the post's title
  2. Paste your code as text in the body so that it be read

Save your edits.

Click the ? Help button in the upper right of SW and select API Help.
Use some of the commands of your generated code, assuming at least some of it isn't at zero value, and learn what API Help has to offer.
If there is a linked example, click and review it.

JB
JB 2025-02-18

Maybe this guy can help... 

comment imagecomment image