ChartSaveTemplate Error 4112

 
I have a script letting user to customise the chart properties. I use ChartSetSymbolPeriod to change the chart timeframe. my last input in the script is to save the template. problem is ChartSetSymbolPeriod causing the script to be terminated and then I recieve error in applying template.

How to fix it?
 
Any help?