PC Review


Reply
Thread Tools Rate Thread

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

 
 
=?Utf-8?B?bWVjZzk2?=
Guest
Posts: n/a
 
      26th Jul 2005
Hi!!

Does anybody know how to create a macro that will move all messages from the
Sent Items to a .pst??
 
Reply With Quote
 
 
 
 
=?Utf-8?B?RXJpYyBMZWdhdWx0IFtNVlAgLSBPdXRsb29rXQ==
Guest
Posts: n/a
 
      26th Jul 2005
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

--
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/


"mecg96" wrote:

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

 
Reply With Quote
 
=?Utf-8?B?bWVjZzk2?=
Guest
Posts: n/a
 
      26th Jul 2005
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

"Eric Legault [MVP - Outlook]" wrote:

> 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
>
> --
> 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/
>
>
> "mecg96" wrote:
>
> > Hi!!
> >
> > Does anybody know how to create a macro that will move all messages from the
> > Sent Items to a .pst??

 
Reply With Quote
 
=?Utf-8?B?RXJpYyBMZWdhdWx0IFtNVlAgLSBPdXRsb29rXQ==
Guest
Posts: n/a
 
      27th Jul 2005
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/


"mecg96" wrote:

> 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
>
> "Eric Legault [MVP - Outlook]" wrote:
>
> > 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
> >
> > --
> > 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/
> >
> >
> > "mecg96" wrote:
> >
> > > Hi!!
> > >
> > > Does anybody know how to create a macro that will move all messages from the
> > > Sent Items to a .pst??

 
Reply With Quote
 
=?Utf-8?B?bWVjZzk2?=
Guest
Posts: n/a
 
      27th Jul 2005
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?

"Eric Legault [MVP - Outlook]" wrote:

> 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/
>
>
> "mecg96" wrote:
>
> > 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
> >
> > "Eric Legault [MVP - Outlook]" wrote:
> >
> > > 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
> > >
> > > --
> > > 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/
> > >
> > >
> > > "mecg96" wrote:
> > >
> > > > Hi!!
> > > >
> > > > Does anybody know how to create a macro that will move all messages from the
> > > > Sent Items to a .pst??

 
Reply With Quote
 
=?Utf-8?B?RXJpYyBMZWdhdWx0IFtNVlAgLSBPdXRsb29rXQ==
Guest
Posts: n/a
 
      27th Jul 2005
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/


"mecg96" wrote:

> 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?
>
> "Eric Legault [MVP - Outlook]" wrote:
>
> > 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/
> >
> >
> > "mecg96" wrote:
> >
> > > 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
> > >
> > > "Eric Legault [MVP - Outlook]" wrote:
> > >
> > > > 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
> > > >
> > > > --
> > > > 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/
> > > >
> > > >
> > > > "mecg96" wrote:
> > > >
> > > > > Hi!!
> > > > >
> > > > > Does anybody know how to create a macro that will move all messages from the
> > > > > Sent Items to a .pst??

 
Reply With Quote
 
lobe
Guest
Posts: n/a
 
      5th Aug 2008


"mecg96" wrote:

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

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A rule for creating a folder based on sender's name and move messages to it hprod Microsoft Outlook 3 21st Feb 2007 11:21 PM
Macro to Move Mail Messages from Draft folder to other folders =?Utf-8?B?VkJub3ZpY2U=?= Microsoft Outlook VBA Programming 4 14th Jun 2006 07:10 PM
Rule to Move messages rather than creating a copy =?Utf-8?B?QWxsYW4gVEZG?= Microsoft Outlook Discussion 4 18th May 2006 08:02 PM
Folder secific rule to move all messages to another folder ~Von Microsoft Outlook Discussion 1 20th Jun 2005 11:42 PM
newbie request: how to build a macro to move messages into a folder based on a message custom field content Cristi@n Microsoft Outlook VBA Programming 5 2nd Mar 2005 04:18 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:00 PM.