To do a mail merge, users write a letter using the BulkLttr.DOT template. This template includes standard merge fields for letters and uses Header.DOC, a table with the column headings from the BulkMail database, as the data source for these fields. Using BulkLttr.DOT, users can write new text without bothering with fields.
Once users write the form letter for the mail merge, they can perform the merge by choosing the Mail Merge command from the BulkMail menu. The application opens a dialog box that lets users select the form letter and its recipients.
When the BulkMail dialog box first opens, the drop-down list displays the .DOC files in the current directory. (The current directory is stored in the label above the drop-down list; if this label is empty, the current directory is the Word program directory.) Users can select from this list or click the Change button to select a .DOC file in a different directory.
Most of the code for handling this list is in the ListDocFiles subroutine in modMailMerge. This routine calls the Files function in modMain to retrieve a list of .DOC files in the current directory. The Files function uses the Dir$ function to retrieve files that meet a given specification (such as *.DOC), stores their names in a string array provided by the calling procedure (ListDocFiles), and returns the number of file names retrieved.
The code for the Files function follows:
Function Files(ByVal strPath As String, strFiles() As String) As
Integer Dim counter As Integer Dim strTemp As String ReDim strFiles(1 To 1) On Error GoTo FilesProblem strFiles(1) = Dir$(strPath) If strFiles(1) <> "" Then counter = 1 Do strTemp = Dir$() If strTemp <> "" Then counter = counter + 1 ReDim Preserve strFiles(1 To counter) strFiles(counter) = strTemp End If Loop Until strTemp = "" End If Files = counter Exit Function FilesProblem: 'If the path doesn't exist, return -1. If Err = 76 Then Files = -1 Exit Function 'On any other error, notify user and return -2. ElseIf Err <> 0 Then GlobalErrorMsg "Files", Err Files = -2 Exit Function End If End Function
When users click the Change button, BulkMail calls the cmdChange_Click subroutine in modMailMerge. This routine uses the GetOpenFilename method to open the standard File Open dialog box—with a twist. Rather than opening the selected file, this method returns the file's full path. The selected directory becomes the current directory, and its .DOC files are displayed in the drop-down list. The code follows:
Sub cmdChange_Click() On Error GoTo ChangeError Dim strCurDir As String Dim strDocDirectory As String Dim strFullPath As String Dim strPath As String Dim strFileName As String 'Store the current directory in strCurDir and make the 'directory displayed on the dialog box the current directory. strCurDir = CurDir() strDocDirectory = objDocDirectory.Caption ChDir Left$(strDocDirectory, Len(strDocDirectory) - 1) 'Open the File Open dialog box to allow the user to 'select a mail merge document. (The GetOpenFilename method 'makes it possible to get the full path for the selected 'document from the File Open dialog box without actually 'opening the document.) strFullPath = Application.GetOpenFilename _ (fileFilter:="Word Docs (*.doc),*.doc", _ Title:="Select Mail Merge Document") 'When the user clicks the OK button... If strFullPath <> "False" Then '...extract the path and file name from the full path for 'the document chosen by the user. strPath = Extract(strFullPath, 0) strFileName = Extract(strFullPath, 1) 'Write the path in the current directory label on the dialog box 'and call ListDocFiles to fill document list box. objDocDirectory.Caption = strPath ListDocFiles strPath objDocList.Text = strFileName End If RestoreDirectory: 'Restore the original directory. ChDir strCurDir Exit Sub ChangeError: 'If the directory displayed on the dialog box isn't a valid 'directory, leave the current directory at its original setting. If Err = 76 Then Resume Next ElseIf Err <> 0 Then GlobalErrorMsg "cmdChange_Click", Err Resume RestoreDirectory End If End Sub