Access.2007.VBA.Bibl..

(John Hannent) #1

Check for a previously saved document in the documents folder, and append an incremented
number to the save name if one is found:


strDocType = _
appWord.ActiveDocument.
BuiltInDocumentProperties(wdPropertyTitle)
strSaveName = strDocType & “ on “ _
& strShortDate & strExtension
i = 2
intSaveNameFail = True
Do While intSaveNameFail
strSaveNamePath = strDocsPath & strSaveName
Debug.Print “Proposed save name and path: “ _
& vbCrLf & strSaveNamePath
strTestFile = Nz(Dir(strSaveNamePath))
Debug.Print “Test file: “ & strTestFile
If strTestFile = strSaveName Then
Debug.Print “Save name already used: “ _
& strSaveName

Create a new save name with the incremented number:


intSaveNameFail = True
strSaveName = strDocType & “ “ & CStr(i) & “ on “ _
& strShortDate & strExtension
strSaveNamePath = strDocsPath & strSaveName
Debug.Print “New save name and path: “ _
& vbCrLf & strSaveNamePath
i = i + 1
Else
Debug.Print “Save name not used: “ & strSaveName
intSaveNameFail = False
End If
Loop

Set the merge data source to the text file just created, and do the merge:


With appWord
.ActiveDocument.MailMerge.OpenDataSource _
Name:=strTextFile, _
Format:=wdOpenFormatText
.ActiveDocument.MailMerge.Destination = _
wdSendToNewDocument
.ActiveDocument.MailMerge.Execute

Working with Word Documents and Templates 6

Free download pdf