A Microsoft Outlook email forum. Outlook Banter

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » Outlook Banter forum » Microsoft Outlook Email Newsgroups » Outlook and VBA
Site Map Home Register Authors List Search Today's Posts Mark Forums Read Web Partners

Archiving with VBA



 
 
Thread Tools Search this Thread Display Modes
  #1  
Old September 8th 08, 02:37 PM posted to microsoft.public.outlook.program_vba
Guest
 
Posts: n/a
Default Archiving with VBA

Hello all

I found a code on http://www.outlookcode.com

The latter to the advantage of opening a pst archives, moving the former
mails
from a folder (in this example: deleted items) and then closed the
PST.

Code:
Option Explicit

''=======================================================================
''  Code for attaching my archive pst, moving older emails to
''  a specific folder within this pst and then detaching it.
''
''  In this example all items in the Deleted Items folder older than
''  60 days are moved to my own archive file into the 'Deletions' folder
''=======================================================================
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private Const m_strDeletedPST As String = "C:\Outlook_Data\archivage.pst"
Private Const m_strDelDispName As String = "Archives"
Private Const m_iDays As Integer = 60

Sub MoveOldMail()
''=======================================================================
''  This routine is visible as a macro and is the heart of the move process
''  Calls: AttachPST, DetachPST, Quote
''=======================================================================

On Error GoTo Proc_Err

    Dim blnSuccess As Boolean
    Dim objNS As Outlook.NameSpace
    Dim objAllItems As Outlook.Items
    Dim objItemsToMove As Outlook.Items
    Dim objItem As Object
    Dim objTargetFolder As Outlook.MAPIFolder
    Dim objPST As Outlook.MAPIFolder
    Dim strSearch As String
    Dim iCount As Integer
    Dim i As Integer

    ''Attach pst file
    blnSuccess = AttachPST(m_strDeletedPST, m_strDelDispName, objPST)

    If Not blnSuccess Then
        MsgBox "Could not attached '" & m_strDeletedPST & "', aborting
move."
        GoTo Proc_Exit
    End If

    '' Wait a couple of seconds for everything to catch up
    Sleep 3000

    ''We have the archive pst attached
    Set objNS = Application.GetNamespace("MAPI")
    Set objAllItems = objNS.GetDefaultFolder(olFolderDeletedItems).Items

    ''create filter based on date
    strSearch = "[Reçu] = " & Quote(FormatDateTime(Now - m_iDays,
vbShortDate) & " " & _
                 FormatDateTime(Now - m_iDays, vbShortTime))

    ''========== Move Deleted Items =============
    ''Get the 'Deletions' folder in the newly attached pst file
    Set objTargetFolder = objPST.Folders.Item("éléments supprimés")

    ''Now restrict the email according to date
    Set objItemsToMove = objAllItems.Restrict(strSearch)

    ''Get count of all items to be moved
    iCount = objItemsToMove.Count

    Debug.Print "Deleted Items: " & iCount

    '' Loop from back to front of the restricted collection, moving each
file
    For i = iCount To 1 Step -1
        objItemsToMove.Item(i).Move objTargetFolder
    Next


    '' Now detach the added pst file
    DetachPST m_strDelDispName

    '' Wait a couple of seconds for everything to catch up
    Sleep 3000


Proc_Exit:
    ''Clean up
    If Not objAllItems Is Nothing Then Set objAllItems = Nothing
    If Not objItem Is Nothing Then Set objItem = Nothing
    If Not objItemsToMove Is Nothing Then Set objItemsToMove = Nothing
    If Not objTargetFolder Is Nothing Then Set objTargetFolder = Nothing
    If Not objNS Is Nothing Then Set objNS = Nothing

    Exit Sub
Proc_Err:
    MsgBox Err.Description, , "MoveOldMail"
    GoTo Proc_Exit

End Sub

Private Function AttachPST(astrPSTName As String, astrDisplayName As String,
aobj As Outlook.MAPIFolder) As Boolean
''=======================================================================
''  This routine used the received information to attach an existing pst
''  file, returning a handle to the attached file
''=======================================================================
On Error GoTo Proc_Err
    Dim objNS As Outlook.NameSpace


    'Check if pst file exists, if exist then Add pst file...
    If Len(Dir$(astrPSTName)) = 0 Then
        MsgBox "Cannot connect to 'Deleted' pst file"
        Exit Function
    End If

    Set objNS = Application.GetNamespace("MAPI")
    objNS.AddStore astrPSTName
    Set aobj = objNS.Folders.GetLast
    'Change the Display Name from the new pst file ...
    aobj.Name = astrDisplayName

    '' Return success code
    AttachPST = True

Proc_Exit:
    ''If Not objFolder Is Nothing Then Set objFolder = Nothing
    If Not objNS Is Nothing Then Set objNS = Nothing
    Exit Function
Proc_Err:
    MsgBox Err.Description, , "AttachPST"
    AttachPST = False
    GoTo Proc_Exit
End Function


Function DetachPST(astrDisplayName As String) As Boolean
''=======================================================================
''  This routine used the received display name to close an existing pst
''  file
''=======================================================================
On Error GoTo Proc_Err
   Dim objNS As Outlook.NameSpace
   Dim objFolder As Outlook.MAPIFolder

    Set objNS = Application.GetNamespace("MAPI")
    Set objFolder = objNS.Folders.Item(astrDisplayName)
    objNS.RemoveStore objFolder

    '' Return success code
    DetachPST = True

Proc_Exit:
    If Not objFolder Is Nothing Then Set objFolder = Nothing
    If Not objNS Is Nothing Then Set objNS = Nothing
    Exit Function

Proc_Err:
    MsgBox Err.Description, , "DetachPST"
    DetachPST = False
    GoTo Proc_Exit

End Function

Private Function Quote(MyText)
    ''Used for properly quoting the filter string
    Quote = Chr(34) & MyText & Chr(34)
End Function

I know how he indicated the box receipt but not how to tell him to go
through all subfolders and me recreate the same tree in the pst archiving.

If someone can help me fill this code would be super.

Thanks in advance

seb



Ads
  #2  
Old September 9th 08, 08:18 AM posted to microsoft.public.outlook.program_vba
Michael Bauer [MVP - Outlook]
external usenet poster
 
Posts: 1,885
Default Archiving with VBA



This demonstrates how to loop recursively through folders and its
subfolders:
http://www.vboffice.net/sample.html?...cmd=showite m

--
Best regards
Michael Bauer - MVP Outlook

: VBOffice Reporter for Data Analysis & Reporting
: Outlook Categories? Category Manager Is Your Tool
: http://www.vboffice.net/product.html?pub=6&lang=en


Am Mon, 8 Sep 2008 14:37:02 +0200 schrieb am:

Hello all

I found a code on
http://www.outlookcode.com

The latter to the advantage of opening a pst archives, moving the former
mails
from a folder (in this example: deleted items) and then closed the
PST.

Code:
 
 Option Explicit
 
 ''=======================================================================
 ''  Code for attaching my archive pst, moving older emails to
 ''  a specific folder within this pst and then detaching it.
 ''
 ''  In this example all items in the Deleted Items folder older than
 ''  60 days are moved to my own archive file into the 'Deletions' folder
 ''=======================================================================
 Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
 
 Private Const m_strDeletedPST As String = "C:\Outlook_Data\archivage.pst"
 Private Const m_strDelDispName As String = "Archives"
 Private Const m_iDays As Integer = 60
 
 Sub MoveOldMail()
 ''=======================================================================
 ''  This routine is visible as a macro and is the heart of the move
Code:
process
 ''  Calls: AttachPST, DetachPST, Quote
 ''=======================================================================
 
 On Error GoTo Proc_Err
 
     Dim blnSuccess As Boolean
     Dim objNS As Outlook.NameSpace
     Dim objAllItems As Outlook.Items
     Dim objItemsToMove As Outlook.Items
     Dim objItem As Object
     Dim objTargetFolder As Outlook.MAPIFolder
     Dim objPST As Outlook.MAPIFolder
     Dim strSearch As String
     Dim iCount As Integer
     Dim i As Integer
 
     ''Attach pst file
     blnSuccess = AttachPST(m_strDeletedPST, m_strDelDispName, objPST)
 
     If Not blnSuccess Then
         MsgBox "Could not attached '" & m_strDeletedPST & "', aborting
 move."
         GoTo Proc_Exit
     End If
 
     '' Wait a couple of seconds for everything to catch up
     Sleep 3000
 
     ''We have the archive pst attached
     Set objNS = Application.GetNamespace("MAPI")
     Set objAllItems = objNS.GetDefaultFolder(olFolderDeletedItems).Items
 
     ''create filter based on date
     strSearch = "[Reæ´ = " & Quote(FormatDateTime(Now - m_iDays,
 vbShortDate) & " " & _
                  FormatDateTime(Now - m_iDays, vbShortTime))
 
     ''========== Move Deleted Items =============
     ''Get the 'Deletions' folder in the newly attached pst file
     Set objTargetFolder = objPST.Folders.Item("è*©ments supprimè±¢)
 
     ''Now restrict the email according to date
     Set objItemsToMove = objAllItems.Restrict(strSearch)
 
     ''Get count of all items to be moved
     iCount = objItemsToMove.Count
 
     Debug.Print "Deleted Items: " & iCount
 
     '' Loop from back to front of the restricted collection, moving each
 file
     For i = iCount To 1 Step -1
         objItemsToMove.Item(i).Move objTargetFolder
     Next
 
 
     '' Now detach the added pst file
     DetachPST m_strDelDispName
 
     '' Wait a couple of seconds for everything to catch up
     Sleep 3000
 
 
 Proc_Exit:
     ''Clean up
     If Not objAllItems Is Nothing Then Set objAllItems = Nothing
     If Not objItem Is Nothing Then Set objItem = Nothing
     If Not objItemsToMove Is Nothing Then Set objItemsToMove = Nothing
     If Not objTargetFolder Is Nothing Then Set objTargetFolder = Nothing
     If Not objNS Is Nothing Then Set objNS = Nothing
 
     Exit Sub
 Proc_Err:
     MsgBox Err.Description, , "MoveOldMail"
     GoTo Proc_Exit
 
 End Sub
 
 Private Function AttachPST(astrPSTName As String, astrDisplayName As
String,
 aobj As Outlook.MAPIFolder) As Boolean
 ''=======================================================================
 ''  This routine used the received information to attach an existing pst
 ''  file, returning a handle to the attached file
 ''=======================================================================
 On Error GoTo Proc_Err
     Dim objNS As Outlook.NameSpace
 
 
     'Check if pst file exists, if exist then Add pst file...
     If Len(Dir$(astrPSTName)) = 0 Then
         MsgBox "Cannot connect to 'Deleted' pst file"
         Exit Function
     End If
 
     Set objNS = Application.GetNamespace("MAPI")
     objNS.AddStore astrPSTName
     Set aobj = objNS.Folders.GetLast
     'Change the Display Name from the new pst file ...
     aobj.Name = astrDisplayName
 
     '' Return success code
     AttachPST = True
 
 Proc_Exit:
     ''If Not objFolder Is Nothing Then Set objFolder = Nothing
     If Not objNS Is Nothing Then Set objNS = Nothing
     Exit Function
 Proc_Err:
     MsgBox Err.Description, , "AttachPST"
     AttachPST = False
     GoTo Proc_Exit
 End Function
 
 
 Function DetachPST(astrDisplayName As String) As Boolean
 ''=======================================================================
 ''  This routine used the received display name to close an existing pst
 ''  file
 ''=======================================================================
 On Error GoTo Proc_Err
    Dim objNS As Outlook.NameSpace
    Dim objFolder As Outlook.MAPIFolder
 
     Set objNS = Application.GetNamespace("MAPI")
     Set objFolder = objNS.Folders.Item(astrDisplayName)
     objNS.RemoveStore objFolder
 
     '' Return success code
     DetachPST = True
 
 Proc_Exit:
     If Not objFolder Is Nothing Then Set objFolder = Nothing
     If Not objNS Is Nothing Then Set objNS = Nothing
     Exit Function
 
 Proc_Err:
     MsgBox Err.Description, , "DetachPST"
     DetachPST = False
     GoTo Proc_Exit
 
 End Function
 
 Private Function Quote(MyText)
     ''Used for properly quoting the filter string
     Quote = Chr(34) & MyText & Chr(34)
 End Function
 



I know how he indicated the box receipt but not how to tell him to go
through all subfolders and me recreate the same tree in the pst archiving.

If someone can help me fill this code would be super.

Thanks in advance

seb

 




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Archiving Pawe³ Rzeczewski Outlook - General Queries 2 October 26th 07 02:11 AM
Archiving ScoobyDoo Outlook - General Queries 1 May 23rd 07 09:03 PM
archiving etc Tim Scott Mathews Outlook - General Queries 1 November 27th 06 03:46 PM
Archiving Jordan Outlook and VBA 3 March 2nd 06 10:30 PM
Archiving mcp6453 Outlook Express 1 January 21st 06 04:26 AM


All times are GMT +1. The time now is 08:19 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.Search Engine Friendly URLs by vBSEO 2.4.0
Copyright ©2004-2025 Outlook Banter.
The comments are property of their posters.