View Single Post
 
Old 07-29-2021, 08:40 PM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

As you only want the last record in the array, I would be inclined to work from an Excel macro. I would also replace the merge fields in the document with Content Controls, then you should be able to populate the controls directly from the array e.g. something like
Code:
Option Explicit

Sub GetLastInArray(Arr() As Variant)

Dim iRow As Long
Dim wdApp As Object
Dim wdDoc As Object
Dim oCC As Object

    On Error Resume Next
    Set wdApp = GetObject(, "Word.Application")
    If Err Then
        Set wdApp = CreateObject("Word.Application")
    End If
    On Error GoTo 0
    wdApp.Visible = True

    Set wdDoc = wdApp.Documents.Add(Template:="C:\Path\Mergedoc.docx")    'the merge document

    iRow = UBound(Arr, 2)    'The last row of the array

    For Each oCC In wdDoc.contentcontrols
        Select Case oCC.Title
            Case "Title1"
                oCC.Range.Text = Arr(1, iRow)    'column 1
            Case "Title2"
                oCC.Range.Text = Arr(2, iRow)    'Column 2
                'etc
        End Select
    Next oCC
lbl_Exit:
    Set oCC = Nothing
    Set wdDoc = Nothing
    Set wdApp = Nothing
    Exit Sub
End Sub
Instead of using an array, you could populate the controls directly from the appropriate worksheet cells.
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote