Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations MintJulep on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Code to open a new part in solidwords 2009 from excel vba 1

Status
Not open for further replies.

Spurs

Mechanical
Nov 7, 2002
297
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?
 
Replies continue below

Recommended for you

It doesn't really matter what program has the VBA in it - Visio, Excel, Word, PowerPoint, whatever. All you need to do is get the SldWorks.SldWorks object that you get at the beginning of every SW macro with something like:

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)
 
I recorded the following macro using Solidworks to create a simple cylinder.

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
 
In the VBA editor you will need to go to Tools->References and pick all the SolidWorks stuff.

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)
 
 http://files.engineering.com/getfile.aspx?folder=3685b27c-ddd1-4a59-be16-dc69f6f17287&file=StartSWPart.xls
Hi Handleman

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?
 
Sorry, I guess that once again I was unclear.

"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)
 
Handleman

Your suggestions worked fantastic

Thank you so much for your help

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor