Results 1 to 4 of 4
  1. #1
    New Lounger
    Join Date
    Sep 2014
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Word 2003 merge to individual files macro problem

    Hello,

    I've adapted some vba code for word 2003 which mail merges each active record to a separate file and saves it (in the same folder as the template file) with the file name found in a specific field in the data source. This then closes the resulting document and returns the template before advancing to the next record in the datasource.

    Code:
    Sub SplitFiles()
    '
    'Adapted from Søren Francis 6/7-2013
    
        Dim DocName  As String   'ADDED CODE
        Dim Docpath As String
        Dim i As Integer
        Dim Source As Document
        Dim Target As Document
        Dim Letter As Range
        Dim oField As Field
    
        Application.ScreenUpdating = False
        Application.DisplayAlerts = False
    
    Docpath = ActiveDocument.Path
    
    ActiveDocument.MailMerge.DataSource.ActiveRecord = wdLastRecord
    
    For i = 1 To ActiveDocument.MailMerge.DataSource.ActiveRecord
        ActiveDocument.MailMerge.DataSource.ActiveRecord = i
    
        With ActiveDocument.MailMerge
            .Destination = wdSendToNewDocument
            .SuppressBlankLines = True
    '        .DataSource.Included = True 'new line
            With .DataSource
                .FirstRecord = ActiveDocument.MailMerge.DataSource.ActiveRecord
                .LastRecord = ActiveDocument.MailMerge.DataSource.ActiveRecord
    ' Remember the wanted documentname
               DocName = .DataFields("MMFileName").Value         ' ADDED CODE
            End With
    
    ' Merge the active record
            .Execute Pause:=False
        End With
    
    ' Save then resulting document. NOTICE MODIFIED filename
        ActiveDocument.SaveAs FileName:=Docpath + "\" + DocName + " - Prime" + ".doc"
    
    ' Close the resulting document
        ActiveWindow.Close
    
    ' Now, back in the template document, advance to next record
        'ActiveDocument.MailMerge.DataSource.ActiveRecord = wdNextRecord
        
    Next i
    
        Application.ScreenUpdating = True
        Application.DisplayAlerts = True
    
    MsgBox ("Mail Merge Split Process Completed")
    
    End Sub
    For what I need the macro seems to work ok - the main problem seems to be when the data source has lots of records and the user only wants to mail merge a few of those at anyone time - the macro cycles through every record in the datasource and just those that the user has checked/selected to merge.

    Does anyone please know how I can adapt the code to check to see whether the checked property of the mailmerge record has actually been set to true and then proceed only in those instances?

    Any help provided would be greatly appreciated,

    Thanks,

    quetzalc0atl

  2. #2
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    For that, you'd need to supply the macro with its own means of identifying which records to merge. A simpler approach might be to do the filtered merge manually then use the 'Split Merged Output to Separate Documents' example here: http://windowssecrets.com/forums/sho...ips-amp-Tricks
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  3. #3
    New Lounger
    Join Date
    Sep 2014
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank you kindly for getting back to me - unfortunately I think this might confuse my users further. They already accuse MS Word of being a witch.

    I have seen the "included" property of the mail merge data source once or twice while trying to find a solution to this ...is it not possible to do something like
    for allrecords where includedproperty = TRUE do xyz??

  4. #4
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    You could have the macro do its own filtering, buth that's fairly inflexible. For example, the following is taken from the 'Send Mailmerge Output to Individual Files' example here: http://windowssecrets.com/forums/sho...ips-amp-Tricks
    Code:
    Sub Filter_Merge_To_Individual_Files()
    'Merges one record at a time to the folder containing the mailmerge main document.
    ' Sourced from: http://windowssecrets.com/forums/showthread.php/163017-Word-Mailmerge-Tips-amp-Tricks
    Application.ScreenUpdating = False
    Dim StrFolder As String, StrName As String, MainDoc As Document, i As Long, j As Long
    Set MainDoc = ActiveDocument
    With MainDoc
      StrFolder = .Path & Application.PathSeparator
      For i = 1 To .MailMerge.DataSource.RecordCount
        With .MailMerge
          .Destination = wdSendToNewDocument
          .SuppressBlankLines = True
          With .DataSource
            .FirstRecord = i
            .LastRecord = i
            .ActiveRecord = i
            If Trim(.DataFields("MMFileName")) = "" Then Exit For
            StrName = .DataFields("MMFileName")
          End With
          If Trim(.DataSource.DataFields("MyField")) = "x" Then .Execute Pause:=False
        End With
        For j = 1 To 255
          Select Case j
            Case 1 To 31, 33 To 45, 47, 58 To 64, 91 To 94, 96, 123 To 141, 143 To 149, 152 To 157, 160 To 180, 182 To 191
            StrName = Replace(StrName, Chr(j), "")
          End Select
        Next
        StrName = Trim(StrName)
        With ActiveDocument
          .SaveAs FileName:=StrPath & StrName & ".doc", FileFormat:=wdFormatDocument, AddToRecentFiles:=False
          .Close SaveChanges:=False
        End With
      Next i
    End With
    Application.ScreenUpdating = True
    End Sub
    Simply supply the right field name for MyField. As coded, it is assumed you want the output if the field contains 'x'. You can change that to another value or the argument to 'not x' (i.e. = becomes <>).
    Cheers,

    Paul Edstein
    [MS MVP - Word]

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
  •