Home » excel » excel – Why won't my macro connect with solidworks?

excel – Why won't my macro connect with solidworks?

Posted by: admin May 14, 2020 Leave a comment

Questions:

I was using excel last night and my macro stopped working. It is linked to a solidworks file. This macro has been in use for 3-4 years and had no problems until last night.

I get error 91 around this line “Part.SketchManager.InsertSketch True”
No change was made to the file with the exception that I pressed escape to try and stop the macro the last time I ran it. This may or may not be a coincidence for why it stopped working.
I did not write this code and cannot contact the person who did. Please help me fix it.

The code is:

Dim swApp As Object
Dim Part As Object
Dim boolStatus As Boolean
Dim longStatus As Long, longwarnings As Long
Dim conv As Double
Dim angle As Double
Dim counter As Double
Dim SelMgr As Object
Dim value As Double
Dim Feature As Object
Dim vPoint As Variant
Dim droop_Steps As Integer
Dim step_Size As Double
Dim comp_Steps As Integer
Dim front_row As Integer


Sub plotmotion()
angle = 57.2957795
conv = 0.0254

Set swApp = CreateObject("SldWorks.Application")
Set Part = swApp.ActiveDoc
Dim myDimension As Object
Part.SketchManager.InsertSketch True
front_row = 1
boolStatus = Part.Extension.SelectByID2("plot", "SKETCH", 0, 0, 0, False, 0, Nothing, 0)
Part.EditSketch

For counter = 0.887 To 0 Step -0.005
    front_row = front_row + 1

    Set myDimension = Part.Parameter("Sheave [email protected]")
    myDimension.SystemValue = counter * conv

    Excel.Range("E" & CStr(front_row)) = Part.Parameter("Sheave [email protected]@RAMP 20-1.Part").SystemValue
    Excel.Range("I" & CStr(front_row)) = Part.Parameter("[email protected]@RAMP 20-1.Part").SystemValue
    Excel.Range("F" & CStr(front_row)) = Part.Parameter("[email protected]@RAMP 20-1.Part").SystemValue
    Excel.Range("G" & CStr(front_row)) = Part.Parameter("[email protected]@RAMP 20-1.Part").SystemValue
    Excel.Range("H" & CStr(front_row)) = Part.Parameter("[email protected]@RAMP 20-1.Part").SystemValue * angle


Next counter
Part.SketchManager.InsertSketch True

End Sub
How to&Answers:

With CreateObject() a new SOLIDWORKS process is started and is hidden by default. You then try to immediately use the ActiveDoc property, but there is no document open in this new process and then the following lines will trigger an error.
Sometimes it is working like this to connect a already opened SOLIDWORKS process – but not always!

If you want to connect to an open SOLIDWORKS process you should use Set swApp = GetObject(, "SldWorks.Application") instead.