export outlook 2003 or 2007 mailbox to pst

Author Message
faulkkev

  • Total Posts : 593
  • Scores: 13
  • Reward points : 0
  • Joined: 11/1/2005
  • Location: Kansas City, MO
  • Status: offline
export outlook 2003 or 2007 mailbox to pst Thursday, October 22, 2009 8:06 AM (permalink)
0
runs local with outlook open on desktop, but could be modified for login script.  Will create a pst file and name it to the logged on users name.  It will mout the pst and will copy the contents of the outlook directory structure to the pst.  Once it is completed just dismount the pst and it should be a full export.  I believe this will work on 2003 and up but I only tested it on 2007.  Also there are const for different versions of pst files ansi or unicode. 
 
 
 
 
WScript.echo "Warning This Script will Use The Default PST Format Supported by The Version of Outlook Your Using. Pre Outlook 2003 Supports A max" &_
" PST Size of 2gig.  Outlook 2003 or Greater Supports PST files over 2gig.  A Registry Change May Be Necessary to Allow Extremely Large PST Files in Unicode To Be Created"
 
' defined for future use if needed
Const olFolderDeletedItems = 3
Const olFolderOutbox = 4
Const olFolderSentMail = 5
Const olFolderInbox = 6
Const olFolderCalendar = 9
Const olFolderContacts = 10
Const olFolderJournal = 11
Const olFolderNotes = 12
Const olFolderTasks = 13
Const olFolderDrafts = 16 
'PST Constants
Const olStoreDefault = 1    ' Supports Default Pst Type for Version of Outlook
Const olStoreUnicode =2     ' Enables Support for Unicode PST Files that can Grow in Excess of 2gig
Const olStoreANSI = 3       ' Enables Ansi Support for pre outlook 2003 versions that support pst files of no more then 2gig
 
Set objOutlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")    
'Get user's name
Set objNetwork = WScript.CreateObject("WScript.Network")
strUserName = objNetwork.UserName    
'create pst add user name
Dim WshShell : Set WshShell = WScript.CreateObject( "WScript.Shell" )
Dim objFSO : Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")     
' PST Folder Path   
OutlookFolder = "C:\Outlook"
'Create Folder if it Doesn't Exist
If Not objFSO.FolderExists(OutlookFolder) Then
 Set oFolder = objFSO.CreateFolder (OutlookFolder)
End If
'Create .Pst file as Username in The Outlook folder
objNameSpace.AddStoreEx OutlookFolder & "\" & objNameSpace.CurrentUser & "pst",olStoreDefault
strpstFolder = objnamespace.currentuser
strdisplayname = "Exported Mailbox"
'Renames PST File To Unique Display Name 
Set pstrename = objNameSpace.Folders.GetLast
pstrename.name = strdisplayname
'Bind to Pst File
Set pstfoldermount = objNameSpace.folders(strdisplayname)
pstroot = pstfoldermount.name
'Set Namspace to Default Mailbox Inbox Folder 
Set objInbox = objNamespace.GetDefaultFolder(olFolderInbox)    
strFolderName = objInbox.Parent
'Sets Default to Mailbox Root vs Inbox. Must bind to inbox like above first Before Parent Below this is by design
Set objMailbox = objNamespace.Folders(strFolderName)    
 
'set collection for mailbox contents at root
Set colFolders = objMailbox.Folders
For Each objFolder In colFolders
 
 If LCase(objfolder.name) = LCase("Deleted Items") Then
  Set objmailboxfolder = objmailbox.folders(objfolder.name)
  Set objPstFolder = objNameSpace.folders(pstroot)
  Set objPstDeleted = objPstFolder.folders("Deleted Items")
  Set copyFolder = objmailboxfolder.CopyTo(objPstdeleted)
  
  
 Else
  
  Set objmailboxfolder = objmailbox.folders(objfolder.name)
  Set objPstFolder = objNameSpace.folders(pstroot)
  Set copyFolder = objmailboxfolder.CopyTo(objPstFolder)
  
 End If
 
Next
 
WScript.echo "Outlook/Exchange Mailbox Export To PST File Completed.  PST Located in C:\outlook Directory"
 
 
 
 
 
 
 
 
 
 
 
 
#1
    faulkkev

    • Total Posts : 593
    • Scores: 13
    • Reward points : 0
    • Joined: 11/1/2005
    • Location: Kansas City, MO
    • Status: offline
    Re:export outlook 2003 or 2007 mailbox to pst Tuesday, November 10, 2009 3:24 AM (permalink)
    0
    I discovered a bug running it against non windows 2007 clients.  I finally got around to playing with it.
     
    modify this line to look like below if the outlook client isn't office 2007.  2003 wants the period in front of the pst. 
     
    objNameSpace.AddStoreEx OutlookFolder & "\" & objNameSpace.CurrentUser & ".pst",olStoreDefault
     
    #2
      dm_4ever

      • Total Posts : 3687
      • Scores: 82
      • Reward points : 0
      • Joined: 6/29/2006
      • Location: Orange County, California
      • Status: offline
      Re:export outlook 2003 or 2007 mailbox to pst Tuesday, November 10, 2009 4:47 AM (permalink)
      0
      ...just a thought...you might modify your script to automatically select the right format depending on the version...

      objOutlook.Version
      dm_4ever

      My philosophy: K.I.S.S - Keep It Simple Stupid
      Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
      Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm
       
      #3
        faulkkev

        • Total Posts : 593
        • Scores: 13
        • Reward points : 0
        • Joined: 11/1/2005
        • Location: Kansas City, MO
        • Status: offline
        Re:export outlook 2003 or 2007 mailbox to pst Wednesday, November 11, 2009 3:09 PM (permalink)
        0
        thanks I have it set to use default pst format I thought by by using the olStoreDefault constant.  Here is an updated version that does work with outlook 2003.  It has an issue with changing the display name and tends to not chang it in the folder listing.  Even though if you right click the created pst and choose properties it will say the new display name. If you close outlook and re open it the correct display name will appear.  To get around it I had to dismount and remount if the version was 2003.  I also had some success strarting the script with outlook closed and use an outlook /cleanviews command but that didn't seem like the best approach.  this one works with outlook open and has been tested on 2003 and 2007 versions.
         
         
        code starts here:
         
        WScript.echo "Warning This Script will Use The Default PST Format Supported by The Version of Outlook Your Using. Pre Outlook 2003 Supports A max" & " PST Size of 2gig.  Outlook 2003 or Greater Supports PST files over 2gig.  A Registry Change May Be Necessary to Allow Extremely Large PST Files in Unicode To Be Created"
         
         
         
        ' defined for future use if needed
        Const olFolderDeletedItems = 3
        Const olFolderOutbox = 4
        Const olFolderSentMail = 5
        Const olFolderInbox = 6
        Const olFolderCalendar = 9
        Const olFolderContacts = 10
        Const olFolderJournal = 11
        Const olFolderNotes = 12
        Const olFolderTasks = 13
        Const olFolderDrafts = 16 
         
         
        'PST Constants
        Const olStoreDefault = 1    ' Supports Default Pst Type for Version of Outlook
        Const olStoreUnicode =2     ' Enables Support for Unicode PST Files that can Grow in Excess of 2gig
        Const olStoreANSI = 3       ' Enables Ansi Support for pre outlook 2003 versions that support pst files of no more then 2gig
         
         
         
        Set objOutlook = CreateObject("Outlook.Application")
        Set objNamespace = objOutlook.GetNamespace("MAPI")    
         
         
        'Get user's name
        Set objNetwork = WScript.CreateObject("WScript.Network")
         
        strUserName = objNetwork.UserName    
         
         
        'create pst add user name
        Dim WshShell : Set WshShell = WScript.CreateObject( "WScript.Shell" )
        Dim objFSO : Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")     
         
        ' PST Folder Path   
        OutlookFolder = "C:\Outlook"
         
        'Create Folder if it Doesn't Exist
        If Not objFSO.FolderExists(OutlookFolder) Then
                        Set oFolder = objFSO.CreateFolder (OutlookFolder)
        End If
         
         
         
        ' Check version of office before creating .Pst
        sComputer = "."
        iOfficeVer = GetOfficeVer(sComputer)
        If iOfficeVer = -1 Then
                        WScript.Echo "Version of Office installed is unknown, " & "could not connect to the remote computer."
                       
        ElseIf iOfficeVer = 0 Then
                        WScript.Echo "Office is not installed."
        Else
                        WScript.Echo "Version of Office installed: " & iOfficeVer
        End If  
         
          
         
        'Create .Pst file as Username in The Outlook folder
         
        If iOfficeVer = "2007" Then
                        objNameSpace.AddStoreEx OutlookFolder & "\" & objNameSpace.CurrentUser & "pst",olStoreDefault
                       
        Else
                        objNameSpace.AddStoreEX OutlookFolder & "\" & objNameSpace.CurrentUser & ".pst",olStoreDefault
                       
                       
                       
                       
        End If
         
        strpstFolder = objnamespace.currentuser
         
        strdisplayname = "Exported Mailbox"
         
        'Renames PST File To Unique Display Name 
        Set pstrename = objNameSpace.Folders.GetLast
        pstrename.name = strdisplayname
         
         
        'Recycle pst mount to get display name to appear if not office 2007
        If iOfficeVer <> "2007" Then
           objNamespace.RemoveStore pstrename ' Step 1 to refresh folder tree view
           objNamespace.AddStore outlookfolder & "\" & strpstfolder & ".pst" ' Step 2 to refresh folder tree view
        end if
        'Bind to Pst File
        Set pstfoldermount = objNameSpace.folders(strdisplayname)
        pstroot = pstfoldermount.name
        'Set Namspace to Default Mailbox Inbox Folder 
        Set objInbox = objNamespace.GetDefaultFolder(olFolderInbox)    
        strFolderName = objInbox.Parent
         
        'Sets Default to Mailbox Root vs Inbox. Must bind to inbox like above first Before Parent Below this is by design
        Set objMailbox = objNamespace.Folders(strFolderName)    
         
         
         
        'set collection for mailbox contents at root
        Set colFolders = objMailbox.Folders
         
        For Each objFolder In colFolders
                       
                        If LCase(objfolder.name) = LCase("Deleted Items") Then
                                        Set objmailboxfolder = objmailbox.folders(objfolder.name)
                                        Set objPstFolder = objNameSpace.folders(pstroot)
                                        Set objPstDeleted = objPstFolder.folders("Deleted Items")
                                        Set copyFolder = objmailboxfolder.CopyTo(objPstdeleted)
                                       
                                       
                        Else
                                       
                                        Set objmailboxfolder = objmailbox.folders(objfolder.name)
                                        Set objPstFolder = objNameSpace.folders(pstroot)
                                        Set copyFolder = objmailboxfolder.CopyTo(objPstFolder)
                                       
                        End If
                       
        Next
         
         
         
        ' sub Dismount PST
         
        get_DismountPST()
         
         
        WScript.echo "Outlook/Exchange Mailbox Export To PST File Completed.  PST Has Been Dismounted and is Located in C:\outlook Directory"
         
         
         
         
         
         
        '******************* FUNCTIONS and Subs *************************************************************
         
         
         
        '*** Sub Dismount PST ******
        Sub get_DismountPST()
         
        Set myOlApp = CreateObject("Outlook.Application")
        Set myNS = myolapp.GetNamespace("MAPI")
        set pstfolder = myns.folders("Exported Mailbox")
         
         
        myNS.removeStore pstfolder
         
        End sub
         
         
         
         
         
         
         
        '*** Function
         
        Function GetOfficeVer(sNode)
                        On Error Resume Next
                        Const HKLM = &H80000002 'HKEY_LOCAL_MACHINE
                        Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & sNode & "/root/default:StdRegProv")
                        If Err.Number <> 0 Then
                                        GetOfficeVer = -1
                                        Exit Function  '------->
                        End If
                        sValueName = "Path"
                        sRegPre = "SOFTWARE\Microsoft\Office\"
                        sRegPost = "\Common\InstallRoot"
                       
                        If oReg.GetStringValue(HKLM, sRegPre & "12.0" & sRegPost, sValueName, sValue) = 0 Then
                                        GetOfficeVer = 2007
                        ElseIf oReg.GetStringValue(HKLM, sRegPre & "11.0" & sRegPost, sValueName, sValue) = 0 Then
                                        GetOfficeVer = 2003
                        ElseIf oReg.GetStringValue(HKLM, sRegPre & "10.0" & sRegPost, sValueName, sValue) = 0 Then
                                        GetOfficeVer = 2002
                        ElseIf oReg.GetStringValue(HKLM, sRegPre & "9.0" & sRegPost, sValueName, sValue) = 0 Then
                                        GetOfficeVer = 2000
                        ElseIf oReg.GetStringValue(HKLM, sRegPre & "8.0" & sRegPost, sValueName, sValue) = 0 Then
                                        GetOfficeVer = 97
                        Else
                                        GetOfficeVer = 0
                        End If
        End Function
         
         
         
         
         
         
        #4

          Online Bookmarks Sharing: Share/Bookmark

          Jump to:

          Current active users

          There are 0 members and 1 guests.

          Icon Legend and Permission

          • New Messages
          • No New Messages
          • Hot Topic w/ New Messages
          • Hot Topic w/o New Messages
          • Locked w/ New Messages
          • Locked w/o New Messages
          • Read Message
          • Post New Thread
          • Reply to message
          • Post New Poll
          • Submit Vote
          • Post reward post
          • Delete my own posts
          • Delete my own threads
          • Rate post

          2000-2012 ASPPlayground.NET Forum Version 3.9