I needed a button to send information from the text fields in a form as the body of an email, and then attach the attachments that are in the attachment field "IssuePics." I have read hours of forums and found the code that turns the text fields into the body of the email, and I thought I found the code to attach the attachments. I'm having trouble merging the two.
This is the send attachments portion:
And this is the one I found to send the fields in the body:
I really don't know how to merge them and when I ran the code to attach the attachment field, I got a compile error: "User-defined type not defined."
I'm using Access 2010 and Office 2010.
I appreciate any and all help most graciously! Thank you.
This is the send attachments portion:
Code:
Dim appOutLook As Outlook.Application
Dim MailOutLook As Outlook.MailItem
Dim OutlookAttach As Outlook.Attachment
Set appOutLook = CreateObject("Outlook.Application")
Set MailOutLook = appOutLook.CreateItem(olMailItem)
Dim db As DAO.Database
Dim rsParent As DAO.Recordset2
Dim rsChild As DAO.Recordset2
Set db = CurrentDb
Set rsParent = Me.Recordset
rsParent.OpenRecordset
Set rsChild = rsParent.Fields("Attachments").Value
While Not rsChild.EOF
If Dir("C:\dbtemp", vbDirectory) = "" Then
MkDir ("C:\dbtemp")
Else
'do nothing for the "C:\dbtemp" directory already exists
'MsgBox "C:\dbtemp\ directory already exists"
End If
rsChild.OpenRecordset
rsChild.Fields("FileData").SaveToFile ("c:\dbtemp\")
rsChild.MoveNext
Wend
With MailOutLook
.BodyFormat = olFormatRichText
'.To = "email address"
'.CC = " "
.Subject = "test"
Dim fso As Object, SourceFolder As Object, SourceFile As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Set SourceFolder = fso.GetFolder("C:\dbtemp\")
For Each SourceFile In SourceFolder.Files
.Attachments.Add SourceFolder.Path & "\" & SourceFile.Name
Next
'Send email
'.DeleteAfterSubmit = True 'This would let Outlook send the note without storing it in your sent bin
'.Send
Kill "C:\dbtemp\*.*" ' delete all files in the folder
RmDir "C:\dbtemp\" ' delete folder
End With
And this is the one I found to send the fields in the body:
Code:
Dim oFilesys, oTxtStream As Object
Dim txtHTML As String
Dim olApp As Object
Dim objMail As Object
DoCmd.OpenReport "ReviewWorkOrdeMROR", acViewPreview, , "WorkOrderID=" & WorkOrderID, acHidden
DoCmd.OutputTo acOutputReport, strReviewWorkOrdeMROR, acFormatHTML, "C:\temp\" & strReviewWorkOrdeMROR & ".HTML", False
Set oFilesys = CreateObject("Scripting.FileSystemObject")
Set oTxtStream = oFilesys.OpenTextFile("C:\temp\" & strReviewWorkOrdeMROR & ".HTML", 1)
txtHTML = oTxtStream.ReadAll
oTxtStream.Close
Set oTxtStream = Nothing
Set oFilesys = Nothing
'Set olApp = GetObject(, "Outlook.Application") 'See if Outlook is open
'If Err Then 'Outlook is not open
Set olApp = CreateObject("Outlook.Application") 'Create a new instance of Outlook
'End If
Set objMail = olApp.CreateItem(olMailItem)
With objMail
''.BodyFormat = olFormatHTML
.HTMLBody = txtHTML
'.Recipients.Add ""
.Subject = ""
'.Send if you want to send it directly without displaying on screen
.Display
End With
'you can delete the outputted file if you want
Kill "c:\temp\" & strReviewWorkOrdeMROR & ".HTML"
Set olApp = Nothing
Set objMail = Nothing
I really don't know how to merge them and when I ran the code to attach the attachment field, I got a compile error: "User-defined type not defined."
I'm using Access 2010 and Office 2010.
I appreciate any and all help most graciously! Thank you.