Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #2  
Old 12-18-2017, 05:49 AM
gmayor's Avatar
gmayor gmayor is offline Folder path in outlook for moving files Windows 10 Folder path in outlook for moving files Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,142
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

You can loop through the folders collection below any start folder and move the message to that folder. The following puts the selected message in the named folder ("Folder Name"), creating it as a subfolder of the start folder if not present. The macro prompts for the top level start folder.
Code:
Option Explicit

Sub MoveMessage()
Dim olMsg As MailItem
Dim olFolder As Folder
    On Error Resume Next
    Set olMsg = ActiveExplorer.Selection.Item(1)
    GetFolder olMsg, "Folder Name"
lbl_Exit:
    Set olMsg = Nothing
    Exit Sub
End Sub

Sub GetFolder(olItem As MailItem, strFolderName As String)
Dim olNS As NameSpace
Dim cFolders As Collection
Dim olFolder As Outlook.Folder
Dim StartFolder As Outlook.Folder
Dim SubFolder As Outlook.Folder
Dim bExists As Boolean
    Set cFolders = New Collection
    Set olNS = GetNamespace("MAPI")
    Set StartFolder = olNS.PickFolder
    cFolders.Add StartFolder
    Do While cFolders.Count > 0
        Set olFolder = cFolders(1)
        cFolders.Remove 1
        If UCase(olFolder.Name) = UCase(strFolderName) Then
            bExists = True
            Exit Do
        End If
        For Each SubFolder In olFolder.folders
            cFolders.Add SubFolder
        Next SubFolder
    Loop
    If Not bExists Then
        Set olFolder = StartFolder.folders.Add(strFolderName)
    End If
    olItem.Move olFolder
lbl_Exit:
    Set olNS = Nothing
    Set StartFolder = Nothing
    Set cFolders = Nothing
    Set olFolder = Nothing
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
Folder path in outlook for moving files Save to PDF with predetermined filename and specific folder path cutemich Word VBA 1 05-09-2017 06:27 PM
Folder path in outlook for moving files Moving body text from old files to new files (based on template) vagabond Word 7 05-02-2017 03:24 PM
Linking to a spreadsheet in the same folder without folder path Jacky837 Excel 0 07-30-2016 05:07 AM
Folder path in outlook for moving files Using user form fields as folder path and file name carlandtina02 Excel Programming 3 04-15-2016 01:47 PM
How to find the path to the downloads folder when it is different for each user CatMan Excel Programming 2 12-07-2012 09:59 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:51 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft