Hello.
I want to run a macro when printing mail merged documents.
The document has a table with 22 rows. When merging with data, the mail merged documents have various numbers of empty rows depending on the data. I have found a macro which deletes the empty rows in the table. If I run the macro on the merged documents it deletes the empty rows on the first document which is fine. However, it deletes the same number of rows on the following documents which is a problem. I can give an example. The first document has 5 rows with data, so 17 rows gets deleted. The second documents has only 3 rows with data, so 19 rows should be deleted, but only 17 gets deleted because of the first document.
I would assume the solution was that the macro should run when printing all merged documents. Is there a way to do this or is there another solution.
The macro is here:
Hope somebody can help!Code:Sub DelAllBlankTableRows() Application.ScreenUpdating = False Dim Pwd As String, pState As Variant, bFit As Boolean, i As Long, j As Long With ActiveDocument pState = False If .ProtectionType <> wdNoProtection Then Pwd = InputBox("Please enter the Password", "Password") pState = .ProtectionType .Unprotect Pwd End If For i = .Tables.Count To 1 Step -1 With .Tables(i) bFit = .AllowAutoFit .AllowAutoFit = False For j = .Rows.Count To 1 Step -1 On Error Resume Next 'skip vertically merged cells With .Rows(j) 'ChrW(8194) is the formfield space character. Trim erases any other spaces If Trim(Replace(Replace(Replace(.Range.Text, Chr(13) & Chr(7), ""), _ ChrW(8194), ""), Chr(160), "")) = "" Then .Delete End With On Error GoTo 0 Next On Error Resume Next 'skip deleted tables .AllowAutoFit = bFit On Error GoTo 0 End With Next If pState <> wdNoProtection Then .Protect Type:=pState, NoReset:=True, Password:=Pwd End With Application.ScreenUpdating = True End Sub




Reply With Quote
