I have a macro that takes three tables in Excel that are separated by a row of blank cells and produces three formatted tables in Word with page breaks after each table. At the moment it does this on three different documents but I need them to be all on the same one. Here is the code at present;
Dim wdApp As New Word.Application
Dim wdDoc As Word.Document
Dim wdTbl1 As Word.Table
Dim wdTbl2 As Word.Table
Dim wdTbl3 As Word.Table
Dim xlSht As Worksheet
Dim lRow As Integer
Dim lCol As Integer
Dim r As Integer
Dim c As Integer
Dim Blanks As Integer
Dim First As Integer
Dim Second As Integer
lRow = Sheets("Feedback Sheets").Range("A1000").End(xlUp).Row - 2
Blanks = 0
i = 1
Do While i <= lRow
Set rRng = Worksheets("Feedback Sheets").Range("A" & i)
If IsEmpty(rRng.Value) Then
Blanks = Blanks + 1
If Blanks = 1 Then First = i
If Blanks = 2 Then Second = i
End If
i = i + 1
Loop
'Now I know where end of each table is
Set xlSht = ActiveSheet: lCol = 5
With wdApp
.Visible = True
Set wdDoc = .Documents.Add
With wdDoc
Set wdTbl1 = .Tables.Add(Range:=.Range, NumRows:=2, NumColumns:=lCol)
With wdTbl1
.Rows(1).Range.Font.Bold = True
.Rows(1).HeadingFormat = True
.Cell(1, 1).Range.Text = "Header 1"
If lCol > 1 Then .Cell(1, 2).Range.Text = "Header 2"
If lCol > 2 Then .Cell(1, 3).Range.Text = "Header 3"
End With
With xlSht
For r = 1 To First
If (r + 1) > wdTbl1.Rows.Count Then wdTbl1.Rows.Add
If r = First Then wdDoc.Characters.Last.InsertBreak Type:=wdPageBreak
For c = 1 To lCol
wdTbl1.Cell(r + 1, c).Range.Text = xlSht.Cells(r, c).Text
Next c
Next r
End With
End With
End With
Set myTable = ActiveDocument.Tables(1)
With myTable.Borders
.InsideLineStyle = wdLineStyleSingle
.OutsideLineStyle = wdLineStyleDouble
End With
With wdApp
.Visible = True
Set wdDoc = .Documents.Add
With wdDoc
Set wdTbl2 = .Tables.Add(Range:=.Range, NumRows:=2, NumColumns:=lCol)
With wdTbl2
.Rows(1).Range.Font.Bold = True
.Rows(1).HeadingFormat = True
.Cell(1, 1).Range.Text = "Header 1"
If lCol > 1 Then .Cell(1, 2).Range.Text = "Header 2"
If lCol > 2 Then .Cell(1, 3).Range.Text = "Header 3"
End With
With xlSht
For r = First To Second
If (r + 1) > wdTbl2.Rows.Count Then wdTbl2.Rows.Add
If r = Second Then wdDoc.Characters.Last.InsertBreak Type:=wdPageBreak
For c = 1 To lCol
wdTbl2.Cell(r + 1, c).Range.Text = xlSht.Cells(r, c).Text
Next c
Next r
End With
End With
End With
Set myTable = ActiveDocument.Tables(1)
With myTable.Borders
.InsideLineStyle = wdLineStyleSingle
.OutsideLineStyle = wdLineStyleDouble
End With
With wdApp
.Visible = True
Set wdDoc = .Documents.Add
With wdDoc
Set wdTbl3 = .Tables.Add(Range:=.Range, NumRows:=2, NumColumns:=lCol)
With wdTbl3
.Rows(1).Range.Font.Bold = True
.Rows(1).HeadingFormat = True
.Cell(1, 1).Range.Text = "Header 1"
If lCol > 1 Then .Cell(1, 2).Range.Text = "Header 2"
If lCol > 2 Then .Cell(1, 3).Range.Text = "Header 3"
End With
With xlSht
For r = Second To lRow
If (r + 1) > wdTbl3.Rows.Count Then wdTbl3.Rows.Add
If r = Second Then wdDoc.Characters.Last.InsertBreak Type:=wdPageBreak
For c = 1 To lCol
wdTbl3.Cell(r + 1, c).Range.Text = xlSht.Cells(r, c).Text
Next c
Next r
End With
End With
End With
Set myTable = ActiveDocument.Tables(1)
With myTable.Borders
.InsideLineStyle = wdLineStyleSingle
.OutsideLineStyle = wdLineStyleDouble
End With
Set wdTbl1 = Nothing: Set wdTbl2 = Nothing: Set wdTbl3 = Nothing: Set wdDoc = Nothing: Set wdApp = Nothing: Set xlSht = Nothing
I tried to remove the Set wdDoc = .Documents.Add
from each iteration of the Set wdTbl
block but that caused it to produce only table 3 and the page break at the end of it. I feel that it might be this part;
Set myTable = ActiveDocument.Tables(1)
With myTable.Borders
.InsideLineStyle = wdLineStyleSingle
.OutsideLineStyle = wdLineStyleDouble
End With
I think this might be leaving the full table selected and so it overwrites it with the next table, but I can’t figure out how to deselect it (if that even is the error!)
There is too much code in your question to give you a good answer, but I think I can give you some pointers.
Your first idea was good. Remove all but the first Set wdDoc = .Documents.Add
. Your second thought is also good. The part ActiveDocument.Tables(1)
is 2 and 3 for the second and third table.
Lastly, make something about Set wdTbl1 = .Tables.Add(Range:=.Range, NumRows:=2, NumColumns:=lCol)
. The .Range
part is the problem. That says do this for the entire document, which is okay if it is a single table in the document, but not if there are more than one.
Something like this should do it. Untested.
Dim TargetRange As Word.Range
Set TargetRange = .Range
TargetRange.Collapse Direction:=wdCollapseEnd
' There must be a paragraph between tables.
TargetRange.InsertParagraph
Set TargetRange = .Range
TargetRange.Collapse Direction:=wdCollapseEnd
Set wdTbl2 = .Tables.Add(Range:=TargetRange, NumRows:=2, NumColumns:=lCol)
8
Please, also try the next simpler (and a little faster way) way:
Sub CopyTablesToWdDocument()
Dim ws As Worksheet, lastR As Long, rngTbl As Range, A As Range, lRow As Long, arrTbl, i As Long, j As Long
Const lCol As Long = 5
Dim wdApp As Word.Application, wdDoc As Word.Document, wdTbl As Word.Table
Set ws = ActiveSheet 'use here the sheet you need
lastR = ws.Range("A" & ws.rows.count).End(xlUp).row 'last row on column A:A
Set rngTbl = ws.Range("A1:A" & lastR).SpecialCells(xlCellTypeConstants) 'each table is in an Area...
'no any empty cell must be in A:A!
Set wdApp = CreateObject("Word.application") 'set the Word Application
Dim rngTableTarget As Word.Range 'set this range to set position of the next table to be created!
With wdApp
.Visible = True 'make the application visible
Set wdDoc = .Documents.Add: Set rngTableTarget = wdDoc.content 'initially the content of a new document is enough
For Each A In rngTbl.Areas 'iterate between the range areas (in fact the X tables...):
lRow = A.rows.count 'extract the number of table rows
arrTbl = A.Resize(, lCol).Value 'place the table range in an array (resizing to X columns)
Set wdTbl = wdDoc.tables.Add(Range:=rngTableTarget, NumRows:=lRow, NumColumns:=lCol) 'add the table
With wdTbl
For i = 1 To UBound(arrTbl) 'iterate between the table array rows
For j = 1 To lCol 'iterate between the table array columns
.cell(i, j).Range.text = arrTbl(i, j) 'write the values in the newly added table
Next j
Next i
.rows(1).Range.Font.Bold = True 'the format of the table header
.rows(1).HeadingFormat = True 'the format of the table header
With .Borders
.InsideLineStyle = wdLineStyleSingle
.OutsideLineStyle = wdLineStyleDouble
End With
rngTableTarget.start = wdDoc.content.End 'reinitialize the reference for adding the neext table
rngTableTarget.InsertBreak Type:=wdPageBreak 'page break
rngTableTarget.InsertParagraph 'insert a paragraph to avoid adding rows to the same table...
End With
Next A
End With
End Sub