M
MS Access Question
I am trying to send an automated email message with an MS Word attachment
using MS Access and MS Outlook. Everything works fine until I use the
Attachment.Add command, I get an error message "Cannot write to file..."
I am not trying to write a file, I am merely trying to attach an existing
file... any thoughts?
Option Compare Database
Option Explicit
Private Sub cmdMerge_Click()
On Error GoTo Err_cmdMerge_Click
Dim db As DAO.Database, rst As DAO.Recordset
Dim First As String
Dim Email As String
Set db = CurrentDb
Set rst = db.OpenRecordset("qry_JobContactMerge")
DoEvents
rst.MoveFirst
Do Until rst.EOF
First = rst![FIRSTNAME]
DoEvents
Call SendEmail(Email, First)
Me.TxtProcess = rst![EMAILName]
rst.MoveNext
Loop
Exit_cmdMerge_Click:
Exit Sub
Err_cmdMerge_Click:
MsgBox Err.Description
Resume Exit_cmdMerge_Click
End Sub
Private Sub cmdQuit_Click()
On Error GoTo Err_cmdQuit_Click
If Me.Dirty Then Me.Dirty = False
DoCmd.Quit
Exit_cmdQuit_Click:
Exit Sub
Err_cmdQuit_Click:
MsgBox Err.Description
Resume Exit_cmdQuit_Click
End Sub
'******begin code*****
Function SendEmail(Email As String, First As String)
Dim strEmail, strBody As String
Dim objOutlook As Outlook.Application
Dim objEmail As Outlook.MailItem
'**creates an instance of Outlook
Set objOutlook = CreateObject("Outlook.application")
Set objEmail = objOutlook.CreateItem(olMailItem)
'**************************************************************
'*create string with email address
strEmail = EMAIL
'strBody = txtTDate & Chr(13) & Chr(13)
strBody = strBody & "Hello " & First & "," & Chr(13) & Chr(13)
strBody = strBody & "Thank you for your recent correspondence." & Chr(13)
strBody = strBody & "Sincerely," & Chr(13) & Chr(13)
'***creates and sends email
With objEmail
.To = strEmail
.SUBJECT = "My Document is attached"
.body = strBody
.Attachments.Add ("C:\Users\John\Documents\MSWordDoc2003Format.doc")
'add attachment
.Send
End With
Set objEmail = Nothing
objOutlook.Quit
Exit Function
End Function
'****end code*****
using MS Access and MS Outlook. Everything works fine until I use the
Attachment.Add command, I get an error message "Cannot write to file..."
I am not trying to write a file, I am merely trying to attach an existing
file... any thoughts?
Option Compare Database
Option Explicit
Private Sub cmdMerge_Click()
On Error GoTo Err_cmdMerge_Click
Dim db As DAO.Database, rst As DAO.Recordset
Dim First As String
Dim Email As String
Set db = CurrentDb
Set rst = db.OpenRecordset("qry_JobContactMerge")
DoEvents
rst.MoveFirst
Do Until rst.EOF
First = rst![FIRSTNAME]
DoEvents
Call SendEmail(Email, First)
Me.TxtProcess = rst![EMAILName]
rst.MoveNext
Loop
Exit_cmdMerge_Click:
Exit Sub
Err_cmdMerge_Click:
MsgBox Err.Description
Resume Exit_cmdMerge_Click
End Sub
Private Sub cmdQuit_Click()
On Error GoTo Err_cmdQuit_Click
If Me.Dirty Then Me.Dirty = False
DoCmd.Quit
Exit_cmdQuit_Click:
Exit Sub
Err_cmdQuit_Click:
MsgBox Err.Description
Resume Exit_cmdQuit_Click
End Sub
'******begin code*****
Function SendEmail(Email As String, First As String)
Dim strEmail, strBody As String
Dim objOutlook As Outlook.Application
Dim objEmail As Outlook.MailItem
'**creates an instance of Outlook
Set objOutlook = CreateObject("Outlook.application")
Set objEmail = objOutlook.CreateItem(olMailItem)
'**************************************************************
'*create string with email address
strEmail = EMAIL
'strBody = txtTDate & Chr(13) & Chr(13)
strBody = strBody & "Hello " & First & "," & Chr(13) & Chr(13)
strBody = strBody & "Thank you for your recent correspondence." & Chr(13)
strBody = strBody & "Sincerely," & Chr(13) & Chr(13)
'***creates and sends email
With objEmail
.To = strEmail
.SUBJECT = "My Document is attached"
.body = strBody
.Attachments.Add ("C:\Users\John\Documents\MSWordDoc2003Format.doc")
'add attachment
.Send
End With
Set objEmail = Nothing
objOutlook.Quit
Exit Function
End Function
'****end code*****