# Macro to combine tables in MS word



## tarjeik (Jul 5, 2011)

Hi,
I am working on a massive mail merge (10,000 records) in MS Word 2003 that consists of several tables, some of which needs to be joined together after completing the merge.
I'm currently using the following macro to do this:

Sub TableJoiner()
Dim oPara As Paragraph
For Each oPara In ActiveDocument.Paragraphs
With oPara.Range
If .Information(wdWithInTable) = True Then
With .Next
If .Information(wdWithInTable) = False Then
If .Text = vbCr Then .Delete
End If
End With
End If
End With
Next
End Sub

and this works fine when the merge is small (300-400 records), but stops working when the number increases (Word stops responding and I have to restart)
I don't much about macros, and did not write this myself, but it seems to me to be a general macro looking for ANY table and removing the space between (beneath(?)) them.

Because I know which table to remove the space (every other page on line 12, 14 and 16) I was wondering if someone could give me a hint on how to make the macro only remove the space at the designated line(s). 

This would be easier (i think) if i could 'define' an area like you can in excel, but I'm not sure if this is possible?

The only alternative I can think of is to split the merge into 30 odd 'mini merges' to avoid the macro to time out, but I would really like to awoid this if possible.

Not sure if that makes any sense to anyone but myself, but any feedback would be much appreciated.

Thanks and Regards

Tarjei K


----------



## macropod (Apr 11, 2008)

Hi Tarjei,

That looks suspiciously like a macro from my my Microsoft Word Catalogue/Directory Mailmerge Tutorial at:
Word 97-2007 Mailmerge Tutorial: Create Sorted Listings (v1.52) | Windows Secrets Lounge
or
http://www.gmayor.com/Zips/Catalogue Mailmerge.zip

You could make the macro run faster, by temporarily disabling screen updating:

```
Sub TableJoiner()
Application.Screenupdating = False
Dim oPara As Paragraph
For Each oPara In ActiveDocument.Paragraphs
  With oPara.Range
    If .Information(wdWithInTable) = True Then
      With .Next
        If .Information(wdWithInTable) = False Then
          If .Text = vbCr Then .Delete
        End If
      End With
    End If
  End With
Next
Application.Screenupdating = True
End Sub
```
However, you also need to accept that, with thousands of lines to process, the macro is going to take some time to complete. With long processes, Word can appear to hang even though the macro is still running.


----------



## tarjeik (Jul 5, 2011)

Hi Paul, 

You are absolutely spot on! I did get it from you, and I have used it on multiple occations. Your tutorial is great and it has made me the mail merge guru around the office, so thank you very much!

I will use your updated macro and I do appreciate that it will take time to complete,

Thank again!

Cheers

Tarjei


----------



## tarjeik (Jul 5, 2011)

Hi Paul,
Don't know if it is of any interest to you, but I did find a quicker way of removing the empty rows in the table.
The first thing I did was to insert a unique character the blank lines (in my case this character was #). Then I recorded a macro that would remove the "#" and the space after it to remove the empty row in the merged document:


```
Sub Macro2()
'
' Macro2 Macro
'
'
 
Do While Selection.Information(wdActiveEndPageNumber) < 212
 
    Selection.Find.ClearFormatting
    With Selection.Find
        .Text = "#"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute
    Selection.Delete Unit:=wdCharacter, Count:=2
 
Loop
ActiveDocument.save
End Sub
```
Due to my limited macro knowledge I had to insert the last page number every time I rant he macro (in the case above, there were 212 pages), but I'm sure this can be modified to automatically find the last page. The one problem I see with this however is that when there were a great number of lines per record the final number of pages might be less than unformatted number of pages, and if I didn't take this into account the macro went into an infinite loop. Once again, I'm sure this can be improved.

I'd like to thank you once again for your great tutorial, without it I would not have been able to do my job!

Cheers
T


----------



## macropod (Apr 11, 2008)

Hi Tarjei,


> I did find a quicker way of removing the empty rows in the table.


The previous discussion was about _joining tables_; your latest post seems to be about something entirely different (_deleting empty table rows_). You can do that much more efficiently with code like:

```
Sub Demo()
Application.ScreenUpdating = False
Dim i As Long, j As Long, k As Long
With ActiveDocument
  'Skip over errors caused by tables with vertically merged cells
  On Error Resume Next
  For i = .Tables.Count To 1 Step -1
    With .Tables(i)
      'Delete empty tables
      If Len(.Range.Text) = (.Columns.Count + 1) * .Rows.Count * 2 Then
        .Delete
      Else
        'Delete empty rows
        For j = .Rows.Count To 1 Step -1
          With .Rows(j)
            If Len(.Range.Text) = (.Cells.Count + 1) * 2 Then .Delete
            End With
        Next
      End If
    End With
  Next
End With
Application.ScreenUpdating = True
End Sub
```
That said, your code really has nothing to do with tables or deleting empty rows. Basically, all it does is look for the '#' character, then delete that and the following character.


----------



## tarjeik (Jul 5, 2011)

No worries. From now on I'll just leech of forums and tutorials like your own, much better than trying to add something back in case I'm wrong.

All the best
T


----------



## macropod (Apr 11, 2008)

Well, if you take that approach, you may never know that something you're using doesn't do what you think it does (eg it works in one situation, but for the wrong reasons and, hence, won't work in others) or that it could be done in a better way.


----------

