Consulting

Results 1 to 5 of 5

Thread: Outlook in Excel - move folder

  1. #1

    Outlook in Excel - move folder

    Is it possible to move an email from my inbox to a sub-folder. I set my criteria:

    Set OLF = GetObject("", "Outlook.Application").GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
       With OLF.Items(i)
       If InStr(LCase(.Body), LCase(Bodyin(varc))) > 0 Then
          att = True
           Exit For
    I see you can set the subfolder, but I don't see how to move the email to a subfolder. Any help is appreciated.
    Last edited by Aussiebear; 04-09-2023 at 05:16 AM. Reason: Adjusted the code tags

  2. #2
    Knowledge Base Approver VBAX Master Oorang's Avatar
    Joined
    Jan 2007
    Posts
    1,135
    Location

    Hi Chris,
    Welcome to the board. Here is an example of how to do what you requested. This example is simple is just clones stuff from your inbox to an archive. You will probably want to update the folder path for it to work, but the drift should be there Post back if you hit rough water!
    Public Sub AttackOfTheClones()
        Dim ns As Outlook.NameSpace
        Dim fldrInbox As Outlook.mapiFolder
        Dim destFldr As Outlook.mapiFolder
        Dim itm As Object
        Dim mlItm As Outlook.MailItem
        On Error GoTo Err_Hnd
        Set ns = Outlook.Application.Session
        Set fldrInbox = ns.GetDefaultFolder(olFolderInbox)
        Set destFldr = GetFolder("Archive 2007\Clones")
        If Not destFldr Is Nothing Then
            For Each itm In fldrInbox.Items
                If itm.Class = olMail Then
                    Set mlItm = itm.Copy
                    mlItm.Move destFldr
                End If
            Next
        End If
    Exit_Proc:
        On Error Resume Next
        Exit Sub
    Err_Hnd:
        VBA.MsgBox "Error " & VBA.Err.number & " (" & VBA.Err.Description & _
            ") in procedure AttackOfTheClones of Module Module1"
        Resume Exit_Proc
    End Sub
    
    Public Function GetFolder(ByVal strFolderPath As String) As Outlook.mapiFolder
        Const strBckSlsh_c As String = "\"
        Const strFwdSlsh_c As String = "\"
        Const lngLwrBnd_c As Long = 1
        Const lngRootIndx_c As Long = 0
        Dim lngIndex As Long
        Dim strFolders() As String
        Dim fldr As Outlook.mapiFolder
        On Error GoTo Err_Hnd
        strFolderPath = Replace(strFolderPath, strFwdSlsh_c, strBckSlsh_c)
        strFolders() = Split(strFolderPath, strFwdSlsh_c)
        Set fldr = Outlook.Session.Folders.Item(strFolders(lngRootIndx_c))
        For lngIndex = lngLwrBnd_c To UBound(strFolders)
            Set fldr = fldr.Folders.Item(strFolders(lngIndex))
        Next
        Set GetFolder = fldr
        Exit Function
    Err_Hnd:
        VBA.MsgBox "Error " & VBA.Err.number & " (" & VBA.Err.Description & _
            ") in procedure GetFolder of Module Module1"
    End Function
    Last edited by Aussiebear; 04-09-2023 at 05:17 AM. Reason: Adjusted the code tags
    Cordially,
    Aaron



    Keep Our Board Clean!
    • Please Mark your thread "Solved" if you get an acceptable response (under thread tools).
    • Enclose your code in VBA tags then it will be formatted as per the VBIDE to improve readability.

  3. #3
    Thanks for the reply. I can't get the "destfldr" to come up with anything.

    I tried it a few ways:

    Set destFldr = GetFolder("Inbox\keep")
    Set destFldr = GetFolder("keep")
    It keeps coming up with nothing. I added the set fldr function. If you can't tell, I am new to trying outlook vb, so any help is appreciated. Thanks!
    Last edited by Aussiebear; 04-09-2023 at 05:18 AM. Reason: Added the code tags

  4. #4
    Knowledge Base Approver VBAX Master Oorang's Avatar
    Joined
    Jan 2007
    Posts
    1,135
    Location
    Don't sweat, outlook's folder structure is a bear. I suspect the issue is the inbox name. For instance my Inbox is not named "Inbox", it's named "Mailbox - Bush, Aaron" so my path would look like this:

    Set destFldr = GetFolder("Mailbox - Bush, Aaron\keep")
    I just right clicked on my inbox and went to properties to get the name.
    Last edited by Aussiebear; 04-09-2023 at 05:18 AM. Reason: Adjusted the code tags
    Cordially,
    Aaron



    Keep Our Board Clean!
    • Please Mark your thread "Solved" if you get an acceptable response (under thread tools).
    • Enclose your code in VBA tags then it will be formatted as per the VBIDE to improve readability.

  5. #5

    Thumbs up Solved

    That was it. Thank you, much appreciated.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •