ChatGPT and I are writing a program to manipulate a Word document that will contain songs and lyrics written in a new music notation system as described here: https://www.youtube.com/watch?v=dSkx76Kjfmw The width of each letter (note) is changed using the font scaling capability of Word and indicates how long the note is played for:
The module we are currently working on will vertially align the notes with the lyrics syllables and output them with increased or decreased spacing between them, as per the image above. (It will be the only music notation program that automatically detects syllables without needing the user to manually hyphenate multi-syllable words).
The notes are in Courier New font so the characters are all the same width (before scaling). Every character, including the spaces between the notes, has unique font formatting (Font name, Size, Bold and Scaling)
Neither I nor ChatGPT can get the formatting right. Our code reads in each character and stores it along with the Font attributes. After determining how many spaces are required the characters have to be written back using the same attributes as they had before. The spaces are scaled down to 30% to allow finer gap adjustment.
This is the code we have come up with so far to output one line of notes after deleting the existing line but it does not work. It writes the characters to the start of the next line instead of the current line and does not apply the unique font attributes to each character.
Sub InsertNotesIntoDocument(notesLine As NotesLineClass)
Dim lineNumber As Integer
Dim i As Integer
Dim noteClass As noteClass
Dim noteSubClass As noteSubClass
' Determine the line number to insert the notes into
lineNumber = notesLine.lineNumber
' Set the range to the start of the specified line within the selection
Dim rng As Range
Set rng = ActiveDocument.Paragraphs(lineNumber).Range
rng.End = rng.End - 1 ' Exclude paragraph mark at the end of the line
' Clear the existing content of the line (except the paragraph mark)
rng.Text = vbNullString
' Iterate through each NoteClass object in the collection
For Each noteClass In notesLine.Notes
' Set font properties for the entire note
With rng.Font
.Name = noteClass.FontName
.Size = noteClass.FontSize
End With
' Iterate through each SubNoteClass object in the SubNotes collection
For Each noteSubClass In noteClass.SubNotes
' Insert a new range for each character insertion
Dim charRng As Range
Set charRng = rng.Duplicate
' Apply character-specific properties
With charRng.Font
.Bold = noteSubClass.Bold
.Scaling = noteSubClass.Scaling
End With
' Insert the character into the document
charRng.Text = noteSubClass.NoteChar
' Move the original range to the end of the inserted character
rng.Collapse wdCollapseEnd
Next noteSubClass
' Insert spaces scaled to 30% between notes if needed
For i = 1 To noteClass.PaddingBytes
rng.Text = " " ' Insert a space directly using the main range
rng.Collapse wdCollapseEnd ' Move past the space
Next i
Next noteClass
End Sub