Solved

How to move contents of Outlook folder to another folder

Posted on 2015-02-15
26
176 Views
Last Modified: 2016-02-11
I want to move the contents of one mail folder to another. Note that I want to move "contents", not the folder. I have a folder: DEL > Deleted Items with several thousand messages. I want to move all these messages to the normal 'Deleted Items' folder:


 Trying to highlight all the messages hangs Outlook.

How do I do this?
Untitled.jpg
0
Comment
Question by:jmarkfoley
  • 11
  • 8
  • 6
  • +1
26 Comments
 
LVL 11

Expert Comment

by:Wilder1626
ID: 40611284
Hi jmarkfoley

if you select a couple only. can you drag them into the other folder?

Normally, drag and drop is what i do to transfer from one folder to the other.
0
 
LVL 1

Author Comment

by:jmarkfoley
ID: 40611287
yes
0
 
LVL 11

Expert Comment

by:Wilder1626
ID: 40611289
You must have a lot to transfer and also emails with attachment in it.

This is why it hangs.

Just transfer them progressively and not all at the same time.
0
 
LVL 1

Author Comment

by:jmarkfoley
ID: 40611294
I know.

There are several thousand message. And btw - I have to do this for several workstations. I cannot use the 'drag' method as there are other things I had planned on doing with the rest of my life.

Solutions?
0
 
LVL 11

Expert Comment

by:Wilder1626
ID: 40611301
Not that i know! sorry
0
 
LVL 90

Expert Comment

by:John Hurst
ID: 40611357
I cannot use the 'drag' method as there are other things I had planned on doing with the rest of my life.  <--- So do I.

So I use the method proposed above by Wilder1626 and train users how to do it. I would not spend time doing their work. No issue at all.
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 40611358
The only other effective way is to write up VBA code to perform the move.
0
 
LVL 11

Expert Comment

by:Wilder1626
ID: 40611363
VBA would be a good option but it would still hang as you would still have the same amount of emails to transfer. I assume you want to do a massive charge and remove the other folder after. Correct?

Then, for all the other users, they would still have to get access to that VBA code also.

So at the end, manually would still be the best way to go since folder names may probably vary. A good training (Best practice)  for all the users would be a good option i would say.
0
 
LVL 11

Expert Comment

by:Wilder1626
ID: 40611377
FYI, what i normally do, if i have a huge amount of email transfer to do, i run it at the end of my day. That way, it transfers while i do something else.
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 40611384
For the VBA code it does not matter much how long the operation lasts, as long as DoEvents is called often.
0
 
LVL 11

Expert Comment

by:Wilder1626
ID: 40611388
ah!!! I get what you are saying Qlemo!!! :-)
0
 
LVL 1

Author Comment

by:jmarkfoley
ID: 40618210
John Hurst:
I would not spend time doing their [users'] work.
I'm totally sympathetic to this sentiment as I found it a bit irritating that they have 10,000+ message in their deleted boxes. That's *not* the place to archive messages. However, as they are simple user-folk after all, they shouldn't have to suffer from Microsoft idiosyncrasies more than usual.

Wilder1626/Qlemo: I like the VB macro idea. Could one of you post code to do this, or point me to a website having such code? I'll do this if I have a ready-to-go-macro, but I've not done Outlook macros in many years and don't want to spend too much time researching, coding, and debugging for a one-shot. If it comes to that, I'll fall back on John Hurst's suggestion!
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 40618588
This should work for the folder structure as shown in the screenshot.
Public Sub MoveDeleted()
Dim itm As Object, del As Folder
  Set del = Session.GetDefaultFolder(olFolderDeletedItems)
  For Each itm In Session.GetDefaultFolder(olFolderInbox).Parent.Folders("DEL").Folders("Deleted Items").Items
    itm.Move del
    DoEvents
  Next
End Sub

Open in new window

If you start that macro, it will move all files, but still allowing to interact with Outlook while doing that. Outlook will not be as responsive as without running the macro, of course. If it is too inresponsive, adding an delay (here 10 ms) will help.
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliSeconds As Long)

Public Sub MoveDeleted()
Dim itm As Object, del As Folder
  Set del = Session.GetDefaultFolder(olFolderDeletedItems)
  For Each itm In Session.GetDefaultFolder(olFolderInbox).Parent.Folders("DEL").Folders("Deleted Items").Items
    itm.Move del
    DoEvents
    Sleep 10
  Next
End Sub

Open in new window

0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 1

Author Comment

by:jmarkfoley
ID: 40620488
Thanks Qlemo! I'll give that a try tomorrow!
0
 
LVL 1

Author Comment

by:jmarkfoley
ID: 40623811
Qlemo: I tried your 1st version and got the message "The attempted operation failed. AN object could not be found.

Questions: Session.GetDefaultFolder(olFolderDeletedItems) ... Session.GetDefaultFolder(olFolderInbox)

Are all folders immediately subordinate to the account accessed via Session.GetDefaultFolder(), or is that for "special" folders like inbox, Deleted Items, Sent Items, etc.

Assuming your code applied to my initial posted image, why do you have the source folder as Session.GetDefaultFolder(olFolderInbox).Parent.Folders("DEL").Folders("Deleted Items")? Not quite sure I understand the syntax, but are you not saying DEL is a subfolder of Inbox? If so, it's not. It is immediately subordinate to the account.

I tried highlighting the source folder DEL > Deleted Items, and modifying your code to the following:
Public Sub MoveDeleted()
Dim itm As Object, del As Folder
  Set del = Session.GetDefaultFolder(olFolderDeletedItems)
 For Each itm In Application.ActiveExplorer.CurrentFolder.Items
    itm.Move del
    DoEvents
  Next
End Sub

Open in new window

That did something, but not exactly right. I put 9 messages in the DEL > Deleted Items folder and ran the macro. It moved 5 of them, not all. I ran it again and it move 2 more; again, 1 more; again it move the last one. Obviously, something's not right. Nor do I know where it moved them because the count of items in the default Deleted Items folder did not change. Will keep experimenting ...
0
 
LVL 1

Author Comment

by:jmarkfoley
ID: 40623840
Found out where they got moved. They were moved into the Outlook Data File, not the @ohprs.org folders, see image. I tried modifying your program by adding:

Dim myNameSpace As Outlook.NameSpace
Set myNameSpace = Application.GetNamespace("MAPI")
Set del = myNameSpace.GetDefaultFolder(olFolderDeletedItems)

but that didn't help. It still moved the items to My Outlook Data Files > Deleted Items. Also, still only moving half the items at a time. Odd.

I think the code is close. If I can get it working that will do the trick, I'm sure.
move-mail-folder-contents.jpg
0
 
LVL 68

Accepted Solution

by:
Qlemo earned 500 total points
ID: 40624217
Session.GetDefaultFolder always refers to specific, predefined folders. Using it for the target folder will always use the primary account. Since that is not what you want to do (neither from nor to primary account), we need to be specific. That requires to write out the mailbox name used (we could search for a folder named like @ohprs.org, but that makes things more complicated).
The other issue is that we are moving stuff away, and the collection we refer to changes, so mails are skipped. We would need to go from last to first item to get it right. I've decided to take a different road:
Public Sub MoveDeleted()
Dim del As Folder
  Set del = Session.Folders("mark@ohprs.org").Folders("Deleted Items")
  While del.Parent.Folders("DEL").Folders("Deleted Items").Items.Count > 0
    del,Parent.Folders("DEL").Folders("Deleted Items").Items(1).Move del
    DoEvents
  Wend
End Sub

Open in new window

0
 
LVL 68

Expert Comment

by:Qlemo
ID: 40624231
If you want to get the folder searched for:
Public Sub MoveDeleted()
Dim del As Folder, fld as Folder
  For Each fld In Session.Folders
    If InStr(fld.Name, "@ohprs.org") > 0 Then Exit For
  Next
  Set del = fld.Folders("Deleted Items")
  While fld.Folders("DEL").Folders("Deleted Items").Items.Count > 0
    fld.Folders("DEL").Folders("Deleted Items").Items(1).Move del
    DoEvents
  Wend
End Sub

Open in new window

0
 
LVL 1

Author Comment

by:jmarkfoley
ID: 40624353
I used your first example -- a couple of apparent syntax errors: I removed the ) at the end of the while statement and change commas to periods in "del,Parent" and "),Items". That seemed to work on my test account.

I'll try this on a real user mailbox and post back results.
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 40624362
@!#~ typos! I've corrected those in the posted code above.
0
 
LVL 1

Author Comment

by:jmarkfoley
ID: 40624965
Qlemo: I'm using the following modification:
Public Sub MoveDeleted()
Dim dst As Folder
  Set dst = Session.Folders("bpatterson@ohprs.org").Folders("SENT")
  While Session.Folders("bpatterson@ohprs.org").Folders("Sent Items").Items.Count > 0
    Session.Folders("bpatterson@ohprs.org").Folders("Sent Items").Items(1).Move dst
    DoEvents
'    Threading.thread.sleep (10)
  Wend
End Sub

Open in new window

It works OK-ish, except as it copies from 'Sent Items' to 'SENT' it also puts a copy in 'Deleted Items'. Is there a way to prevent this? We'll end up with 20,000+ deleted items!
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 40625193
There is no obvious reason why that should happen.
0
 
LVL 1

Author Comment

by:jmarkfoley
ID: 40627429
Yeah, not obvious. Well, I'll try making a copy of the deleted items folder, then clearing out the original after the 'sent items' copy, then putting the sent items back.
0
 
LVL 1

Author Comment

by:jmarkfoley
ID: 40627465
Confirmed: moving from source folder to target folder puts a copy in Deleted Items on the way through! Here's the code I used:
Public Sub MoveDeleted()
Dim dst As Folder
Dim src As Folder
  Set dst = Session.Folders("mpress@ohprs.org").Folders("Sent Items")
  Set src = Session.Folders("mpress@ohprs.org").Folders("SNT").Folders("Sent Items")
  While src.Items.Count > 0
    src.Items(1).Move dst
    DoEvents
  Wend
End Sub

Open in new window

To work around, I saved off the 'Deleted Items' folder to a .pst, then emptied that folder, then ran the script, emptied the 'Deleted Items' folder again, then restored it from .pst. Tedious, but it worked.

I'm curious as to whether anyone has any insight into way it copies to both the programmed destination folder AND the 'Deleted Items' folder. Is a 'Move' treated as copying to the destination folder then deleting from the source folder?
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 40627898
Maybe some rule or other VBA code doing that? A move is no delete, so should never appear in Deleted Items, though the thought is not off.
0
 
LVL 1

Author Comment

by:jmarkfoley
ID: 40628203
Well, just another Microsoft idiosyncrasy, I guess. Thanks for the vb scripts. That is a lifesaver.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
If you don't know how to downgrade, my instructions below should be helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now