|
|
|
|
|
|
Excel
|
Create formatted Word table from Excel data range
|
|
Ease of Use
|
Easy
|
Version tested with
|
2002, 2003
|
Submitted by:
|
Killian
|
Description:
|
This short and simple routine copies a range of cells and pastes it into a new Word file from a template with a formatted table style.
|
Discussion:
|
With some minor tweaking, this routine provides a quick and easy way of outputting Excel data into formatted Word tables for inclusion in your reports.
It could provide the framework (with some extra coding and extension of the styles in the Word template) for a cumstomized data report generator.
|
Code:
|
instructions for use
|
Option Explicit
Sub Data2Word()
Dim rng As Range
Dim wdApp As New Word.Application
Dim wdDoc As Word.Document
Dim t As Word.Range
Dim myWordFile As String
myWordFile = ThisWorkbook.Path & "\DocWithTableStyle.dot"
Set rng = Range("A1").CurrentRegion
rng.HorizontalAlignment = xlCenter
rng.Copy
Set wdDoc = wdApp.Documents.Add(myWordFile)
Set t = wdDoc.Content
t.Paste
With t
.Style = "GreenBar"
.Tables(1).Columns.SetWidth (rng.Width / rng.Columns.Count), wdAdjustSameWidth
End With
wdApp.Visible = True
wdApp.Activate
End Sub
|
How to use:
|
- First, prepare a Word template that has a table style set how you want it. The example attached has one set with a Heading row and odd row formatting (producing a table where the alternate rows are shaded light green).
- Save the Word file as a template. Close Word and open Excel. Add a few rows and columns of data (from cell A1) and save in the same folder as the Word template.
- Open the Visual Basic Editor and go to Tools>References
- You will need to select the reference to the Microsoft Word (version number) Object model
- Add a new module and paste in the code below.
- Save the workbook and run the routine!
|
Test the code:
|
- Tools | Macro | Macros...
- Select Data2Word and press Run.
|
Sample File:
|
Data2WordTable.zip 13.84KB
|
Approved by mdmackillop
|
This entry has been viewed 329 times.
|
|