Creating a Macro to move all messages from one folder to another..

G

Guest

Hi!!

Does anybody know how to create a macro that will move all messages from the
Sent Items to a .pst??
 
G

Guest

How about this:

Sub CopySentItemsMessagesToPSTFile(PSTFilePath As String)
On Error Resume Next

Dim objNS As Outlook.NameSpace
Dim objSentItemsFolder As Outlook.MAPIFolder, objDestinationFolder As
Outlook.MAPIFolder
Dim objItem As Object
Dim intX As Integer

Set objNS = Application.GetNamespace("MAPI")
Set objSentItemsFolder = objNS.GetDefaultFolder(olFolderSentMail)

objNS.AddStore PSTFilePath 'PST will be created if it doesn't exist
Set objDestinationFolder = objNS.Folders.GetLast 'Get the PST we just
added
Set objDestinationFolder = objDestinationFolder.Folders("Inbox")
If Err.Number <> 0 Then
'Inbox doesn't exist; create it
Set objDestinationFolder = objDestinationFolder.Folders.Add("Inbox",
olFolderInbox)
End If

For intX = objSentItemsFolder.Items.Count To 1 Step -1
Set objItem = objSentItemsFolder.Items(intX)
objItem.Move objDestinationFolder
Next

Leave:
Set objNS = Nothing
Set objSentItemsFolder = Nothing
Set objDestinationFolder = Nothing
Set objItem = Nothing
End Sub
 
G

Guest

Thanks!! It is useful. I want to use a specific .pst which is called BackUp
and has a folder called "Old Send Items". The Outlook data file name is
MyBackUp.

Do I have to reference the file and it's path in the hard drive, or is there
a simpler way like: Folders("Old Sent Items") ?

MC
 
G

Guest

If the .pst is already loaded in Outlook, then you can retrieve its top-level
folder by using NameSpace.Folders("PSTNAME").

If it is not loaded, then you need to use the AddStore method with the full
path to the pst's file name. If this file doesn't exist, this method will
create it.

To reference a specific folder in a pst, you have to continually walk
through a series of Folders collections until you find the nested folder you
need from the root NameSpace.Folders("LOADEDPST") collection.

i.e. Set objInboxSubFolder =
NameSpace.Folders("LOADEDPST").Folders("Inbox").Folders("InboxSubFolder")

--
Eric Legault - B.A, MCP, MCSD, Outlook MVP
Try Picture Attachments Wizard for Outlook! http://tinyurl.com/ckytm
Job: http://www.imaginets.com
Blog: http://blogs.officezealot.com/legault/
 
G

Guest

I changed it to:
Set objDestinationFolder = NameSpace.Folders("BackUp").Folders("Old Sent
Items")
But I get the "Variable not defined" error for "NameSpace"

Before you used the Set objNS = Application.GetNamespace("MAPI") to refer to
the Mailbox, right?

I guess the same has to be done with the BackUp folder?
 
G

Guest

Yeah, sorry - I used "NameSpace" as an object instead of using a variable
name. So you would use this:

Dim objNS As Outlook.Namespace

Set objNS = Application.GetNameSpace("MAPI")

This object contains a Folders collection which represents all loaded .pst
files, as well as Exchange Mailboxes and Public Folders. So if you had two
..pst files loaded, Folders.Count would equal 2. To get a reference to any of
the .pst files, either set a reference to variable by calling the
Folders.GetFirst or Folders.GetLast method (i.e. Set objFolder =
objNS.Folders.GetFirst), or call it by name (Set objFolder =
objNS.Folders("PST1")) or odinal number (Set objFolder = objNS.Folders(1)).

These methods only get you the very top folder in the pst hierarchy - which
contains NO items! All the default folders are one level down:

Set objTopLevelFolder = objNS.Folders("PST1")
Set objInbox = objTopLevelFolder.Folders("Inbox")
Set objInboxSubFolder = objInbox.Folders("Subfolder of Inbox")

And keep going for deeper nested folders. Does this make sense?

--
Eric Legault - B.A, MCP, MCSD, Outlook MVP
Try Picture Attachments Wizard for Outlook! http://tinyurl.com/ckytm
Job: http://www.imaginets.com
Blog: http://blogs.officezealot.com/legault/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top