Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mail Merge into Word Table

Status
Not open for further replies.

MarcusESP79

Programmer
Mar 30, 2007
3
GB
I've got a problem when merging data from excel into word.

Basically, I've got a table in word where the data is being merged to, and where a field meets a certain condition I want to delete the whole row from the Word table.

I've got a macro which can do this, but I want to try and avoid this since it uses a lot of resource when running a selection.rows.delete over potentially 5,000 pages!!

Does anyone know how I can do this...or even if it's possible?

Many thanks
 
Hi MarcusESP79,

A mailmerge can't delete a row from a table, as such, but there are ways around this.

For example, you might use an IF field to test whether the trigger condition exists and use the response to insert a version of the table with or without the row concerned. This is quite feasible if there are only a few variations of the table for each merged record.

Alternatively, if there are many potential variations for each merged record, it might be better to have multiple tables (one for each condition), that can be output one after the other with minimal white space in between.

Cheers

[MS MVP - Word]
 
Hi macropod,

Thanks for your help on this. I didn't think it was possible to do it.

Basically the way I'm doing it at the moment inserts a "$$" for any nulls in the source data. The macro then searches and removes all rows with those characters.

The problem with inserting different tables depending on the conditions set is that there's many combinations of the conditional fields.

One of my documents has a potential 6x6x6 different combinations! Which would get very very messy.

I think I'll have to stick with my macro due to the complexity of trying to find another way round the problem.

Thanks for your help though.
 
Using Selection - as in Selection.Row.Delete - is a very inefficient way to do the action.

Selection...selects, that is it highlights whatever it is, and thus is using GUI resources.

You can do it without Selecting anything.

Code:
Dim oTable As Word.Table
Dim oRow As Row

For Each oTable In ActiveDocument.Tables
  For Each oRow In oTable.Rows
      If oRow.Cells(1).Range.Text = _
          "$$" & Chr(13) & Chr(7) Then
          oRow.Delete
      End If
  Next
Next
will delete every row that has "$$" in the first cell of that row, in every table of a document.

I did a test document of 297 pages containing 1034 tables. Six different varieties of tables of up to 7 rows. Some rows with "$$", and some without. It took 1.3 seconds to clear all rows with the first cell containing "$$" from every table.

No Selection was made.

It may speed things up for you, considering you are talking about up to 5,000 pages!

The assumption for the code above is that "$$" in the first cell of the row.

NOTE: there is no testing for any other content of the row. There is no other testing at all. If the first cell of a row is "$$" - but ONLY "$$" - then it is deleted.

Gerry
My paintings and sculpture
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top