Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 10-03-2020, 04:57 AM
dlafko1 dlafko1 is offline Mail Merge Code Windows 10 Mail Merge Code Office 2016
Advanced Beginner
Mail Merge Code
 
Join Date: Apr 2019
Posts: 31
dlafko1 is on a distinguished road
Default Mail Merge Code

I am trying to get this code to work (novice to vba ) this was something shared.



I keep getting an error 5174 couldn't find your file. I know the location and file names are correct any help

Code:
Option Explicit

Const FOLDER_SAVED As String = "C:\Users\david\Desktop\PhotoRelease\"
Const SOURCE_FILE_PATH As String = "C:\Users\david\Desktop\PhotoRelease\ScoutRoster.xls\"

Sub MailMerge_Automation()
Dim MainDoc As Document, TargetDoc As Document
Dim dbPath As String
Dim recordNumber As Long, totalRecord As Long

Set MainDoc = ActiveDocument
With MainDoc.MailMerge
    
        '// if you want to specify your data, insert a WHERE clause in the SQL statement
        
        .OpenDataSource Name:=SOURCE_FILE_PATH, sqlstatement:="SELECT * FROM [database$]"
            
        totalRecord = .DataSource.RecordCount

        For recordNumber = 1 To totalRecord
        
            With .DataSource
                .ActiveRecord = recordNumber
                .FirstRecord = recordNumber
                .LastRecord = recordNumber
            End With
            
            .Destination = wdSendToNewDocument
            .Execute False
            
            Set TargetDoc = ActiveDocument

            TargetDoc.SaveAs2 FOLDER_SAVED & .DataSource.DataFields("Name").Value & ".docx", wdFormatDocumentDefault
            TargetDoc.ExportAsFixedFormat FOLDER_SAVED & .DataSource.DataFields("Name").Value & ".pdf", exportformat:=wdExportFormatPDF
            
            TargetDoc.Close False
            
            Set TargetDoc = Nothing
                    
        Next recordNumber

End With

Set MainDoc = Nothing
End Sub
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA code to CC multiple people in mail merge by word kuankailok Mail Merge 3 05-04-2020 02:08 PM
Mail Merge Code VBA code not in module of new document produced by mail merge MP1989 Mail Merge 3 09-10-2018 02:16 PM
Mail Merge Code Mail Merge Code Issue Andrewwill Mail Merge 6 01-06-2018 10:25 PM
Mail Merge Code (Default Display) ochiha_ita Mail Merge 3 04-22-2013 04:04 AM
Mail merge Field Code Manipulation macjnr Mail Merge 0 09-10-2009 11:37 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:57 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