Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #3  
Old 01-04-2020, 04:12 PM
chunkyfresh chunkyfresh is offline Mail Merge from Excel to Separate Documents and Different Folders Windows 10 Mail Merge from Excel to Separate Documents and Different Folders Office 2016
Novice
Mail Merge from Excel to Separate Documents and Different Folders
 
Join Date: Jan 2020
Posts: 2
chunkyfresh is on a distinguished road
Default

Hello,

Thanks for the article. I'm having a difficult time understanding a lot of it since my knowledge of VBA is basic. Sorry about that. I have a few questions.

Where do I indicate the Data Source in this script? Is there a way I can call it directly by it? For example, I tell the script the data source is file of name "Data Source" and it is located at X path.




Code:
Sub Merge_To_Individual_Files()

Application.ScreenUpdating = False

Dim StrFolder As String

Dim StrName As String 

Dim MainDoc As Document 

Dim i As Long 

Dim j As Long

Const StrNoChr As String = """*./\:?|"
Set MainDoc = ActiveDocument
With MainDoc
  'StrFolder = .Path & "\"
  With .MailMerge
    .Destination = wdSendToNewDocument
    .SuppressBlankLines = True
    On Error Resume Next
    For i = 1 To .DataSource.RecordCount
      With .DataSource
        .FirstRecord = i
        .LastRecord = i
        .ActiveRecord = i
        StrFolder = .DataFields("Supervisor") & "\"
        StrName = i & "-" & .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
        '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
  End With
End With
Application.ScreenUpdating = True
End Sub
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
Mail merge separate recipients same file Cafeaulait Outlook 6 05-10-2018 08:00 PM
Mail Merge from Excel to Separate Documents and Different Folders Mail merge into separate list mds Mail Merge 5 04-06-2016 02:17 AM
Mail Merge from Excel to Separate Documents and Different Folders Merge info into 5 separate documents kenjmaraty Word 3 07-06-2015 07:30 AM
Mail Merge from Excel to Separate Documents and Different Folders Split each page of mail merge into separate pdf Declan Mail Merge 1 05-28-2014 02:02 AM
Mail Merge from Excel to Separate Documents and Different Folders Creating separate file for each mail merge doshshirl Mail Merge 3 02-15-2014 08:49 PM

Other Forums: Access Forums

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