Originally Posted by
Dave
I usually don't look at files but it's a boring Saturday afternoon. You need to set up your XL sheet so that data for each column is only in 1 column (ie. You have $ 120.00 taking up 3 columns instead of 1.. just right click the cell/range and hit format cells). Anyways, this might provide a better starting point for you. This code refers to a "test" folder on the "C" drive...adjsut to suit. You can insert this in sheet code and then call the macro. Good luck. Dave
[vba]Sub XLWordTable2()
Dim AppWD As Object, wrdDoc As Object
Dim Rowtot As Integer, Coltot As Integer, Cnt As Integer
'adds text & table to Word doc from XL
'no Word reference required
On Error GoTo ErFix
Set AppWD = CreateObject("Word.Application")
'open existing word doc file ie. "C:\test\test.doc"
'Set wrdDoc = AppWD.Documents.Open(Filename:="C:\test\test.doc")
'open new doc
Set wrdDoc = AppWD.Documents.Add
'AppWD.Visible = True
'clear doc
' With wrdDoc
' .Range(0, .Characters.Count).Delete
'End With
AppWD.activedocument.Select
With AppWD.Selection
.Font.Size = 11
.Font.Bold = True
.Font.Name = "Times New Roman"
.typetext Text:=CStr(Sheets("lit").Range("AB" & 1))
.typeparagraph
.typetext Text:=CStr(Sheets("lit").Range("AB" & 2))
.typeparagraph
.typetext Text:=CStr(Sheets("lit").Range("AB" & 3))
.typeparagraph
.typetext Text:=CStr(Sheets("lit").Range("AB" & 4))
.typeparagraph
End With
'add table
Coltot = 4
With Sheets("lit")
lastrow = .Range("c" & .Rows.Count).End(xlUp).Row
End With
Rowtot = lastrow - 18
With wrdDoc
.Tables.Add AppWD.Selection.Range, numrows:=Rowtot, Numcolumns:=Coltot
.Tables(1).cell(1, 1).Range = CStr(Sheets("lit").Range("c" & 19))
.Tables(1).cell(1, 2).Range = CStr(Sheets("lit").Range("m" & 19))
.Tables(1).cell(1, 3).Range = CStr(Sheets("lit").Range("t" & 19))
.Tables(1).cell(1, 4).Range = CStr(Sheets("lit").Range("aa" & 19))
For Cnt = 2 To Rowtot
.Tables(1).cell(Cnt, 1).Range = CStr(Sheets("lit").Range("c" & 18 + Cnt))
.Tables(1).cell(Cnt, 2).Range = CStr(Sheets("lit").Range("o" & 18 + Cnt))
.Tables(1).cell(Cnt, 3).Range = CStr(Sheets("lit").Range("v" & 18 + Cnt))
.Tables(1).cell(Cnt, 4).Range = CStr(Sheets("lit").Range("ab" & 18 + Cnt))
Next Cnt
End With
'autoformat table
'table autoformat #'s 0 to 42 'ie. exchange 13 for 0 to 42
AppWD.activedocument.Tables(1).AutoFormat Format:=13, _
ApplyBorders:=True, ApplyFont:=True, ApplyColor:=True
'autofit table cell contents
With AppWD.activedocument.Tables(1)
.Columns.AutoFit
End With
'make text string with space following table
Dim Txtstr As String
Txtstr = vbCrLf & "Test finished at: " & Now()
'add text string after table
With AppWD.activedocument
.Content.InsertAfter Txtstr
End With
'close and save new or existing doc
AppWD.activedocument.SaveAs ("C:\test\test.doc")
'close and save existing doc
' AppWD.ActiveDocument.Close savechanges:=True
'clean up
Set wrdDoc = Nothing
AppWD.Quit
Set AppWD = Nothing
MsgBox "Finished"
Exit Sub
ErFix:
On Error GoTo 0
MsgBox "error"
Set wrdDoc = Nothing
AppWD.Quit
Set AppWD = Nothing
End Sub[/vba]