Hi all,
good morning
I aim to populate word existing table from an excel rang.
I'd prefer to use excel range to array and pass it to the word table rather than copy each cell and paste it to word table.
then I come up with this code hope to have help to optimize
I was trying for this method but I failed
[code] wdDoc.Tables(1).Cell(i, j).Range = array(i,j) [code]
then I come up with this code, hope to have help to optimize it if possible
Thanks a lot
Sub xl_Rng2Word_table()
Dim appWord As Word.Application
Dim wordDoc As Word.Document
Dim rng As Range
Dim sh_source As Worksheet
Dim wdApp As Word.Application
Dim wdDoc As Word.Document
Set sh_source = ThisWorkbook.Sheets("sheet1")
Set rng = sh_source.Range("a1:j21")
vaData = rng
u = UBound(vaData, 1)
l = UBound(vaData, 2)
Set wdApp = New Word.Application
wdApp.Visible = True
Set wdDoc = wdApp.Documents.Open(ThisWorkbook.Path & "\" & "3333.docx")
j = 1
For Each Cel In wdDoc.Tables(1).Range.Cells
i = i + 1
Count = Count + 1
Cel.Range.Text = vaData(j, i)
If i = l Then
i = 0
j = j + 1
End If
If j = u + 1 Then Exit For
Next Cel
End Sub
Bookmarks