Hello I am trying to use this code to export data from excel to a pre-existing word table. I am a novice in this area. Pls help! Regards, Fane
I tried to write the code, but I have a Compile error (user-defined type...):
I tried to write the code, but I have a Compile error (user-defined type...):
Code:
Sub ExportDataWordTable()
Const stWordDocument As String = "Template fisa de esantionare var.4.docm"
Dim wdApp As Word.Application
Dim wdDoc As Word.Document
Dim wdCell As Word.Cell
Dim i As Long
Dim j As Long
Dim wbBook As Workbook
Dim wsSheet As Worksheet
Dim vaData As Variant
Set wbBook = ThisWorkbook
Set wsSheet = wbBook.Worksheets("Sheet1")
ReDim vaData(1 To 10, 1 To 8)
With wsSheet
vaData = .Range("A2:H11")
End With
'Here we instantiate the new object.
Set wdApp = New Word.Application
'Here the target document resides in the same folder as the workbook.
Set wdDoc = wdApp.Documents.Open(wbBook.Path & "" & stWordDocument)
'Import data to the first table and in the first column of a table in Microsoft Word.
For j = 1 To 8
i = 0
For Each wdCell In wdDoc.Tables(1).Columns(j).Cells
i = i + 1
wdCell.Range.Text = vaData(i, j)
Next wdCell
Next j
'Save and close the document.
With wdDoc
.Save
.Close
End With
'Close the hidden instance of Microsoft Word.
wdApp.Quit
'Release the external variables from the memory
Set wdDoc = Nothing
Set wdApp = Nothing
MsgBox "The data has been transferred to Template fisa de esantionare var.4", vbInformation
End Sub
Last edited by a moderator: