Private Sub SendMailWithWordAttachment()
'Created by Helen Feddema 5-19-2000
'Last modified 14-Jan-2010
On Error GoTo ErrorHandler
Dim dbs As DAO.Database
Dim appOutlook As New Outlook.Application
Dim itm As Outlook.MailItem
Dim strFileName As String
Dim strDBName As String
Set dbs = CurrentDb
strFileName = SelectFile
'Create new mail message and attach text file to it
Set itm = appOutlook.CreateItem(olMailItem)
With itm
.To = "John Doe"
.Subject = "Word document you requested"
.Body = "Your message"
.Attachments.Add strFileName
.Display
End With
ErrorHandlerExit:
Exit Sub
ErrorHandler:
MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description
Resume ErrorHandlerExit
End Sub
Public Function SelectWordDoc() As String
'Requires Office XP (2002) or higher
'Requires a reference to the Microsoft Office Object Library
'Created by Helen Feddema 3-Aug-2009
'Last modified 14-Jan-2010
On Error GoTo ErrorHandler
Dim fd As Office.FileDialog
Dim varSelectedItem As Variant
Dim strFileNameAndPath As String
'Create a FileDialog object as a File Picker dialog box.
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
'Set AllowMultiSelect to True to allow selection of multiple files
.AllowMultiSelect = False
.Title = "Browse for File"
.ButtonName = "Select"
.Filters.Clear
.Filters.Add "Documents", "*.doc; *.docx", 1
.InitialView = msoFileDialogViewDetails
If .Show = -1 Then
'Get selected item in the FileDialogSelectedItems collection
For Each varSelectedItem In .SelectedItems
strFileNameAndPath = CStr(varSelectedItem)
Next varSelectedItem
Else
Debug.Print "User pressed Cancel"
strFileNameAndPath = ""
End If
End With
SelectWordDoc = strFileNameAndPath
ErrorHandlerExit:
Set fd = Nothing
Exit Function
ErrorHandler:
MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description
Resume ErrorHandlerExit
End Function
|