View Single Post
 
Old 12-04-2017, 04:26 AM
beefcake2000 beefcake2000 is offline Windows 7 64bit Office 2010 64bit
Novice
 
Join Date: Nov 2017
Posts: 10
beefcake2000 is on a distinguished road
Default VBA code to save to individual files and skipif function

Hi all,

I am using the following VBA code to save mailmerge records to individual files:

Code:
Application.ScreenUpdating = False
Dim StrFolder As String, StrName As String, MainDoc As Document, i As Long, j As Long
Const StrNoChr As String = """*./\:?|"
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("Dossier")) = "" Then Exit For
        'StrFolder = .DataFields("Folder") & Application.PathSeparator
        StrName = .DataFields("Dossier") & "_" & .DataFields("Aanvrager")
      End With
      .Execute Pause:=False
    End With
      For j = 1 To Len(StrNoChr)
        StrName = Replace(StrName, Mid(StrNoChr, j, 1), "_")
      Next
    StrName = Trim(StrName)
    With ActiveDocument
    .SaveAs FileName:=StrFolder & StrName & ".docx", FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False
    'and/or:
    '.SaveAs FileName:=StrFolder & StrName & ".pdf", FileFormat:=wdFormatPDF, AddToRecentFiles:=False
        .Close savechanges:=False
    End With
  Next i
End With
Application.ScreenUpdating = True
This code works fine, but I get an error message ("Runtime error - 5631; Word could not merge the main document with the data source because the data records were empty or no data records matched your query options") when one of the records matches the SKIPIF conditions. Is there any way to solve this?

Thanks!

Last edited by macropod; 12-04-2017 at 01:28 PM. Reason: Added code tags
Reply With Quote