Make sure that you have dimension named D1 in Sketch2 feature (D1@Sketch2)
Well, I dont have it, but that's supposed to be the point of the macro, add a dimension.
I recorded myself drawing two lines and then adding a dimension to one of them, the default name is D1. I dont get it how it is not working if it is a recording...
Maybe I'm missing something, I'm new to solidworks in general.
Solidworks' macro recorder is effectively worthless for generating working code. It uses obsolete API calls, rarely captures the crucial state changing functions, etc. It's better to use it for discover, as a starting point, and then go spelunking in the API documentation to verify what is and isn't valid.
Set myDimension = Part.Parameter("D1@Sketch2")
'this returns Nothing and assigns Nothing to myDimension when the part doesn't have a Dimensions named "D1@Sketch2".
myDimension.SystemValue = 0.107
'this throws an error because myDimensions equals Nothing
The line that actually creates the dimension is above where it throws. You need to use the object reference that it returns to get access to the underlying Dimension and set its parameter. If you don't want to change the dimension, there isn't really any reason to keep any of the code following the dimension creation.
Here is the documentation page for those functions.
Ran into this while trying to "play along" with the API Fundamentals eCourse on mySolidworks.
I try recording a simple macro of sketching a circle and extruding it, and the stinking macro that I get from the recorder won't work, with a failure at the same line...
Poor form, Macro Recorder, poor form.
This seems like a pretty rudimentary problem to have with the macro recorder (spitting out code for one of the simplest operations that there is in SW). Is there an SPR that we can start spamming to try to get this resolved, or would the SW devs be drinking from a fire hose trying to keep this stuff up to date?