The error is at the beginning:
double current = exec.Getvar(6); // Retrieve value in internal macro varaible #6. Work Offset.
Variable #6 hasn't been set, so the return value from Getvar is null, and can't be converted to a double. You'll need to modify the macro to protect against that possibility. I don't see anything in this macro that's updating #6, so I'm guessing it's being done in a different macro or the g code program.
Not related to the error: Where do you define "loop"? At the top of the macro you have:
while(loop)
... I don't see it being defined/declared anywhere.
I'd also question why you'd want to execute the macro body more than once anyway. There's a lot of other things in that macro that I'd change, from an aesthetics perspective. Your use of
goto is poor practice, and can easily be converted to a
while. You convert to double too often. That is, you convert a string to double and put it into one variable, then you convert the same, unchanged string again and put it into a different variable, or you convert the same string to double as part of an
if predicate. You should read about the
switch statement in C#, too.