home *** CD-ROM | disk | FTP | other *** search
- Private Sub cmdMailMerge_Click()
- Dim lcQueryName As String, lcDataFile As String, lcMergeDoc As String
- Dim loQuery As QueryDef, db As Database, SQL As String
- Dim CRLF As String * 2, X As Variant
-
- ' Convenient short-hand form for carriage-return & line-feed
- CRLF = Chr(13) + Chr(10)
-
- ' DataPath is a global constant defined elsewhere, defining
- ' the location of mailshot data and document files
- lcDataFile = AddBS(DataPath) & UniqueName(8) & ".txt"
-
- lcQueryName = UniqueName(8) ' Get a random, unique-ish file name
-
- ' Establish a full path to the merge document
- lcMergeDoc = "" & txtMergeDoc ' This deals with a null
- If lcMergeDoc = JustFName(lcMergeDoc) Then
- lcMergeDoc = AddBS(DataPath) & lcMergeDoc
- End If
-
- X = SysCmd(acSysCmdSetStatus, "Creating query...")
-
- ' Build some SQL to extract the appropriate records. We need to replace
- ' all CRLFs with CHR(11) in case the merge document uses paragraph
- ' spacing, since in Word CRLF is a paragraph break, and CHR(11) is a
- ' line break.
- ' We should only select records for the selected mailshot, and only
- ' where theyÆre not yet marked as sent.
- SQL = "select replaceall(replaceall(Forename & ' ' & Surname & '"
- SQL = SQL & CRLF & "' & [Job Title] & '" & CRLF
- SQL = SQL & "' & [Company Name] & '" & CRLF & "' & [Address] & '"
- SQL = SQL & CRLF & "' & UCase([post code]), '" & CRLF
- SQL = SQL & "', Chr(11)), Chr(11) & Chr(11), Chr(11)) as FullAddress, "
- SQL = SQL & "iif(isempty(contacts.Title) or isnull(contacts.Title), "
- SQL = SQL & "iif(Gender = 'M', 'Mr', 'Ms'), Contacts.Title) & ' ' & "
- SQL = SQL & "Contacts.Surname as Salutation, forename, surname, "
- SQL = SQL & "[company name], replaceall(Address, '" & CRLF
- SQL = SQL & "', chr(11)), [Post Code], forename & ' ' & "
- SQL = SQL & "surname as Contact "
- SQL = SQL & "FROM Companies INNER JOIN (Contacts RIGHT JOIN "
- SQL = SQL & "[Mailshot=Company] ON Contacts.ContactID = "
- SQL = SQL & "[Mailshot=Company].ContactID) ON Companies.CompanyID = "
- SQL = SQL & "[Mailshot=Company].CompanyID "
- SQL = SQL & "where MailshotID = " & MailshotID & " and IsNull(Sent)"
-
- ' Create a querydef using the SQL to use for the export
- Set db = CurrentDb()
- Set loQuery = db.CreateQueryDef(lcQueryName, SQL)
-
- X = SysCmd(acSysCmdClearStatus)
-
- ' The useful TransferText macro action is called to do the export
- DoCmd.TransferText acExportMerge, , lcQueryName, lcDataFile
-
- ' Use the new Access 95 RecordsAffected property to report how many
- ' records were exported for debugging purposes.
- Debug.Print db.RecordsAffected
-
- X = SysCmd(acSysCmdSetStatus, "Deleting query...")
-
- db.QueryDefs.Delete lcQueryName ' Delete the spent querydef
-
- X = SysCmd(acSysCmdClearStatus)
-
- ' If thereÆs no merge document specified, or it doesnÆt yet exist
- ' then just display a helpful message
- If IsNull(txtMergeDoc) Or IsEmpty(txtMergeDoc) Or
- IsEmpty(Dir("" & txtMergeDoc)) Then
- MsgBox "No merge has taken place because you did not specify
- a Merge Document or the document did not exist.
- However, the data has been exported to the file '" & lcDataFile & "',
- and may now be used for setting up a Merge Document.", 16, "Mail Merge"
- Else
- X = SysCmd(acSysCmdSetStatus, "Starting Word...")
-
- If TypeName(loWord) <> "wordbasic" Then
- On Error Resume Next
-
- ' Try getting a handle to an open copy of Word
- Set loWord = GetObject(, "word.basic")
-
- ' If Word isnÆt running, start it up
- If Err <> 0 Then
- Set loWord = CreateObject("word.basic")
- End If
- On Error GoTo 0
- End If
-
- X = SysCmd(acSysCmdSetStatus, "Running MailMerge...")
-
- loWord.AppShow ' Unhide Word
- loWord.FileOpen (lcMergeDoc) ' Open the merge document
- loWord.MailMergeOpenDataSource lcDataFile ' Open the data
- loWord.MailMergeToDoc ' Merge document & data to a new document
-
- X = SysCmd(acSysCmdClearStatus)
-
- ' Check with the user whether or not the operation worked
- If Confirm("Did the mail-merge operation work?", "Mail Merge") Then
- X = SysCmd(acSysCmdSetStatus, "Marking merged records...")
-
- SQL = "UPDATE [Mailshot=Company] SET Sent = Date()"
- SQL = SQL & " where IsNull(Sent) and MailShotID = " & MailshotID
- db.Execute SQL
- End If
-
- X = SysCmd(acSysCmdClearStatus)
- End If
- End Sub
-
-
- Function AddBS(ByVal PathName As String) As String
- ' Adds a backslash to a path name, if there isn't already one there
- PathName = Trim(UCase(PathName))
-
- If (InStr("\:", Right(PathName, 1)) = 0) And PathName <> "" Then
- PathName = PathName + "\"
- End If
-
- AddBS = PathName
- End Function
-
-
- Function UniqueName(ByVal Length As Integer)
- ' Makes a "unique" identifier of specified length out of random letters
-
- Dim TempName As String
- Dim Counter As Integer
- Dim Char as Integer
-
- Const LowerBound = 65 ' A
- Const UpperBound = 90 ' Z
-
- Randomize
-
- TempName = ""
- For Counter = 1 To Length
- Char = (UpperBound - LowerBound + 1) * Rnd + LowerBound
- TempName = TempName + Chr(Char)
- Next
-
- UniqueName = TempName
- End Function
-
-
- Function JustFName(ByVal FileName As String) As String
- ' Returns just the filename (i.e. no path) from FileName
- If RAT("\", FileName) > 0 Then
- FileName = Mid(FileName, RAT("\", FileName) + 1, 255)
- End If
-
- If InStr(FileName, ":") > 0 Then
- FileName = Mid(FileName, InStr(FileName, ":") + 1, 255)
- End If
-
- JustFName = Trim(UCase(FileName))
- End Function
-