You are probably using the Write to Spreasheet.vi.
If so you can simply place it in a case structure controlled by your boolean, remember to set the append to file input TRUE.
In this basic way you open and close your file each time you enable or disable the writing.
A more efficient approach is to open the diagram of Write to Spreadsheet File vi copy it in your diagram and place only the Write File+ (string)vi in your case structure.
Let your file to be opened at the start of your vi and close at the end