Attaching a Previously Sent E-mail (1 Viewer)

LadyDi

Registered User.
Local time
Yesterday, 21:20
Joined
Mar 29, 2007
Messages
894
Hello,

I would like to create a button on my form that will search through my e-mails in Outlook, find the e-mail with a certain subject line, and include that e-mail as an attachment in another e-mail generated out of Access. Is this possible?
 

ErikSnoek

Programmer
Local time
Yesterday, 21:20
Joined
Apr 26, 2007
Messages
100
Since you didn't specify any outlook folders in which to search in, the following code searches through all folders and mailboxes you have in your outlook :). Usage:
Code:
Call MailFindAndSend("The subject")

Code:
Sub MailFindAndSend(strSubject As String)
    Dim olApp As Outlook.Application
    Dim nsNamespace As Outlook.NameSpace
    Dim fldFolders As Outlook.Folders
    Dim fldItFolder As Outlook.MAPIFolder
    Dim miMail As Outlook.MailItem, miNewMail As Outlook.MailItem
            
    Set olApp = CreateObject("Outlook.application")
    Set nsNamespace = olApp.GetNamespace("MAPI")
    Set fldFolders = nsNamespace.Folders
            
    For Each fldItFolder In fldFolders
        If FindInOlFolder(fldItFolder, strSubject, miMail) Then
            Exit For
        End If
    Next
    
    If Not IsNull(miMail) Then
        Set miNewMail = olApp.CreateItem(olMailItem)
        With miNewMail
            .BodyFormat = olFormatPlain
            .HTMLBody = "Some body text!"
            .Subject = "Some subject!"
            .Recipients.Add "[EMAIL="someone@email.com"]someone@email.com[/EMAIL]"
            .Attachments.Add miMail
            .Send
        End With
    Else
        MsgBox "The mail could not be found."
    End If
    
End Sub
Function FindInOlFolder(fldFolder As Outlook.MAPIFolder, strFindSubject As String, miResult As Outlook.MailItem) As Boolean
    
    Dim miMail As Outlook.MailItem
    Dim intIt As Integer
    Dim fldNext As Outlook.MAPIFolder
    
    For intIt = 1 To fldFolder.Items.Count
        If fldFolder.Items(intIt) = strFindSubject Then
            Debug.Print fldFolder.name
            
            Set miResult = fldFolder.Items(intIt)
            FindInOlFolder = True
            Exit Function
        End If
    Next
    
    For Each fldNext In fldFolder.Folders
        If FindInOlFolder(fldNext, strFindSubject, miResult) Then
            FindInOlFolder = True
            Exit Function
        End If
    Next
    
    FindInOlFolder = False
End Function
 

LadyDi

Registered User.
Local time
Yesterday, 21:20
Joined
Mar 29, 2007
Messages
894
I really appreciate the code. I have a question for you. When I try to use the code, the computer highlights "Function FindInOlFolder(fldFolder As Outlook.MAPIFolder, strFindSubject As String, miResult As Outlook.MailItem) As Boolean" and says "Compile Error: User defined type not defined". What does that mean? I just copied and pasted the code you provided and plugged in the subject line that I want it to find.
 

ErikSnoek

Programmer
Local time
Yesterday, 21:20
Joined
Apr 26, 2007
Messages
100
You have to add a reference to "Microsoft Outlook 11.0 Object Library" (Extra->References)
 

LadyDi

Registered User.
Local time
Yesterday, 21:20
Joined
Mar 29, 2007
Messages
894
Attach Previously Sent E-mail

I added the reference, but now it is giving me another error. Now it highlights the call statement and says "Compile error: expected variable or procedure, not module". What does that mean? I saved the code as a module. Was that not the right thing to do?
 

ErikSnoek

Programmer
Local time
Yesterday, 21:20
Joined
Apr 26, 2007
Messages
100
Did you place that line in some test sub? Like:
Code:
Public Sub test()
    Call MailFindAndSend("The subject")
End Sub
orr..... are you trying to launch the sub from some other module? Or a form? In that case you have to add "Public" before "Sub", I forgot to do that ;)
 

LadyDi

Registered User.
Local time
Yesterday, 21:20
Joined
Mar 29, 2007
Messages
894
I added "Public" before "Sub" in the module, but I am still getting that same error message. I put the call statement in the "On Click" event of a button on my form.
 

ErikSnoek

Programmer
Local time
Yesterday, 21:20
Joined
Apr 26, 2007
Messages
100
You didn't name the module "MailFindAndSend" right?
 

LadyDi

Registered User.
Local time
Yesterday, 21:20
Joined
Mar 29, 2007
Messages
894
Yes, I did name it MailFindAndSend. Was I supposed to name it something esle?
 

ErikSnoek

Programmer
Local time
Yesterday, 21:20
Joined
Apr 26, 2007
Messages
100
Well yes. The sub is already named "MailFindAndSend" so you're basically confusing Access :D. A good way to call a module is mod<name>, so in this case modMailFindAndSend :)
 

LadyDi

Registered User.
Local time
Yesterday, 21:20
Joined
Mar 29, 2007
Messages
894
Thank you, that works perfectly. I have one more question for you. Is there a way to include this Call statement in a Docmd Send statement? I would like to include the previously sent e-mail in an e-mail generated by the database that contains a new subject line and body text.
 

Users who are viewing this thread

Top Bottom