Hi all. Still being a VBA newbie, I can fudge myself through most things but a little stuck on this one.
I am using code I found that uses outlook to send emails. I can get it to attach one file but I need it to be able to attach several that the user selects. I think I need an array to go through the records to find which files have a checkbox that has been marked. I'm not exactly sure how to start.
Here is the code that runs in a module.
Then it says to call the function by using this in the form.
The table I have is named tbl_temp_Email and has the following fields: temp_Select, temp_File_Name, temp_File_Location, temp_File_Discription.
temp_File_Location has the path to the file and temp_File_Name, well obviously has the file's name. temp_Select has a yes/no field that the user checks in a form to choose which files they want to attach to an email.
My problem lays in where to start to get it to go through the records and find the selected ones. I am hoping that I would be able to take those and put in the function call and then let the code in the function take those and attach to the email.
If any one would be able and willing to help me out it would be greatly appreciated.
Thanks,
Matt
I am using code I found that uses outlook to send emails. I can get it to attach one file but I need it to be able to attach several that the user selects. I think I need an array to go through the records to find which files have a checkbox that has been marked. I'm not exactly sure how to start.
Here is the code that runs in a module.
Code:
Function fctnOutlook(Optional FromAddr, Optional Addr, Optional CC, Optional BCC, _
Optional Subject, Optional MessageText, Optional AttachmentPath, Optional Vote As String = vbNullString, _
Optional Urgency As Byte = 1, Optional EditMessage As Boolean = True)
'Code sample from Accessory http://www22.brinkster.com/accessory
Dim objOutlook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim objOutlookRecip As Outlook.Recipient
Dim arrAttach(255) As String
Dim bytAttach As Byte
Dim bytCount As Byte
Set objOutlook = CreateObject("Outlook.Application") 'For conventional Access
' For Access runtime, use: Set objOutlook = CreateObject("Outlook.Application", "localhost")
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
With objOutlookMsg
If Not IsMissing(FromAddr) Then
.SentOnBehalfOfName = FromAddr
End If
If Not IsMissing(Addr) Then
Set objOutlookRecip = .Recipients.Add(Addr)
objOutlookRecip.Type = olTo
End If
If Not IsMissing(CC) Then
Set objOutlookRecip = .Recipients.Add(CC)
objOutlookRecip.Type = olCC
End If
If Not IsMissing(BCC) Then
Set objOutlookRecip = .Recipients.Add(BCC)
objOutlookRecip.Type = olBCC
End If
If Not IsMissing(Subject) Then
.Subject = Subject
End If
If Not IsMissing(MessageText) Then
.Body = MessageText
End If
If Not IsMissing(AttachmentPath) Then
If InStr(1, AttachmentPath, ";", vbTextCompare) > 0 Then
If Right(AttachmentPath, 1) <> ";" Then AttachmentPath = AttachmentPath & ";"
Do Until Len(AttachmentPath) <= 1
arrAttach(bytAttach) = Left(AttachmentPath, InStr(1, AttachmentPath, ";", vbTextCompare) - 1)
bytAttach = bytAttach + 1
AttachmentPath = Mid(AttachmentPath, InStr(1, AttachmentPath, ";", vbTextCompare) + 1)
Loop
Else
arrAttach(bytAttach) = AttachmentPath
End If
For bytCount = 0 To bytAttach - 1
'Check file exists before attaching!
If Len(Dir(arrAttach(bytCount))) > 0 Then
Set objOutlookAttach = .Attachments.Add(arrAttach(bytCount))
Else
MsgBox "Attachment not found.", vbExclamation
End If
Next bytCount
End If
If IsNull(Vote) = False Then
.VotingOptions = Vote
End If
Select Case Urgency
Case 2
.Importance = olImportanceHigh
Case 0
.Importance = olImportanceLow
Case Else
.Importance = olImportanceNormal
End Select
For Each objOutlookRecip In .Recipients
objOutlookRecip.Resolve
Next
If EditMessage Then
.Display
Else
.Save
.Send
End If
End With
Set objOutlook = Nothing
End Function
Then it says to call the function by using this in the form.
Code:
fctnoutlook(,"Joe Bloggs",,,"Test subject","Test body","c:\temp\first attachment.txt;c:\temp\second attachment.txt",,,True)
The table I have is named tbl_temp_Email and has the following fields: temp_Select, temp_File_Name, temp_File_Location, temp_File_Discription.
temp_File_Location has the path to the file and temp_File_Name, well obviously has the file's name. temp_Select has a yes/no field that the user checks in a form to choose which files they want to attach to an email.
My problem lays in where to start to get it to go through the records and find the selected ones. I am hoping that I would be able to take those and put in the function call and then let the code in the function take those and attach to the email.
If any one would be able and willing to help me out it would be greatly appreciated.
Thanks,
Matt