Microsoft Office Forums

Go Back   Microsoft Office Forums > Microsoft Word > Mail Merge

Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 12-04-2017, 04:26 AM
beefcake2000 beefcake2000 is offline Windows 7 64bit Office 2010 64bit
Novice
 
Join Date: Nov 2017
Posts: 6
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
  #2  
Old 12-04-2017, 01:31 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 16,666
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

There are numerous ways of addressing the issue. One is to use the mailmerge filters to exclude the unwanted records instead of the SKIPIF field; another is to modify the code to enclose the relevant part in an IF test that skips over the relevant records.
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #3  
Old 12-05-2017, 03:19 AM
beefcake2000 beefcake2000 is offline Windows 7 64bit Office 2010 64bit
Novice
 
Join Date: Nov 2017
Posts: 6
beefcake2000 is on a distinguished road
Default

Thanks! Filtering is indeed an easy way to fix this
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Save mailmerge records as individual word files using VBA beefcake2000 Word VBA 3 11-10-2017 09:55 PM
Run Code on all files and save files as .docx Plokimu77 Word VBA 4 06-05-2016 04:41 PM
Mail Merge To Individual PDF Files iamrickdeans Mail Merge 1 01-15-2014 12:46 AM
VBA code to export individual task lists to Excel OTPM Excel Programming 0 05-24-2011 12:23 PM
VBA Code to Print Individual Resource Lists to a pdf File OTPM Project 0 05-24-2011 12:21 PM


All times are GMT -7. The time now is 11:50 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
MSOfficeForums.com is not affiliated with Microsoft