Move Emails between Folders in Separate Mailbox

Status
Not open for further replies.
Outlook version
Outlook 2016 64 bit
Email Account
Office 365 Exchange
Greetings,

I am trying to move all messages in one subfolder to another. Actually I have to do that twice but I can adapt the code myself. My trouble is that I'm far more adept with Access VBA than Outlook VBA. Also, the message folders (current and target) aren't within my own mailbox, they're within another in my profile (not even sure if I'm using the right terminology). I've included a snapshot of the mailbox layout below. So within the TDEM Contracts mailbox(?), I need a way to move all messages from Daily Emails to "daily_log_holding_area". Once I manipulate it and run the reports I need to, I then need to move them from there to "logged_emails". Certainly I could just click and drag but with 100's - 1,000's needing to move each day I'm forced to move them in multiple chucks, which just takes to much attention/time/steps. I'm able to find similar code to what I need, but I can't seem to adapt it to the fact that it's not my own inbox. It may just be a syntax thing I'm unfamiliar with.

Any help would be greatly appreciated!
David


mailbox_snapshot.PNG
 
they're within another in my profile (not even sure if I'm using the right terminology).
Based on the screenshot, it looks like it is a shared mailbox (the display name instead of an email address)
You'll use the method at Working with VBA and non-default Outlook Folders

This is the inbox:
Set InboxFolder = NS.GetSharedDefaultFolder(objOwner, olFolderInbox)
reference the subfolder of inbox:
Set subFolder = InboxFolder.folder("folder name")
 
Based on the screenshot, it looks like it is a shared mailbox (the display name instead of an email address)
You'll use the method at Working with VBA and non-default Outlook Folders

This is the inbox:
Set InboxFolder = NS.GetSharedDefaultFolder(objOwner, olFolderInbox)
reference the subfolder of inbox:
Set subFolder = InboxFolder.folder("folder name")

Thanks Diane! A quick followup question. Though it's not in the screenshot, I have multiple shared mailboxes that I can access. Will the code you've provided be able to determine the right one or will additional code be necessary? Thanks!
 
Thanks Diane! A quick followup question. Though it's not in the screenshot, I have multiple shared mailboxes that I can access. Will the code you've provided be able to determine the right one or will additional code be necessary? Thanks!
You'll need to repeat the code. Depending on how the macro is written, you'll either use different object names and basically repeat the code below, or loop and use a different recipient name each time. This would work with an array or select case (and cut down on the amount of code you need to use.)

You could use a small macro that sets the mailbox name (and even the folder names to use), then call one that does the move.

Code:
 Set objOwner = NS.CreateRecipient("maryc")
    objOwner.Resolve
      
If objOwner.Resolved Then
   'MsgBox objOwner.Name
Set newCalFolder = NS.GetSharedDefaultFolder(objOwner, olFolderCalendar)
End If
 
You'll need to repeat the code. Depending on how the macro is written, you'll either use different object names and basically repeat the code below, or loop and use a different recipient name each time. This would work with an array or select case (and cut down on the amount of code you need to use.)

You could use a small macro that sets the mailbox name (and even the folder names to use), then call one that does the move.

Code:
 Set objOwner = NS.CreateRecipient("maryc")
    objOwner.Resolve
    
If objOwner.Resolved Then
   'MsgBox objOwner.Name
Set newCalFolder = NS.GetSharedDefaultFolder(objOwner, olFolderCalendar)
End If

Well, I've made some great progress but somewhere I'm screwing up. I have the following code, though the couple of examples you sent had some slightly different naming conventions so my inexperience may be at fault if I've incorrectly pieced them together. Every time I run this code:
Code:
Sub MoveMail()

    Dim objOutlook As Outlook.Application
    Dim objNamespace As Outlook.NameSpace
    Dim objVariant As Variant
    Dim lngMovedItems As Long
    Dim intCount As Integer
    Dim strDestFolder As String
    Dim inboxSourceFolder As Outlook.MAPIFolder
    Dim DestsubFolder As Outlook.MAPIFolder
    Dim SourcesubFolder As Outlook.MAPIFolder
      
    Set objOutlook = Application
    Set objNamespace = objOutlook.GetNamespace("tdem")
    Set inboxSourceFolder = objNamespace.GetDefaultFolder(olFolderInbox)
    Set SourcesubFolder = inboxSourceFolder.Folder("Daily Emails")
    Set DestsubFolder = inboxSourceFolder.Folder("daily_log_holding_area")
  
    For intCount = SourcesubFolder.Items.Count To 1 Step -1
        Set objVariant = SourcesubFolder.Items.Item(intCount)
        DoEvents
 
              objVariant.Move DestsubFolder
            
              'count the # of items moved
               lngMovedItems = lngMovedItems + 1

    Next
  
    ' Display the number of items that were moved.
    MsgBox "Moved " & lngMovedItems & " messages(s)."
Set objDestFolder = Nothing
End Sub

It gives me the most unhelpful error message "Sorry, something went wrong. You may want to try again." When I hit debug it points me to the row that begins "Set objNamespace". For background, I deviated from your example a bit because I want all the emails in a folder moved, regardless of their characteristic. I should also point out that "TDEM" is the alias used for the account. The email prefix before the @ is "tdem.contract", I've tried both and gotten the same error message, I wasn't sure which to use.

Thanks SO much. This has been very helpful for me and I greatly appreciate your feedback and assistance.
 
i have not tested this yet - but i think i fixed all the errors - i need to figure out which test account is shared with another before i can test it. :)

The folder tree in the shared box will be
-Inbox
-- Daily Emails
-- daily_log_holding_area



Code:
Sub MoveMail()

    Dim objOutlook As Outlook.Application
    Dim NS As Outlook.NameSpace
    Dim objVariant As Variant
    Dim lngMovedItems As Long
    Dim intCount As Integer
    Dim strDestFolder As String
    Dim inboxSourceFolder As Outlook.MAPIFolder
    Dim DestsubFolder As Outlook.MAPIFolder
    Dim SourcesubFolder As Outlook.MAPIFolder
     
    Set objOutlook = Application
    Set NS = objOutlook.GetNamespace("MAPI")
   
'### Get Shared
    Set objOwner = NS.CreateRecipient("maryc")
    objOwner.Resolve
     
If objOwner.Resolved Then
   'MsgBox objOwner.Name
Set inboxSourceFolder = NS.GetSharedDefaultFolder(objOwner, olFolderInbox)
End If

'### end get shared

    Set SourcesubFolder = inboxSourceFolder.Folders("Daily Emails")
    Set DestsubFolder = inboxSourceFolder.Folders("daily_log_holding_area")
 
    For intCount = SourcesubFolder.Items.Count To 1 Step -1
        Set objVariant = SourcesubFolder.Items.Item(intCount)
        DoEvents
              objVariant.Move DestsubFolder
           
              'count the # of items moved
               lngMovedItems = lngMovedItems + 1

    Next
 
    ' Display the number of items that were moved.
    MsgBox "Moved " & lngMovedItems & " messages(s)."
Set objDestFolder = Nothing
End Sub
 
i have not tested this yet - but i think i fixed all the errors - i need to figure out which test account is shared with another before i can test it. :)

The folder tree in the shared box will be
-Inbox
-- Daily Emails
-- daily_log_holding_area



Code:
Sub MoveMail()

    Dim objOutlook As Outlook.Application
    Dim NS As Outlook.NameSpace
    Dim objVariant As Variant
    Dim lngMovedItems As Long
    Dim intCount As Integer
    Dim strDestFolder As String
    Dim inboxSourceFolder As Outlook.MAPIFolder
    Dim DestsubFolder As Outlook.MAPIFolder
    Dim SourcesubFolder As Outlook.MAPIFolder
    
    Set objOutlook = Application
    Set NS = objOutlook.GetNamespace("MAPI")
  
'### Get Shared
    Set objOwner = NS.CreateRecipient("maryc")
    objOwner.Resolve
    
If objOwner.Resolved Then
   'MsgBox objOwner.Name
Set inboxSourceFolder = NS.GetSharedDefaultFolder(objOwner, olFolderInbox)
End If

'### end get shared

    Set SourcesubFolder = inboxSourceFolder.Folders("Daily Emails")
    Set DestsubFolder = inboxSourceFolder.Folders("daily_log_holding_area")
 
    For intCount = SourcesubFolder.Items.Count To 1 Step -1
        Set objVariant = SourcesubFolder.Items.Item(intCount)
        DoEvents
              objVariant.Move DestsubFolder
          
              'count the # of items moved
               lngMovedItems = lngMovedItems + 1

    Next
 
    ' Display the number of items that were moved.
    MsgBox "Moved " & lngMovedItems & " messages(s)."
Set objDestFolder = Nothing
End Sub

Thank you so much. The folder tree is exactly right. When I tested the code, it threw "Object Variable or With block variables not set" and directed me to the "Set SourcesubFolder" line. Thank you again so much for all your help!
 
i'm getting a different error on that line - object cant be found (I just created the folder but that shouldn't make a difference)

ETA: opened the mailbox in another profile and the folders i added didn't sync up. that explains my error.
 
Ok.. once i got the folder issues solved, this worked to move mail from Daily Emails to the log holding folder.

move-mail.png


Code:
Sub MoveMail()

    Dim objOutlook As Outlook.Application
    Dim NS As Outlook.NameSpace
    Dim objVariant As Variant
    Dim lngMovedItems As Long
    Dim intCount As Integer
    Dim strDestFolder As String
    Dim inboxSourceFolder As Outlook.MAPIFolder
    Dim destSubFolder As Outlook.MAPIFolder
    Dim sourceSubFolder As Outlook.MAPIFolder
     
    Set objOutlook = Application
    Set NS = objOutlook.GetNamespace("MAPI")
   
'### Get Shared
    Set objOwner = NS.CreateRecipient("replies")
    objOwner.Resolve
     
If objOwner.Resolved Then
   MsgBox objOwner.Name
Set inboxSourceFolder = NS.GetSharedDefaultFolder(objOwner, olFolderInbox)
   MsgBox inboxSourceFolder.FolderPath

End If

'### end get shared

  Set destSubFolder = inboxSourceFolder.Folders("daily_log_holding_area")
  Set sourceSubFolder = inboxSourceFolder.Folders("Daily Emails")
 
    For intCount = sourceSubFolder.Items.Count To 1 Step -1
        Set objVariant = sourceSubFolder.Items.Item(intCount)
        DoEvents
              objVariant.Move destSubFolder
           
              'count the # of items moved
               lngMovedItems = lngMovedItems + 1

    Next
 
    ' Display the number of items that were moved.
    MsgBox "Moved " & lngMovedItems & " messages(s)."
Set objDestFolder = Nothing
End Sub
 
Hey Diane,

I must be doing something else wrong because I'm still getting that error message that you're not. I've attached two pictures that show what's going on. One is the error message and the other is what going into debug shows. Thank you again so much for your help!

pic1.PNG
pic2.PNG


Thanks!
 
Ok.. once i got the folder issues solved, this worked to move mail from Daily Emails to the log holding folder.

View attachment 2152

Code:
Sub MoveMail()

    Dim objOutlook As Outlook.Application
    Dim NS As Outlook.NameSpace
    Dim objVariant As Variant
    Dim lngMovedItems As Long
    Dim intCount As Integer
    Dim strDestFolder As String
    Dim inboxSourceFolder As Outlook.MAPIFolder
    Dim destSubFolder As Outlook.MAPIFolder
    Dim sourceSubFolder As Outlook.MAPIFolder
    
    Set objOutlook = Application
    Set NS = objOutlook.GetNamespace("MAPI")
  
'### Get Shared
    Set objOwner = NS.CreateRecipient("replies")
    objOwner.Resolve
    
If objOwner.Resolved Then
   MsgBox objOwner.Name
Set inboxSourceFolder = NS.GetSharedDefaultFolder(objOwner, olFolderInbox)
   MsgBox inboxSourceFolder.FolderPath

End If

'### end get shared

  Set destSubFolder = inboxSourceFolder.Folders("daily_log_holding_area")
  Set sourceSubFolder = inboxSourceFolder.Folders("Daily Emails")
 
    For intCount = sourceSubFolder.Items.Count To 1 Step -1
        Set objVariant = sourceSubFolder.Items.Item(intCount)
        DoEvents
              objVariant.Move destSubFolder
          
              'count the # of items moved
               lngMovedItems = lngMovedItems + 1

    Next
 
    ' Display the number of items that were moved.
    MsgBox "Moved " & lngMovedItems & " messages(s)."
Set objDestFolder = Nothing
End Sub


Not sure if this is my issue but I have multiple shared mailboxes. How does the code direct to a specific mailbox. I see it get the folders of course, but not the inbox. Again, I've an early novice with Outlook so maybe it does. Thanks!
 
Does that folder exist? if you hover of the yellow words, what does it show?

This is where it directs the shared box:
Set objOwner = NS.CreateRecipient("replies")

This is one way to handle running it on multiple folders. The alias can be the alias, the display name, or the email address.


Code:
Dim strAlias As String

Sub MoveMail()
strAlias = "replies"
DoMoveMail
strAlias = "olSales"
DoMoveMail
End Sub


Sub DoMoveMail()

    Dim objOutlook As Outlook.Application
    Dim NS As Outlook.NameSpace
    Dim objVariant As Variant
    Dim lngMovedItems As Long
    Dim intCount As Integer
    Dim strDestFolder As String
    Dim inboxSourceFolder As Outlook.MAPIFolder
    Dim destSubFolder As Outlook.MAPIFolder
    Dim sourceSubFolder As Outlook.MAPIFolder
     
    Set objOutlook = Application
    Set NS = objOutlook.GetNamespace("MAPI")
   
'### Get Shared
    Set objOwner = NS.CreateRecipient(strAlias)
    objOwner.Resolve
     
If objOwner.Resolved Then
   MsgBox objOwner.Name
Set inboxSourceFolder = NS.GetSharedDefaultFolder(objOwner, olFolderInbox)
   MsgBox inboxSourceFolder.FolderPath

End If

'### end get shared

  Set destSubFolder = inboxSourceFolder.Folders("daily_log_holding_area")
  Set sourceSubFolder = inboxSourceFolder.Folders("Daily Emails")
 
    For intCount = sourceSubFolder.Items.Count To 1 Step -1
        Set objVariant = sourceSubFolder.Items.Item(intCount)
        DoEvents
              objVariant.Move destSubFolder
           
              'count the # of items moved
               lngMovedItems = lngMovedItems + 1

    Next
 
    ' Display the number of items that were moved.
    MsgBox "Moved " & lngMovedItems & " messages(s)."
Set objDestFolder = Nothing
End Sub
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
P Move emails between 2 mailboxes. Using Outlook 0
H Move Selected emails to Local Drive Outlook VBA and Custom Forms 0
humility36 Cannot move emails to archive - 440 error Outlook VBA and Custom Forms 1
B Move emails from one account to another Outlook VBA and Custom Forms 2
R List folders in a combo box + select folder + move emails from inbox to that folder + reply to that email Outlook VBA and Custom Forms 1
V Outlook 2016 will not move emails in search results Using Outlook 4
N Move emails of same conversation to same subfolder Using Outlook 6
B Macro to manually move selected emails to network folder Outlook VBA and Custom Forms 1
K VBA to move emails in folder to a windows folder Outlook VBA and Custom Forms 2
N Move red and unflagged emails to subfolder Outlook VBA and Custom Forms 1
I vba scrip to move emails based on long keywords list Outlook VBA and Custom Forms 0
V move emails to windows folder and show sender details Using Outlook 7
G email returns after running macro to move emails Outlook VBA and Custom Forms 1
S How can I create a rule to move inbound emails from any of my contacts out of Inbox? Using Outlook 1
S Macro to print & move selected emails? Using Outlook 3
H Rule to move emails based on subject and content Outlook VBA and Custom Forms 1
H Move outlook emails >90 days to shared drive (Desktop) Folder Outlook VBA and Custom Forms 5
O Script to move emails to a folder based on various possible keywords Outlook VBA and Custom Forms 11
S how to keep "To" when move sent emails to other folder Using Outlook 1
N Outlook 2010 exchange - auto-move emails from @domain Exchange Server Administration 1
R Move sent items emails to hard disk Using Outlook 10
B OUTLOOK 2013: How Do I Move Emails to Folders stored on my Hard Drive? Using Outlook 3
S Outlook macro to move replied / forwarded emails to a seperate folder Using Outlook 1
C Help with a Macro to move emails to a different PST data file Using Outlook 4
A rule to move emails to a folder then want to delete them from that folder 2010 Using Outlook 5
V Move old emails or emails with large attachments ? Outlook VBA and Custom Forms 1
J move contents from hundreds of emails into excel Outlook VBA and Custom Forms 4
A Search folder and move the email Outlook VBA and Custom Forms 0
A Outlook 365 (OutLook For Mac)Move "On My Computer" Folder Items From Old To New Mac Computer Using Outlook 3
HarvMan Outlook 365 - Rule to Move an Incoming Message to Another Folder Using Outlook 4
B Outlook 2019 Automatically move email after assigning category Using Outlook 4
C Trying to move messages between imap accounts/folders Using Outlook 5
M Move command Outlook VBA and Custom Forms 11
C Code to move mail with certain attachment name? Does Not work Outlook VBA and Custom Forms 3
J Quick steps delete original email and move reply/sent email to folder Using Outlook 2
N How to add or delete items to Move dropdown Menu Using Outlook 0
Commodore Unable to move message Using Outlook 3
N Line to move origEmail to subfolder within a reply macro Outlook VBA and Custom Forms 0
C Move or copy from field to field Outlook VBA and Custom Forms 0
T Outlook 365 Move newly created tasks automatically on save. Outlook VBA and Custom Forms 1
NVDon Create new Move To Folder list Outlook VBA and Custom Forms 0
P Print attachments automatically and move the mail to an existing folder called "Ted" Outlook VBA and Custom Forms 4
T Macro to move reply and original message to folder Outlook VBA and Custom Forms 6
F VBA to move email from Non Default folder to Sub folders as per details given in excel file Outlook VBA and Custom Forms 11
F Excel VBA to move mails for outlook 365 on secondary mail account Outlook VBA and Custom Forms 1
J Dopey move - deleted profile Using Outlook 1
GregS Outlook 2016 Move Outlook to new computer? Using Outlook 4
Witzker Macro to move @domain.xx of a Spammail to Blacklist in Outlook 2019 Outlook VBA and Custom Forms 7
G Move tasks up/down todo list by VBA Outlook VBA and Custom Forms 1
S Macro to move “Re:” & “FWD:” email recieved the shared inbox to a subfolder in outlook Outlook VBA and Custom Forms 0

Similar threads

Back
Top