Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 12-04-2017, 04:26 AM
beefcake2000 beefcake2000 is offline VBA code to save to individual files and skipif function Windows 7 64bit VBA code to save to individual files and skipif function Office 2010 64bit
Novice
VBA code to save to individual files and skipif function
 
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
 

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA code to save to individual files and skipif function Save mailmerge records as individual word files using VBA beefcake2000 Word VBA 3 11-10-2017 09:55 PM
VBA code to save to individual files and skipif function Run Code on all files and save files as .docx Plokimu77 Word VBA 4 06-05-2016 04:41 PM
VBA code to save to individual files and skipif function 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

Other Forums: Access Forums

All times are GMT -7. The time now is 02:07 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft