IEquationMgr::Value is get-only. You cannot use it to set a value. You need to use IEquationMgr::Equation to change the value. There's many different ways you could write code that does this, but here's one way:
Const VARIABLE_NAME As String = "var1"
Const NEW_VALUE As String = "2"
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swEqnMgr As SldWorks.EquationMgr
Dim i As Integer
Dim vSplit As Variant
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swEqnMgr = swModel.GetEquationMgr
For i = 0 To swEqnMgr.GetCount - 1
vSplit = Split(swEqnMgr.Equation(i), " = ")
vSplit(0) = Replace(vSplit(0), Chr(34), Empty)
If vSplit(0) = VARIABLE_NAME Then _
swEqnMgr.Equation(i) = Replace(swEqnMgr.Equation(i), vSplit(1), NEW_VALUE)
Hope this helps. By the way, did you know that you can run VBA code in equations? Check this out --- it's pretty cool.
Thank you for the code. For it to work, you need to delete the spaces in your split function. Other than that, it works perfectly!
This is eventually going to be part of a form that pops up to take user input to automatically create a part- we have thousands of parts that are all the same, but different.
For it to work, you need to delete the spaces in your split function.
Perhaps we are using different versions of SolidWorks? The spaces were necessary in my testing on 2013.
Glad it works for you.
I know this is an old thread but it was very helpful.
Only issue I had was with the Split command, our equations had different formats, one might be " = " or "= ", no way to tell for certain where the = might fall with spaces, Trim() cleaned it up, set Split to look for the "=" and off it went.
vSplit = Split(swEquMgr.Equation(i), "=")
vSplit(0) = Trim(Replace(vSplit(0), Chr(34), Empty))
Thanks again Keith.
I'm trying to understand this code, maybe is a dumb question but here we go.
What are the conditions to change the variable value for "NEW_VALUE" in your code?
The variable name that you are using is "var1" and the "NEW_VALUE" is "2"?