A couple of things: the new document may not be getting seen as the ActiveDoc. So you don't depend on this, change line 6 to
doWork (object);
and line 11 to
function doWork (doc) {
and remove line 12.
Also, make sure that your lines 16 and below are actually the correct syntax for doing what you want.