VBA Word output characters to Word document with different formatting for each character

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:

New music notation system

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật