Check for a previously saved letter in the documents folder, and append an incremented number
to the save name if one is found:
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 = “Letter “ & CStr(i) & “ to “ & _
Me![FirstName] & “ “ & Me![LastName]
strSaveName = strSaveName & “ on “ & strShortDate _
& “.doc”
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
With appWord
.Visible = True
.Selection.WholeStory
.Selection.Fields.Update
Debug.Print “Going to save as “ & strSaveName
.ActiveDocument.SaveAs strSaveNamePath
.Activate
.Selection.EndKey Unit:=wdStory
End With
ErrorHandlerExit:
Exit Sub
ErrorHandler:
If Err = 429 Then
‘Word is not running; open Word with CreateObject
Set appWord = CreateObject(Class:=”Word.Application”)
Resume Next
Part II Writing VBA Code to Exchange Data between Office Components