I found the issue, in one of the scripts that my script is called from, there was some code that checked if the workbook was locked or not. I commented out this code and it now works.