Code to open a new part in solidwords 2009 from excel vba
Code to open a new part in solidwords 2009 from excel vba
(OP)
I have been able to program Solidworks VBA to be able to create a 3D moldel in solidworks.
Now I would like to be able to do the same by launching it from Excel VBA.
I am having problems with the syntax to open up a new part in solidwors from excel vba
does anyone have similar code that is working to use as an example of the syntax?
Now I would like to be able to do the same by launching it from Excel VBA.
I am having problems with the syntax to open up a new part in solidwors from excel vba
does anyone have similar code that is working to use as an example of the syntax?






RE: Code to open a new part in solidwords 2009 from excel vba
Set swApp = Application.SldWorks
except you use GetObject (if you know SW is open) or CreateObject if SW might not be open.
-handleman, CSWP (The new, easy test)
RE: Code to open a new part in solidwords 2009 from excel vba
I moved the macro to excel and tried to run it.
when I run it using excel vba, I get the error
object does not support this property or method
on the line
Set swApp = Application.SldWorks
which is the line you said we need.
any ideas?
the code is below
' ******************************************************************************
' C:\Users\Office\AppData\Local\Temp\swx5468\Macro1.swb - macro recorded on 02/27/11
' ******************************************************************************
Dim swApp As Object
Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Sub main()
Set swApp = Application.SldWorks
Set Part = swApp.NewDocument("C:\Program Files\SolidWorks Corp\SolidWorks\lang\english\Tutorial\part.prtdot", 0, 0, 0)
swApp.ActivateDoc2 "Part4", False, longstatus
Set Part = swApp.ActiveDoc
Dim myModelView As Object
Set myModelView = Part.ActiveView
myModelView.FrameState = swWindowState_e.swWindowMaximized
boolstatus = Part.Extension.SelectByID2("Front", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
Part.SketchManager.InsertSketch True
Part.ClearSelection2 True
Dim skSegment As Object
Set skSegment = Part.SketchManager.CreateCircle(0.03616, 0.021024, 0#, 0.027669, 0.014151, 0#)
Part.ClearSelection2 True
boolstatus = Part.Extension.SelectByID2("Arc1", "SKETCHSEGMENT", 0.03454268968685, 0.03153595505618, 0, False, 0, Nothing, 0)
Dim myDisplayDim As Object
Set myDisplayDim = Part.AddDimension2(-0.007505250388061, 0.04265440074906, 0)
Part.ClearSelection2 True
Dim myDimension As Object
Set myDimension = Part.Parameter("D1@Sketch1")
myDimension.SystemValue = 0.0123
Part.ShowNamedView2 "*Trimetric", 8
Part.ClearSelection2 True
boolstatus = Part.Extension.SelectByID2("D1@Sketch1@Part4.SLDPRT", "DIMENSION", 0, 0, 0, False, 0, Nothing, 0)
Dim myFeature As Object
Set myFeature = Part.FeatureManager.FeatureExtrusion2(True, False, False, 0, 0, 0.014, 0.01, False, False, False, False, 0.01745329251994, 0.01745329251994, False, False, False, False, True, True, True, 0, 0, False)
Part.SelectionManager.EnableContourSelection = False
End Sub
RE: Code to open a new part in solidwords 2009 from excel vba
Sorry I wasn't so clear. Application.SldWorks only works from SolidWorks. From any other program you need to use GetObject or CreateObject, or the New keyword.
Here's an Excel sheet that will attach to a running instance of SW or (if one does not exist already) start a new one, then start a new part file.
-handleman, CSWP (The new, easy test)
RE: Code to open a new part in solidwords 2009 from excel vba
Thank you so far for your help. Your macro has opened up solidworks, but when I add on the rest of the macro that was recorded by solidworks itself, nothing else happens.
Ie solidoworks opens and that it - no errors are reported
here is the full code as I am trying to run from excel
Sub StartNewPartFromExcel()
Dim swApp As SldWorks.SldWorks
Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Const ERR_APP_NOTFOUND As Long = 429
On Error Resume Next
Set swApp = GetObject(, "SldWorks.Application")
If Err.Number = ERR_APP_NOTFOUND Then
Set swApp = New SldWorks.SldWorks
swApp.UserControl = True
End If
swApp.NewPart
Set swApp = Nothing
' from here down, the macro comes from SW
Set swApp = Application.SldWorks
Set Part = swApp.NewDocument("C:\Program Files\SolidWorks Corp\SolidWorks\lang\english\Tutorial\part.prtdot", 0, 0, 0)
swApp.ActivateDoc2 "Part4", False, longstatus
Set Part = swApp.ActiveDoc
Dim myModelView As Object
Set myModelView = Part.ActiveView
myModelView.FrameState = swWindowState_e.swWindowMaximized
boolstatus = Part.Extension.SelectByID2("Front", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
Part.SketchManager.InsertSketch True
Part.ClearSelection2 True
Dim skSegment As Object
Set skSegment = Part.SketchManager.CreateCircle(0.03616, 0.021024, 0#, 0.027669, 0.014151, 0#)
Part.ClearSelection2 True
boolstatus = Part.Extension.SelectByID2("Arc1", "SKETCHSEGMENT", 0.03454268968685, 0.03153595505618, 0, False, 0, Nothing, 0)
Dim myDisplayDim As Object
Set myDisplayDim = Part.AddDimension2(-0.007505250388061, 0.04265440074906, 0)
Part.ClearSelection2 True
Dim myDimension As Object
Set myDimension = Part.Parameter("D1@Sketch1")
myDimension.SystemValue = 0.0123
Part.ShowNamedView2 "*Trimetric", 8
Part.ClearSelection2 True
boolstatus = Part.Extension.SelectByID2("D1@Sketch1@Part4.SLDPRT", "DIMENSION", 0, 0, 0, False, 0, Nothing, 0)
Dim myFeature As Object
Set myFeature = Part.FeatureManager.FeatureExtrusion2(True, False, False, 0, 0, 0.014, 0.01, False, False, False, False, 0.01745329251994, 0.01745329251994, False, False, False, False, True, True, True, 0, 0, False)
Part.SelectionManager.EnableContourSelection = False
End Sub
any thoughts as what I still need to do to get it to draw and extrude?
RE: Code to open a new part in solidwords 2009 from excel vba
"Set swApp = Application.SldWorks" ONLY WORKS WHEN THE CODE IS RUN IN A SOLIDWORKS MACRO. IT WILL NEVER WORK FROM EXCEL. IF YOU PUT THAT LINE ANYWHERE IN AN EXCEL MACRO IT WILL NEVER, EVER WORK.
Read the code a little bit.
My code sets swApp = Nothing, which is good practice once you're done using any object. However, if you want to tack on a bunch of code after it, you aren't done using it. So you shouldn't set it = Nothing until the end of your tack-on code. And you shouldn't try to set it = something with the above line of code either.
You should also put in the line "On Error Goto 0" in the spot where you take out "Set swApp = Nothing". Then you will be told where the rest of your errors are.
-handleman, CSWP (The new, easy test)
RE: Code to open a new part in solidwords 2009 from excel vba
Your suggestions worked fantastic
Thank you so much for your help