VBA Outlook MAPI Folders Error: 'Cannot Move Emails' - refer to unspecified folder?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Rebecca McCallan
    New Member
    • Aug 2010
    • 38

    VBA Outlook MAPI Folders Error: 'Cannot Move Emails' - refer to unspecified folder?

    I am essentially looking for a bit of code which will allow me to refer to an unspecified MAPI folder. Just to put that into context, My code creates a new folder if there is a new sender, the new folder is automatically created by using the domain name of the senders email address. I then want to move the relevant email into that folder (which is why it is unspecified) does anybody know how I would go about this?
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    How about posting your relevant Code, so that we can see exactly how the Folder is created, and how it can be referenced.

    Comment

    • Rebecca McCallan
      New Member
      • Aug 2010
      • 38

      #3
      Code:
      Set rstClient = db.OpenRecordset("Select foldername from tbl001_clientdetails where hostname = '" & strClientHost & "'")
                  If "'" & strFolderName & "'" <> rstClient!FolderName Then
                  'Set new folder for client and move emails into it
                      
                      Set outOutlook = CreateObject("Outlook.Application")
                      Set outNamespace = outOutlook.GetNamespace("MAPI")
                      Set outItem = outNamespace.GetDefaultFolder(olFolderInbox)
                      
                      Set outFoldersClient = outFoldersClient.Folders("'" & strFolderName & "'")
                                    
                      outItem.Folders.Add ("'" & strFolderName & "'")
                      
                      Set outOutlook = Nothing
                      Set outNamespace = Nothing
                      Set outItem = Nothing                         
                  Else
                      MailObject.Move outFoldersClient
                  End If
      So basically I want the code to move the email into the folder in both instances (if and else) but if the case is that it is a new client it creates a new folder and THEN moves in into the folder and if it is an existing client it will go to else and will simply move the email into the folder.

      Comment

      • Rebecca McCallan
        New Member
        • Aug 2010
        • 38

        #4
        the folder which currently exists for the client i mean

        Comment

        • ADezii
          Recognized Expert Expert
          • Apr 2006
          • 8834

          #5
          Rebecca, where are all you Object Variable Declarations? Kindly post along with Scope.

          Comment

          • Rebecca McCallan
            New Member
            • Aug 2010
            • 38

            #6
            On Error GoTo Err_funErrorChe cking
            Dim outNamespace 'As Outlook.NameSpa ce
            Dim outOutlook 'As Outlook.Applica tion
            Dim MailObject 'As Outlook.MailIte m
            Dim outItem 'As Outlook.MailIte m
            Dim outFolder As MAPIFolder
            Dim outFoldersUnaut horised As MAPIFolder
            Dim outFoldersActio ned As MAPIFolder
            Dim outFoldersNewSe nder As MAPIFolder
            Dim outFoldersClien t As MAPIFolder
            Dim InboxItems As Outlook.Items
            Dim intCount As Integer
            Dim intMove As Integer
            Dim strAnswer As String
            Dim intAnswer As Integer
            Dim strField As String
            Dim rstFolders As DAO.Recordset
            Dim rstAdmin As DAO.Recordset
            Dim intID As Integer
            Dim strRequestID As String
            Dim intContactID As Integer
            Dim rstData As DAO.Recordset
            Dim rstUserData As DAO.Recordset
            Dim rstPathData As DAO.Recordset
            Dim db As Database
            Dim strEmailAddress As String
            Dim rstContactCheck As DAO.Recordset
            Dim rstSupportReque st As DAO.Recordset
            Dim intunauthorised As Integer
            Dim intnewsender As Integer
            Dim intuncount As Integer
            Dim intnewcount As Integer
            Dim strClientEmail As String
            Dim strProBaseEmail As String
            Dim rstClient As DAO.Recordset
            Dim rstClientEmail As DAO.Recordset
            Dim strHostName As String
            Dim strClientHost As String
            Dim strFolderName As String
            Dim rstAssignee As DAO.Recordset
            Dim intposition As Integer
            Dim strContactName As String
            Dim rstAuthorised As String
            Dim strsubjectyes As String
            Dim strsubjectno As String

            Cheers

            Comment

            • ADezii
              Recognized Expert Expert
              • Apr 2006
              • 8834

              #7
              Wow, be careful what you ask for. Rebecca, I created some Demo Code for you that appears to work quite well, but has not been extensively tested. I'll leave that part up to you. Good Luck, and let us know how you make out.
              Code:
              'The following Code will create a New Folder under Inbox, then move all
              'Mail to this new Destination Directory, from Inbox
              Dim outOutlook As New Outlook.Application
              Dim outNamespace As Outlook.NameSpace
              Dim myInbox As Outlook.MAPIFolder
              Dim myDestFolder As Outlook.MAPIFolder
              Dim outItems As Outlook.Items
              Dim outItem As Object
              Dim strFolderName As String
              
              strFolderName = "YaDa-YaDa"     'Destination Folder
              
              Set outNamespace = outOutlook.GetNamespace("MAPI")
              Set myInbox = outNamespace.GetDefaultFolder(olFolderInbox)
              'Set myInbox = outNamespace.GetDefaultFolder(olFolderDrafts)    Drafts Folder
              
              Set outItems = myInbox.Items
                  myInbox.Folders.Add strFolderName       'Add Destination Folder under Inbox
              Set myDestFolder = myInbox.Folders(strFolderName)
              
              DoCmd.Hourglass True
              
              'Move each Mail Item to the Destination Folder
              For Each outItem In outItems
                outItem.Move myDestFolder
              Next
              
              '*********************************************************************
              'To Move Mail for a specific Sender                                  *
              'Set outItem = outItems.Find("[SenderName] = 'Herman Munster'")      *
              'While TypeName(myItem) <> "Nothing"                                 *
                'myItem.Move myDestFolder                                          *
                   'Set myItem = myItems.FindNext                                  *
              'Wend                                                                *
              '*********************************************************************
              
              DoCmd.Hourglass False
              
              MsgBox "Mail has been moved from " & myInbox.Name & " to " & strFolderName & "!", _
                      vbInformation, "Mail Move"
              
              Set outOutlook = Nothing
              Set outNamespace = Nothing
              Set outItem = Nothing

              Comment

              • Rebecca McCallan
                New Member
                • Aug 2010
                • 38

                #8
                Wow, it actually worked! Thanks so much... no body else could understand what I wanted to do!

                Thank you!!

                Comment

                • ADezii
                  Recognized Expert Expert
                  • Apr 2006
                  • 8834

                  #9
                  You are quite welcome, Rebecca.

                  Comment

                  Working...