Use AutoPtr for a Document re #8485

Signed-off-by: Karl Palmen <karl.palmen@stfc.ac.uk>
Tidy up code a little re #8485

Signed-off-by: Karl Palmen <karl.palmen@stfc.ac.uk>