You are asking AI to create code and it pretends it knows what it is doing but I can tell you, in this case it really doesn't. I don't recognise the .DefaultSorting command and it absolutely doesn't belong there (what would you expect to happen if you sorted a single thing? what would it go in front or behind of?). If you removed that line AND the subsequent one then I would expect the code to actually have a chance to work without the error you are seeing.
The CleanText function would also need to be edited to actually do something useful. Looking at how the code is calling CleanText, the intention is to return a valid Bookmark name. I don't know the comprehensive rules for valid bookmark names but I can say that a bookmark name can't include spaces or begin with a number. If your selected text is a single word then the macro 'could' run without an error but in most cases it will probably fail because of what was selected when the macro started.
The MsgBox lines aren't right either but at least they don't stop the macro from proceeding.
Then you have to ask, what is the ultimate point of the code. Are you thinking it is useful to create a bookmark around a word with a name that matches the word? How is this going to be something useful?
You should look at CoPilot generated code as an opportunity to learn about coding rather than expecting it to actually work. If this is an example of what it can do then it might be rare that it produces errorless code. This could be because of limitations in AI or perhaps because you asked the question in a 'non-optimal' way. Perhaps if you experimented with asking the question in different ways, you could compare the various code outputs to see if there are variants that work as you expected.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
|