Thursday, January 12, 2012

Copy Excel Data into Word File Using Excel-VBA


!...........Excel VBA Program to Copy Activesheet data and paste into Ms-Word Table...........!

Sub copy_to_Word()

Dim doc As New Word.Application   '' Tools->Reference->Microsoft Word 12.0 Object Library
Dim i, j As Integer
doc.Documents.Add
doc.ActiveDocument.Tables.Add Word.Selection.Range, ActiveSheet.Range("a1").End(xlDown).Row, ActiveSheet.Range("a1").End(xlToRight).Column
doc.ActiveDocument.Tables(1).Style = "table Grid"
doc.Visible = 1
For i = 1 To Range("a1").End(xlToRight).Column

For j = 1 To Range("a1").End(xlDown).Row
doc.ActiveDocument.Tables(1).Columns(i).Cells(j).Range = Cells(j, i).Value

Next j
Next i
doc.ActiveDocument.Save
ActiveWorkbook.Close savechanges = 1

End Sub

Sample file for the above VBA Program:----------------------------
Download Macro File

No comments:

Post a Comment

Excel VBA to send email from excel with HTML table in a email body

'' Excel vba script to send email from outlook & email body in HTML table format. ''Write down email body in HTML tags...