PDA

View Full Version : Export excel data to word (in table)



da_phat
11-26-2008, 06:05 PM
hi,.
i need some help here.I have excel with several heading such as Name,address,postcode.I would like to export all the data into word (in table format) where it contain multiple table in it.I upload the screenshot of the table in word.Thanks.

da_phat
11-26-2008, 06:06 PM
Here is the sample data in excel.Thanks again

Kenneth Hobs
11-26-2008, 07:43 PM
Maybe something like:
Sub PasteNow()
PasteRangeToNewDoc ThisWorkbook.Path & "\Test.doc", Selection
End Sub

Sub PasteRangeToNewDoc(docFilename As String, pasteRange As Excel.Range)
Dim wdApp As Object, wd As Object, hFile As Integer
Dim rngPaste As Word.Range

On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Set wdApp = CreateObject("Word.Application")
End If

'Create an empty docFilename if one does not exist
If Dir(docFilename) = "" Then
hFile = FreeFile
Open docFilename For Binary Access Read As #hFile
Close hFile
End If
On Error GoTo 0

Set wd = wdApp.Documents.Open(docFilename)

'Put pasteRange into clipboard
pasteRange.Copy
Set rngPaste = wd.Content

'Copy to docFilename
wdApp.Visible = True
With wd
'Enter value of a cell to a formfield
'.FormFields("Brand").Result = Cells(activecell.row, "B")
'Enter a value of a cell to the body of the doc
'wd.Selection.TypeText Text:="hi"
rngPaste.Collapse wdCollapseEnd
rngPaste.InsertParagraphAfter
rngPaste.Paste
rngPaste.Font.Name = "Arial"
rngPaste.Font.Size = 24
End With

Application.CutCopyMode = False
Set wd = Nothing
Set wdApp = Nothing
End Sub