Adding and Setting Text to Content Controls within Repeating Section Content Control

372 Views Asked by At

I have a word template that is filled in with information from an Access database and part of that template includes a table with two cells, type and location, for all attachments for that specific document. Within those fields are content controls, name accordingly, and then the whole table is wrapped in a repeating section content control titled "Attachments".

For each specific document there is an attachments folder that I iterate through to get the attachment type and attachment filepath. I would like to add this data to the Attachments content controls but am having issues with displaying the information correctly.

Private Sub AttachmentControl(wrdApp As Word.Application, wrdDoc As Word.Document, attachmentsFolder As String)

    Dim fso As New Scripting.FileSystemObject, folderAttachments As Files, attachment As file, i As Long
    Set folderAttachments = fso.GetFolder(attachmentsFolder).Files

    For Each attachment In folderAttachments
        If attachment.Type <> "Data Base File" Then
            wrdDoc.SelectContentControlsByTag("Type").Item(1).Range.Text = attachment.Type
            wrdDoc.SelectContentControlsByTag("Location").Item(1).Range.Text = attachment.Path
            wrdDoc.SelectContentControlsByTag("Attachments").Item(1).RepeatingSectionItems.Item(1).InsertItemBefore
        End If
    Next attachment

End Sub

The attachmentFolder is where the attachments I want to iterate through are located. For my testing purposes the files within this folder are 6 jpg images named Image 1 through Image 6 but in real life applications will contain more attachments than just images.

Expected Result

Expected Result

Actual Result

Actual Result

Is this a problem with my code or am I just not correct in how this should work?

0

There are 0 best solutions below