We can't help without seeing the script.
You should have all the information you need to fix the problem based on the error message. Something is not available when it's required.
As to initializing the script when the document is opened: Create a document level script (call it e.g. "initialization"). When the editor comes up, it will have a function stub in it. Remove that and add your initialization code. Document level scripts are executed when the file is opened. You may also want to consider putting your code that you need to execute in every button into a document level function and then only call that function from each button - with a parameter that indicates what button was pressed. The "event" object does contain that information. You can e.g. pass event.target.name to the function and then have the button name available in your document level function.