Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-10-2024, 08:34 PM
Craig Norton Craig Norton is offline Determine number of mailmerge records Windows 10 Determine number of mailmerge records Office 2021
Novice
Determine number of mailmerge records
 
Join Date: Apr 2024
Posts: 1
Craig Norton is on a distinguished road
Default Determine number of mailmerge records


Hi.

I have the below code which I got from this site and it produces individual PDFs. When it runs it cycles through all merge records in the Data Source. The issue I have is that we apply a filter when opening and it may only select say 10 records of the total data source of 200 records. Is there a way to determine the number of merge records (e.g. 10) instead of using datasource.RecordCount which returns 200. The issue is that it continues to cycle through the entire Data Source which takes longer compared to just the actual records being merged.


Code:
Public Sub Merge_To_Individual_Files()
' Sourced from: https://www.msofficeforums.com/mail-merge/21803-mailmerge-tips-tricks.html
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 & "\Email Attachments\"
  With .MailMerge
    .Destination = wdSendToNewDocument
    .SuppressBlankLines = True
    On Error Resume Next
    For i = 1 To .DataSource.RecordCount
      With .DataSource
        .FirstRecord = i
        .LastRecord = i
        .ActiveRecord = i
        If Trim(.DataFields("securitycode")) = "" Then Exit For
        StrName = "W-8BEN Form" & " - " & .DataFields("Portfoliocode") & " " & .DataFields("securitycode") & " " & .DataFields("name")
      End With
      .Execute Pause:=False
      If Err.Number = 5631 Then
        Err.Clear
        GoTo NextRecord
      End If
      For j = 1 To Len(StrNoChr)
        StrName = Replace(StrName, Mid(StrNoChr, j, 1), "_")
      Next
      StrName = Trim(StrName)
      With ActiveDocument
        .SaveAs FileName:=StrFolder & StrName & ".pdf", FileFormat:=wdFormatPDF, AddToRecentFiles:=False
        .Close SaveChanges:=False
      End With
NextRecord:
    Next i
  End With
End With
Application.ScreenUpdating = True

MsgBox ("PDFs have been created.")
End Sub
Reply With Quote
  #2  
Old 04-11-2024, 03:52 PM
Guessed's Avatar
Guessed Guessed is offline Determine number of mailmerge records Windows 10 Determine number of mailmerge records Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,984
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Can you adapt this looping code to fit your requirements? Start by testing it by itself before you start adding your extra code. It appears you have code included in your sample which may not be required with this approach.
Code:
Public Sub Merge_To_Individual_Files()
  Dim StrFolder As String, MainDoc As Document, i As Long
  Set MainDoc = ActiveDocument
  StrFolder = MainDoc.Path & "\"
  With MainDoc.MailMerge
    .Destination = wdSendToNewDocument
    .SuppressBlankLines = True
    .ViewMailMergeFieldCodes = False
    With .DataSource
      .ActiveRecord = wdFirstRecord
      Do Until i = .ActiveRecord  'next record didn't go forward
        MainDoc.SaveAs FileName:=StrFolder & i & "temp.pdf", FileFormat:=wdFormatPDF, AddToRecentFiles:=False
        i = .ActiveRecord
        .ActiveRecord = wdNextRecord
      Loop
    End With
  End With
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Determine number of mailmerge records Show total number of records mailmerge eduzs Mail Merge 1 11-24-2020 09:04 PM
Determine number of mailmerge records Save mailmerge records as individual word files using VBA beefcake2000 Word VBA 3 11-10-2017 09:55 PM
Determine number of mailmerge records When using a 'Master' template, if number of records change, subsequent records will not be read MailMergeConfused Mail Merge 5 07-28-2016 05:07 PM
Determine number of mailmerge records Mailmerge - when some records have photos, and some without smitty777 Mail Merge 3 05-20-2016 08:30 PM
Determine number of mailmerge records Missing records after word mailmerge matthewchin Mail Merge 18 12-11-2015 12:06 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:03 AM.


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