Results 1 to 8 of 8

Thread: [RESOLVED] - Run macro when printing merged documents

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2016
    Posts
    6

    Resolved [RESOLVED] - Run macro when printing merged documents

    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:
    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
    Hope somebody can help!
    Last edited by holm100; May 21st, 2016 at 05:31 AM.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [Word] - Run macro when printing merged documents

    are you merging to the printer? or a document then printing?

    pls use code tags when posting code
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2016
    Posts
    6

    Re: [Word] - Run macro when printing merged documents

    Quote Originally Posted by westconn1 View Post
    are you merging to the printer? or a document then printing?

    pls use code tags when posting code
    I have a document and use mail merge with data from an excel sheet. After using mail merge I can look through the merged documents. Then I run the macro. Then I print.

  4. #4
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,470

    Re: [Word] - Run macro when printing merged documents

    the empty cells are in the data before merging or a result of merging?

    ie can you fix the table before merging begins and have a cleaner merge if the problem is "pre merge"

    here to help

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [Word] - Run macro when printing merged documents

    i tested your code on a very basic merged formletters document, it worked correctly without error

    are you sure that the formletters document is the activedocument at the time?
    i would be trying to avoid using the activedocument in the code, maybe

    Code:
    for each d in documents
       if instr(d.name, "FormLetters") > 0 then exit for
    next
    with d
    assumes only one form letters document would be open
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  6. #6

    Thread Starter
    New Member
    Join Date
    May 2016
    Posts
    6

    Re: [Word] - Run macro when printing merged documents

    Thanks for the input.
    I found a solution in Word. After the merge I should choose to merge them all to a new document. This collects all my letters into one new document. After that I run the macro which works fine. Then I print the document.

  7. #7
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [RESOLVED] - Run macro when printing merged documents

    After using mail merge I can look through the merged documents. Then I run the macro. Then I print.
    I should choose to merge them all to a new document
    i thought that was what you were doing already
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  8. #8

    Thread Starter
    New Member
    Join Date
    May 2016
    Posts
    6

    Re: [RESOLVED] - Run macro when printing merged documents

    Quote Originally Posted by westconn1 View Post
    i thought that was what you were doing already
    Almost. The difference is that after the first merge, where you have the option to print from word, word also leaves an option of editing all documents as a new document. Waiting to run the macro in the new document solves the problem.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width