Automatic serial numbers for file names

Can anybody tell me how to generate serial numbers as file names such as 0001,0002 etc. ?
The document is created from a .ott template. I tried to use a user defined property from the template, but no idea about how to access and update the property value stored in the attached template.

mDocProperties = ThisComponent.getDocumentProperties().UserDefinedProperties

mdocproperties.refno= mdocproperties.refno + 1

msgbox mdocproperties.refno

This code works fine in the current document. But I need to get the value from the attached template and increment the value in the template. Can somebody help?