Extracting Song Titles

  Kiến thức lập trình

I have a songbook and want to be able to make up set lists from the book. Typically extracting 18-20 songs from a total of 200, and putting them into a new document, for a specific gig.

I’ve worked out how to extract the songs in order and put them in new document by listing the page numbers in a word table. I find the beginning of the song (always the title) and then expand the Selection range to the Page Break (always at the end of each song. It’s then straightforward to copy that song into a new document and loop round to do the other songs. That bit works ok, but I also want a summary list of all the song titles alongside the page numbers in the table I provided.

I can find the start of each song title as it’s always at the top of the selected song. What I need to do is select from the beginning to the start of an embedded field code.

For example, if I turn on ‘Show paragraph marks’ on this song I see the text “Green, Green Grass{ TC “Green, Greed Grass” f C l “1” }”. I want to select the text from the start up to, but not including the “{” character. Then want to copy this song title and put it into the table where I got the set list page numbers from.
I’m aware also that this is not a standard curly bracket but Word’s way of showing the start of a field code for the ToC (where the title can sometimes be shorter, to fit into the ToC).

If for the first part (extracting the whole song I can expand the Selection range to the Page Break, is there an equivalent wat to expand the selection from the start to the field code?

I tried varying the syntax of the bit that selects the whole song, which is:

Set rng = ActiveDocument.Range(0, 0)
Set rng = rng.GoTo(What:=wdGoToPage, Name:=NextPage) ' NextPage contains the page number of the song
Set rng = rng.GoTo(What:=wdGoToBookmark, Name:="page")

For the song title I substituted the third line with:

`Set rng = rng.GoTo(What:=wdGoToField, Name:="TC")

When I do that, the Selection.Copy command fails because nothing has been selected.

New contributor

Jeremy Helliwell is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.