andrewpiconnect
asked on
access vba create outlook email, send and save
Hi,
I am wishing to to have access automatically save an email that is created by a user via the database.
The following code creates the email pefectly and even lets me know whether it was sent or not.
This is working fine.
However, i now wish to add to this code the ability to automatically save the email after the email has been sent.
I have a .SaveAs line of code further down in the following code but this only works when it is placed in its current position.
If i move it a few lines down as indicated below then the email does not save.
Any ideas anyone?
Dim objOutlook As Object
Set objOutlook = GetObject(, "Outlook.Application") ' Determine if Outlook is open
If Err <> 0 Then
MsgBox "You need to open 'Outlook' in order to invite renewal by email.", vbInformation, "Outlook Not Open Alert"
Set objOutlook = Nothing
Exit Sub
Else
Dim strBody As String
Dim strEmail As String
Dim strSubject As String
Dim objMailItem As Object
Const olMailItem As Integer = 0
Set objMailItem = objOutlook.CreateItem(olMa ilItem)
strEmail = strEmailAdd
strSubject = "My Subject Text - Our Ref: " & Me.PolicyRef
strBody = "My email message to go here"
With objMailItem
.To = strEmail
.Subject = strSubject
.Body = strBody
'****** save email here but if user changes text then it wil not be saved
.SaveAs "C:\myfolder\email.msg"
' ******
.Display True 'make outlook modal
' This is used just to determine if the email was sent as there are table updates that follow if the email was sent or not
On Error Resume Next 'check if the email was sent or just closed
Dim bSent As Boolean
bSent = objMailItem.Sent 'just used to get a error,
If Err = 0 Then
'no message, email closed, objMailItem still exists
bSent = False
Else
'message sent or saved, objMailItem is null
bSent = True
'****** I WANT THE .SAVEAS to go here !!!!
End If
End With
Set objOutlook = Nothing
Set objMailItem = Nothing
End If
I am wishing to to have access automatically save an email that is created by a user via the database.
The following code creates the email pefectly and even lets me know whether it was sent or not.
This is working fine.
However, i now wish to add to this code the ability to automatically save the email after the email has been sent.
I have a .SaveAs line of code further down in the following code but this only works when it is placed in its current position.
If i move it a few lines down as indicated below then the email does not save.
Any ideas anyone?
Dim objOutlook As Object
Set objOutlook = GetObject(, "Outlook.Application") ' Determine if Outlook is open
If Err <> 0 Then
MsgBox "You need to open 'Outlook' in order to invite renewal by email.", vbInformation, "Outlook Not Open Alert"
Set objOutlook = Nothing
Exit Sub
Else
Dim strBody As String
Dim strEmail As String
Dim strSubject As String
Dim objMailItem As Object
Const olMailItem As Integer = 0
Set objMailItem = objOutlook.CreateItem(olMa
strEmail = strEmailAdd
strSubject = "My Subject Text - Our Ref: " & Me.PolicyRef
strBody = "My email message to go here"
With objMailItem
.To = strEmail
.Subject = strSubject
.Body = strBody
'****** save email here but if user changes text then it wil not be saved
.SaveAs "C:\myfolder\email.msg"
' ******
.Display True 'make outlook modal
' This is used just to determine if the email was sent as there are table updates that follow if the email was sent or not
On Error Resume Next 'check if the email was sent or just closed
Dim bSent As Boolean
bSent = objMailItem.Sent 'just used to get a error,
If Err = 0 Then
'no message, email closed, objMailItem still exists
bSent = False
Else
'message sent or saved, objMailItem is null
bSent = True
'****** I WANT THE .SAVEAS to go here !!!!
End If
End With
Set objOutlook = Nothing
Set objMailItem = Nothing
End If
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
If the email has not been sent then i do not want it to try to auto save.
The link you provided was informative but never resolved so not much use im afraid