Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 08-28-2024, 03:04 PM
DaniMul DaniMul is offline Word doc - Mail Merge Document returns compile error if only one record available Windows 11 Word doc - Mail Merge Document returns compile error if only one record available Office 2021
Novice
Word doc - Mail Merge Document returns compile error if only one record available
 
Join Date: Aug 2024
Location: New Zealand
Posts: 4
DaniMul is on a distinguished road
Default Word doc - Mail Merge Document returns compile error if only one record available

Hi,



I have been to the https://www.msofficeforums.com/mail-...ps-tricks.html and have adjusted the code to suit what I need it to do and have named the macro 'MailMergeToDoc' .

So when we click 'Edit Individual Documents' (In the finish & Merge tab) the macro clicks in and runs through the code saving the multiple documents as separate pages BUT if there is only one record it give a compile error that my next has no for.

I have looked through and there are two 'for' and two 'next' so one will open and close the other. it only errors if there is one record and no next record so how can I close this so if no next record it still saves and completes the macro. Code below (Sorry if hard to read I am unsure how to attach like the other codes I see in these forums)


Code:
Sub MailMergeToDoc()
'
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 & ""
  StrFolder = Replace(StrFolder, "TEMPLATES for ", "")
  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("Property")) = "" Then Exit For
                StrName = .DataFields("Property") & " - " & .DataFields("Formated_Date") & " " & .DataFields("Premises_")
            End With
    On Error GoTo NextRecord
      .Execute Pause:=False
      ' skip over unticked rejections
        If Err.Number = 5631 Then
        Err.Clear
        GoTo NextRecord
      For j = 1 To Len(StrNoChr)
        StrName = Replace(StrName, Mid(StrNoChr, j, 1), "_")
    Next
      StrName = Trim(StrName)
    With ActiveDocument
        'Add the name to the footer
        '.Sections(1).Footers(wdHeaderFooterPrimary).Range.InsertBefore StrName
        ' 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
NextRecord:
       Next i 'THIS IS WHERE COMPILE ERROR HAPPENS IF ONLY ONE RECORD'
  End With
End With
Application.ScreenUpdating = True
End Sub
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
Word doc - Mail Merge Document returns compile error if only one record available supress mail merge error document vincenzo345 Mail Merge 2 10-10-2017 12:03 PM
Word doc - Mail Merge Document returns compile error if only one record available Mail Merge Next Record If rule RHensley Mail Merge 10 03-07-2017 08:05 AM
avoid duplicete record and merge the record with the existed record hemant.behere Excel 0 01-10-2012 02:53 AM
compile error in Word raco Word 0 09-28-2010 12:40 PM
Extraneous record in Word Mail Merge aldo13 Mail Merge 0 01-23-2006 01:58 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 08:35 AM.


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