Don't make any changes to this local function. Keep it for closing text document. Instead, move all your new code into another local function, probably name it doCloseCustomViewer and call it from where you were trying to create a new file entry.