Page 1 of 2 12 LastLast
Results 1 to 15 of 30
  1. #1
    JeroenMioch's Avatar
    JeroenMioch is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2012
    Location
    Den Haag, Netherlands
    Posts
    368

    Docmd.sendObject Sending email to specific folder

    Hey guys,



    I have a Docmd.sendobject feature in my database that generates an email with attachement.
    User has only to click the send button and the email is sent.
    A copy of this email is send as BCC to our own emailaddress

    What i want is that the incomming email or the outgoing email automatically is moved to a specific Outlook folder of our own email account. (Outlook 2010)

    I know i can create rules in Outlook itself but that is not an option.

    Any help would be appreciated

    Greetings, Jeroen

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,625
    Bing: Outlook VBA move message to folder

    http://answers.microsoft.com/en-us/o...9-c5fb387b63d7

    Adapting code to run from Access may or may not be possible.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    JeroenMioch's Avatar
    JeroenMioch is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2012
    Location
    Den Haag, Netherlands
    Posts
    368
    Hi June,

    I think the code is close to what im looking for.
    I am getting errors though, but im confident that if i resolve them it might work.

    Im getting the error at the point im defining the destination folder wich i marked bold.
    I also tryed the subfolder variant as the destination folder is a subfolder.

    The destination folder is something like this : \\My personal account\Inbox\Archive\notices
    Maybe the problem is that its a shared account. But then again i tried it with a non shared account as well. And it still doesnt work.

    Code:
    Sub Test()
    Dim MAPI As NameSpace
    Dim Source As Folder, Dest As Folder
    Dim Mail As MailItem
    
    Set MAPI = GetNamespace("MAPI")
    
    'Get the inbox folder
    Set Source = MAPI.GetDefaultFolder(olFolderInbox)
    'Set the destination folder (main folder)
    Set Dest = MAPI.Folders("Test")
    
    'Use this for a sub folder
    'Set Dest = MAPI.Folders("test").Folders("MySubFolder")
    
    'Visit each mail
    For Each Mail In Source.Items
    'Match with our criteria?
    If Mail.Subject = "test" Then
    'Move it to the other folder
    Mail.Move Dest
    End If
    Next
    End Sub
    

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,625
    For code in Access to manipulate Outlook features, will probably first have to declare and set Outlook application object.

    Review examples that set Outlook objects for purpose of sending email. https://www.accessforums.net/access/...ect-17713.html
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    JeroenMioch's Avatar
    JeroenMioch is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2012
    Location
    Den Haag, Netherlands
    Posts
    368
    Im really stuck with this
    Any other suggestions ?

    Im not trying to get some easy code. Ive tried a lot of things..

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,625
    Did you revise code to declare Outlook objects? Post attempted code.

    This is entirely new frontier for me.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    JeroenMioch's Avatar
    JeroenMioch is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2012
    Location
    Den Haag, Netherlands
    Posts
    368
    Its kind of new to me too. I can send mails through the docmd.Sendobject and stuff but thats it.

    ill give it another try in an hour or so and let you know how i progress

  8. #8
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    It's been a while since I have played around with that stuff. I am going to guess you are going to need a folder named "Test" in order for the following to work.
    Set Dest = MAPI.Folders("Test")

    Perhaps you can create a folder named 'Test' or 'ItemsWereMoved' and then give it another go. Just make sure the folder you create is not a subfolder. Right click the main account so it is at the root.

  9. #9
    JeroenMioch's Avatar
    JeroenMioch is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2012
    Location
    Den Haag, Netherlands
    Posts
    368
    Hi itsMe. Thanks for your comment. Im gonna give it a go right away. Perhaps the test folder not being in the root was the problem all along.

  10. #10
    JeroenMioch's Avatar
    JeroenMioch is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2012
    Location
    Den Haag, Netherlands
    Posts
    368
    Right im a bit closer to solving this.
    With this code :
    Code:
    Set Dest = MAPI.Folders("<my personal account name>").Folders("Test")
    i got through it without errors.
    The sad thing is the mails were not moved haha.

    At least now i know where the "test" folder should be located.
    And i know which piece of code to use, the one with the subfolder.

    Now to figure out why mails are not moving.

  11. #11
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    You are going to have to search for something that exists within your Inbox. Maybe something like the following will help to determine how ...

    Code:
    On Error Resume Next
    'Visit each mail
    For Each Mail In Source.Items
    
        Debug.Print "**********************************"
        Debug.Print "Subject = " & Mail.Subject
        Debug.Print "CC = " & Mail.CC
        Debug.Print "BCC = " & Mail.BCC
        Debug.Print "Importance = " & Mail.Importance
        
    Next

  12. #12
    JeroenMioch's Avatar
    JeroenMioch is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2012
    Location
    Den Haag, Netherlands
    Posts
    368
    Hi ItsMe,

    Im not sure how to use debug.print.
    Ive read up on it. I just get no results wich can either mean i dont know how to use it OR there are no results.
    I think the latter is the case.

    After a night (i work the graveyard shift) of trying im just about to give it up.
    When i use this code to identify the destination folder i get no error.

    Code:
    'Set Dest = MAPI.Folders("<my account name>").Folders("Test")


    But nothing else happens after the code, not even if i create a simple msgbox "hello"

    When i use that line in another Outlook code grabbed from the internet that seem to work for others, it doesnt work for me.
    The error i get is that the object is not there basicly.

    Is there any other suggestion you can give me in solving this problem ?



  13. #13
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    debug.print will produce results in the Immediate Window within the Visual Basic Editor. You can view the Immediate Window using the keyboard shortcut Ctrl + G or you can use the toolbar; View > Immediate Window

  14. #14
    JeroenMioch's Avatar
    JeroenMioch is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2012
    Location
    Den Haag, Netherlands
    Posts
    368
    Ive got the following code alongside two functions

    The error im now getting is 'Object variable or With block variable not set'
    Any clues ?


    Code:
    Sub MoveAllMessages_Click()
         
         Dim olApp As Object
         Dim Mapi As NameSpace
         Dim olkSource As Outlook.MAPIfolder
         Dim olkDestination As Outlook.MAPIfolder
         Dim olkMsg As Outlook.MailItem
         Dim intIndex As Integer
         Dim Mail As MailItem
         
         Set olkSource = OpenOutlookFolder(olFolderInbox)
         Set olkDestination = OpenOutlookFolder("<My account name>").Folders("Test")
         
         For intIndex = olkSource.Items.Count To 1 Step -1
         olkSource.Items.Item(intIndex).Move olkDestination
         Next
    
    
         Mail.Move olkDestination
         
        Set olkSource = Nothing
        Set olkDestination = Nothing
        Set olkMsg = Nothing
        MsgBox "Finished!", vbInformation + vbOKOnly, "Move All Messages"
    End Sub
    Code:
    Function IsNothing(obj)
      If TypeName(obj) = "Nothing" Then
        IsNothing = True
      Else
        IsNothing = False
      End If
    End Function
    Code:
    Function OpenOutlookFolder(strFolderPath As String) As Outlook.MAPIfolder
        Dim arrFolders As Variant, _
            varFolder As Variant, _
            olkFolder As Outlook.MAPIfolder
        On Error GoTo ehOpenOutlookFolder
        If strFolderPath = "" Then
            Set OpenOutlookFolder = Nothing
        Else
            If Left(strFolderPath, 1) = "\" Then
                strFolderPath = Right(strFolderPath, Len(strFolderPath) - 1)
            End If
            arrFolders = Split(strFolderPath, "\")
            For Each varFolder In arrFolders
                If IsNothing(olkFolder) Then
                    Set olkFolder = Session.Folders(varFolder)
                Else
                    Set olkFolder = olkFolder.Folders(varFolder)
                End If
            Next
            Set OpenOutlookFolder = olkFolder
        End If
        On Error GoTo 0
        Exit Function
    ehOpenOutlookFolder:
        Set OpenOutlookFolder = Nothing
        On Error GoTo 0
    End Function


    Thanks for looking into this guys, really appreciate it !

  15. #15
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Quote Originally Posted by JeroenMioch View Post
    The error im now getting is 'Object variable or With block variable not set'
    Any clues ?...
    Which line is it halting on, you posted a lot of code there? When the exception is thrown, click on Debug to see where the code is halting. Maybe you did not make a reference to Outlook or you tried to use the Dot notation to access members of a variable before instantiating it as an Object.

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 07-25-2014, 07:53 AM
  2. VBA Send Email using DoCmd.SendObject
    By malamute20 in forum Programming
    Replies: 15
    Last Post: 10-05-2011, 12:44 PM
  3. Replies: 2
    Last Post: 09-29-2011, 10:57 AM
  4. Replies: 1
    Last Post: 11-17-2010, 11:24 AM
  5. Email sent via DoCmd.SendObject
    By silverback in forum Programming
    Replies: 0
    Last Post: 10-29-2009, 06:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums