Add new configuration fail

Hi

I'm writing a small macro to pull in info from a db and populate some configuration specific properties.  Its all working fine, except I also need to create extra configurations in my part first if they don't already exist (to store the stuff) and its falling over here.   I'm using.

     result = ModelDoc.AddConfiguration3(ConfigName, ConfigName, AltName, swConfigOption_DontActivate)

where modeldoc points to the active document, but I get an 'Runtime Error: 91. object variable or with block variable not set'

Sometimes it creates the config and switches to it before I get this error too, even though I set swConfigOption_DontActivate (which is 128)

Any help appreciated

Thanks

Rich

ps. also just tried

result = ModelDoc.ConfigurationManager.AddConfiguration(ConfigName, ConfigName, ConfigName, swConfigOption_DontActivate, "1413", ConfigName) with same result

SolidworksApi macros