Word

Automate saving the new document produced during a mail merge

Ease of Use

Intermediate

Version tested with

2003 

Submitted by:

mdmackillop

Description:

The Save Path is created automatically from mergefield information and the user is prompted for a save name for the new document when it is created. Optionally, a default SaveName can be used for specific standard documents. 

Discussion:

Use in conjunction with a database or other programme. Data passed to the merge document is used to determine the save path, as well as in the creation of the new document. File names will have an incremental suffix if the name has previously been used. 

Code:

instructions for use

			

'//In ThisDocument module Private Sub Document_Open() DocSave "TestSave" End Sub '//In a standard module or in Normal.dot for general use 'DocName is the InputBox default if approriate for a standard letter etc. Sub DocSave(DocName) Dim Rec As Integer Dim ToBeSaved As String, MergeFileName As String, JobNo As String, Direct As String Dim Project As String, SaveName As String, SaveAsName As String Dim Length As Integer Dim Message As String, Title As String, Default As String, MyValue As String Dim DataF As Variant, DRecord As String 'Set Drive\Folder to save to Const SaveRoot = "C:\Atest\" 'Close custom toolbar, delete if not required On Error Resume Next CommandBars("Merge File").Visible = False 'Input box for filename Message = "Enter Sub-Folder/Filename for saving" & Chr(13) & _ "NB - New sub-folders will not be created" & Chr(13) & Chr(13) & _ "Press Cancel to proceed without saving." ' Set prompt. Title = "Document File Name" ' Set title. 'Create name for document SaveName = InputBox(Message, Title, DocName) 'Remember name to close it later MergeFileName = ActiveDocument.Name 'Skip process if save not required If SaveName = "" Then ToBeSaved = "No" GoTo MailMergeLine End If 'Close standard toolbars if visible On Error Resume Next CommandBars("MailMerge").Visible = False On Error Resume Next CommandBars("Web").Visible = False 'Obtain SavePath data from first two merge fields Rec = 0 For Each DataF In _ Documents(MergeFileName).MailMerge.DataSource.DataFields Rec = Rec + 1 DRecord = DataF.Value If Rec = 1 Then JobNo = DRecord If Rec = 2 Then Project = DRecord If Rec = 3 Then GoTo MailMergeLine Next DataF 'Create new merged document MailMergeLine: With ActiveDocument.MailMerge .Destination = wdSendToNewDocument .Execute End With 'Go to end if save not required If ToBeSaved = "No" Then GoTo LastLine 'Create SavePath from obtained data Direct = SaveRoot & JobNo & " " & Project & "\" 'Trim file extension if entered in input box Length = Len(SaveName) If Right(SaveName, 4) = ".doc" Then SaveName = Left(SaveName, Length - 4) End If 'Create name for file SaveAsName = SaveName & ".doc" 'Open Directory for saving ChangeFileOpenDirectory Direct 'Check for previous instances of the SaveName, if found add incremental suffix With Application.FileSearch .NewSearch .LookIn = Direct .SearchSubFolders = False .FileName = SaveName & "*" .FileType = msoFileTypeAllFiles If .Execute() > 0 Then SaveAsName = SaveName & "-" & .FoundFiles.Count & ".doc" End If End With 'ChangeFileOpenDirectory Direct ActiveDocument.SaveAs FileName:=SaveAsName 'Confirm path and filename used MsgBox ActiveDocument.Path & Application.PathSeparator & ActiveDocument.Name LastLine: 'Close mailmerge document Windows(MergeFileName).Close savechanges:=False End Sub

How to use:

  1. To use the sample file, create a folder and subfolder C:\Atest\123 Sample Project\
  2. "123" is the JobNo field in the mailmerge and "Sample Project" is the Project field.
  3. MyData.rtf is a typical merge source which might be created from Access. AutoSaveDoc is the mail merge file: save both sample files into C:\Atest.
 

Test the code:

  1. Open AutoSaveDoc. You should be prompted for a file name, or you can accept the default. The new document should be saved in the sub-folder.
 

Sample File:

MyData.zip 14.74KB 

Approved by mdmackillop


This entry has been viewed 173 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express