Are you trying to remove just the selected content that falls within a particular selection? Or are you trying to select multiple sections and only remove from within them? I'm not sure I understand your objective.
As for the code, sections only appear to be available as a child of the document, not of text or any other object within it. So, depending on what you're selecting, that's likely the cause of the error.
I'm trying to remove the section not the content. It would be the equivalent of selecting the first page of a section in the page panel, then right-clicking on it and clicking on "Numbering and sections..." and then unchecking the "Start Section" checkbox.
That's what the first code snippet does. But I was only able to do it by index number.
I've read in other posts that it's difficult to specify selections in the page panel using scripting. I'm not sure if it's even possible.
Aha, now I understand. Yes, to my knowledge it is very difficult/impossible to interact with the pages panel selection. If you can make the selection in the document, you can use the appliedSection property of page to get the current section, but that might not suit your needs if you're trying to work from the Pages panel.
In any case, if you can work backwards from your selection to a parentPage object, you can use appliedSection to get the section.
Interesting - I will give that a shot. Thanks for your help!