Outlook 365 Outlook folder export to PST archival tool - close, but need a nudge

macdotcom

New Member
OS Version(s)
  1. Windows
Outlook version
Outlook 365 64 bit
Email Account
IMAP
Operating system::    Win 11 & 10
Outlook version:     Office 365
Email type or host:    IMAP

I've been working on a PST archival tool based on this thread (Export Folder to PST).

My background is strong in Excel VBA and VB, but Outlook VBA is new to me so I probably have a declaration wrong or using a wrong object type. I'm sure members here will probably spot it easily, but i'm lost on what I've missed.

Some background: We are an engineering firm. In my office, we have about 200 projects a year, some large (1000's of emails, go on for years) and some small (maybe 10, quick and done). In the last 10 years, I've hit the limit 4 times on size of my account. In the past, I manually exported stored project emails to PSTs to save on the network server in the projects folder. I have to save them, in case there's an issue down the line. But when I need to manually archive 50 folders once a year, typing each one and making 10 clicks to use the built in export tool, it's a royal PITA.

So when I found the thread above, I was excited to get something going -- and it did for a while. And I started this little side project, trying to make it universal so I can share with others in my company, who may not be so Outlook/VBA savvy. I had it working at one point, but now it's not. The problem is that when I get to the indicated section below, I am getting the old "Object or With Variable ..." error and I am blind to see what I'm messed up because this new Outlook object model is a little alien to me.

Finally, the one thing that has driven me nuts for years with PST archival. The Outlook file lock, even after the PST is closed. Is there a way around that?

Help appreciated to push me in the right direction.

Option Explicit

Sub ArchiveEmailtoPST()
'Outlook email archival tool - moves all contents of a folder in Outlook
' to an external PST file, without encryption or passwords
' you can move this PST file afterwards to a project folder for long
' term storage

Dim olNS As Outlook.NameSpace
Dim olPickedFld As Outlook.Folder
Dim olPickedItem, olPSTFile As Object
Dim strPath, strFileName, strUserInitials As String
Dim N, NumItems As Long

Set olNS = GetNamespace("MAPI")

strUserInitials = GetInitials
If Len(strUserInitials) = 0 Then
MsgBox "Please set your user initials. Go to 'File' | 'Options' | 'General'"
GoTo CleanExit 'Don't know who the user is
End If

On Error Resume Next
Set olPickedFld = olNS.PickFolder
strFileName = olPickedFld.Name
If TypeName(olPickedFld) = "Nothing" Then GoTo CleanExit 'Command was cancelled

NumItems = olPickedFld.Items.Count

On Error GoTo 0 'Disable error handling

If IsDefaultFolder(olPickedFld) Then 'Test for a Outlook Default folder being picked
MsgBox "You selected '" & olPickedFld & "' ... archival not allowed."
GoTo CleanExit
End If

strPath = AddTrailSlash(SelectFolder(GetDesktop)) 'Default file location but user can pick

'Develop the File pathname for export using users initials and today's date
strFileName = strFileName & "_" & strUserInitials & "_" & Format(Date, "yyyymmdd") & ".pst"
strPath = strPath & strFileName

'Creates a PST file at the path (strPath).
olNS.AddStore strPath
Set olPSTFile = olNS.Folders.GetLast
olPSTFile.Name = strFileName

'Moves across all the items in the selected folder to the PST
For N = NumItems To 1 Step -1
Set olPickedItem = olPickedFld.Items(N) '********************************************************************** I GET ERROR HERE
olPickedItem.Move olPSTFile
Next

'Closes the PST file - you will need to restart Outlook
' before you can move this file to its final location
olNS.RemoveStore olPSTFile 'Is there a workaround to have Outlook release the lock on the PST so it can be moved without closing/restarting Outlook?

'Deletes the old email folder in Outlook - it should be already empty, so no concern.
If olPickedFld.Items.Count = 0 Then olPickedFld.Delete

CleanExit:
Set olNS = Nothing
Set olPickedFld = Nothing
Set olPSTFile = Nothing
Set olPickedItem = Nothing

End Sub

Function IsDefaultFolder(fld As MAPIFolder) As Boolean
' test if selected folder in an Outlook internal default folder
' trying to rename a default folder will produce an error

Dim strTestName, strSaveName As String
strSaveName = fld.Name
strTestName = "DeleteTest_" & fld.Name

On Error GoTo Errorhandler 'error trap
fld.Name = strTestName 'try to rename the folder
On Error GoTo 0 ' diable error handling

fld.Name = strSaveName 'rename it back to original
IsDefaultFolder = False ' it's not a default folder
GoTo CleanExit

Errorhandler:
'an error occurred renaming, it's probably becuase of a default folder selected.
IsDefaultFolder = True 'assume it's a default folder

CleanExit:
Set fld = Nothing
End Function

Function RegKeyRead(i_RegKey As String) As String
'this function reads registry key entries
Dim myWS As Object
On Error Resume Next
Set myWS = CreateObject("WScript.Shell")
RegKeyRead = myWS.RegRead(i_RegKey)
End Function

Function GetInitials() As String
'this function reads the user initials from MS Office in registry keys
GetInitials = RegKeyRead("HKEY_CURRENT_USER\Software\Microsoft\Office\Common\UserInfo\UserInitials")
End Function

Function GetDesktop() As String
'returns the path to the User's Desktop folder
GetDesktop = CreateObject("WScript.Shell").SpecialFolders("Desktop")
End Function

Function SelectFolder(strStartPath As String) As String
'PURPOSE: Have User Select a Folder Path and Store it to a variable
'based on ~~~ TheSpreadsheetGuru.com/the-code-vault

Dim FldrPicker As FileDialog
Dim strLastChar As String
Dim xlApp As Object

'Remove the Trail \ if it's there
strStartPath = RemTrailSlash(strStartPath)

Set xlApp = CreateObject("Excel.Application") 'Outlook doesn't have dialog, so we'll use Excel's
xlApp.Visible = False

Dim fd As Office.FileDialog
Set fd = xlApp.Application.FileDialog(msoFileDialogFilePicker)

'Have User Select Folder to Save to with Dialog Box
' for help, see: VBA File Dialogs
Set FldrPicker = xlApp.Application.FileDialog(msoFileDialogFolderPicker)
With FldrPicker
.ButtonName = "Select Folder"
.InitialFileName = strStartPath
.Filters.Clear
.AllowMultiSelect = False
.InitialView = msoFileDialogViewList
.Title = "Select A Target Folder"
If .Show <> -1 Then 'Check if user clicked cancel button
SelectFolder = strStartPath
Else
SelectFolder = .SelectedItems(1) 'Default will put it on the Desktop
End If
End With

xlApp.Quit
Set xlApp = Nothing
Set FldrPicker = Nothing

End Function

Function AddTrailSlash(strPath As String) As String
' adds Trailing backslash on a path string
If Right$(strPath, 1) = "\" Then
AddTrailSlash = strPath
Else
AddTrailSlash = strPath & "\"
End If
End Function

Function RemTrailSlash(strPath As String) As String
' removes Trailing backslash on a path string
If Right$(strPath, 1) = "\" Then
RemTrailSlash = strPath = Left$(strPath, Len(strPath) - 1)
Else
RemTrailSlash = strPath
End If
End Function
 
Is the pst file open in the profile? That error seems to say it is not.


>>
Finally, the one thing that has driven me nuts for years with PST archival. The Outlook file lock, even after the PST is closed. Is there a way around that?
>>
It should be locked if open in Outlook - no way to avoid it, but if the folder is not used for 10 minutes or so, it might unlock. Otherwise, close the pst - that should be locked after 10 minutes or until you close outlook.
 
Is the pst file open in the profile? That error seems to say it is not.


>>
Finally, the one thing that has driven me nuts for years with PST archival. The Outlook file lock, even after the PST is closed. Is there a way around that?
>>
It should be locked if open in Outlook - no way to avoid it, but if the folder is not used for 10 minutes or so, it might unlock. Otherwise, close the pst - that should be locked after 10 minutes or until you close outlook.
It is created and opened in the block just above the step where it generates the RTE.

I'm feeling like I don't have the objects dim'd correctly.
 
Similar threads
Thread starter Title Forum Replies Date
P Can't add custom field to custom Outlook form, it always adds to the Folder instead Outlook VBA and Custom Forms 2
P Yahoo/IMAP folder rename by Outlook desktop 365 Using Outlook 0
A Outlook 2019 folder counter Using Outlook 0
N Reply to Outlook messages by moving messages to a specific Outlook folder Outlook VBA and Custom Forms 1
A Outlook 365 (OutLook For Mac)Move "On My Computer" Folder Items From Old To New Mac Computer Using Outlook 3
HarvMan Outlook 365 - Rule to Move an Incoming Message to Another Folder Using Outlook 4
S Email Macros to go to a SHARED Outlook mailbox Draft folder...NOT my personal Outlook Draft folder Using Outlook 2
Kika Melo Outlook Calendar deleted appointments not in Deleted Items folder Using Outlook 3
S Adding a recipient's column to Sent folder in Outlook 2010 Outlook VBA and Custom Forms 1
C Outlook 365 Copy/Save Emails in Folder Outside Outlook to Show Date Sender Recipient Subject in Header Using Outlook 0
richardwing Auto forward email that is moves into a specific outlook folder Outlook VBA and Custom Forms 5
bhamberg Shortcuts in Folder Pane (Outlook 2016) Using Outlook 19
G VBA to save selected Outlook msg with new name in selected network Windows folder Outlook VBA and Custom Forms 1
G Retention Policy for Outlook.com Deleted Items folder Using Outlook 0
Z Outlook 365 delete reminder you can’t make change to contents of this-read only folder Using Outlook 4
O The Outlook API wrongfully shows an outlook folder to have zero sub-folders Outlook VBA and Custom Forms 1
O The Outlook API wrongfully shows an outlook folder to have zero sub-folders Outlook VBA and Custom Forms 2
S Outlook (2016 32bit; Gmail IMAP) - Save sent message to Outllook Folder Outlook VBA and Custom Forms 0
G How to add a folder shortcut to outlook quick access toolbar? Using Outlook 6
G Add to Outlook Contacts - Point to non-default contacts folder Using Outlook 0
B Spam folder not showing in Outlook Using Outlook 5
J Moved many emails to Outlook external folder, need to delete on Gmail server Using Outlook 14
E How to display "Change Folder" in Change Default Email Delivery Location in Exchange Outlook 2016 Using Outlook 1
D Outlook 2016 Deleting emails without affecting folder structure - Outlook Office 365 Using Outlook 3
B Outlook 365 - Folder pane list clears Using Outlook 28
T Missing Folder in Outlook.com Using Outlook 3
R Setup autofoward rule on a particular folder in Outlook Using Outlook 0
N Outlook 2016 Folder Icons Look Using Outlook 0
B Outlook - Mail from safe senders list being sent to Spam Folder Using Outlook 0
9 Outlook 2016 How to save an Outlook attachment to a specific folder then delete the email it came from? Using Outlook 1
R "Can't store Outlook data files under the AppData folder. Please choose another folder." Using Outlook 6
G Can't create Folder Groups in Outlook 2013 Using Outlook 0
geofferyh Outlook 2010 How to Copy Outlook Attachment to a Specific Folder? Outlook VBA and Custom Forms 3
R Can not create folder to store specific emails in in Outlook for Mac Using Outlook 1
N Lots of Folder in Outlook Android Using Outlook 2
A Outlook macro to create search folder with mail categories as criteria Outlook VBA and Custom Forms 3
P Outlook 2016 Client and 365 - Groups Folder Using Outlook 3
R Copy Outlook Public Folders to a File Server Shared Folder Using Outlook 0
R How to delete Facebook Contacts folder in Outlook 365 Using Outlook 7
E Accessing shared outlook folder doesn't work since switch to new outlook/excel Outlook VBA and Custom Forms 11
P when i move inbox mails to another folder in outlook the mail disappears Using Outlook 1
W Save Outlook attachment in network folder and rename to current date and time Outlook VBA and Custom Forms 18
P Outlook 2010 trusted emails going to spam folder Using Outlook 18
T Missing Outlook subordinate folder Using Outlook 0
A remove or turn off outlook.com contact folder from outlook 2016 Using Outlook 4
N Outlook 2013 Folder View Using Outlook 8
V Spam folder not showing in Outlook Using Outlook 4
T Outlook creating a folder named: "Unwanted" Using Outlook 3
J Searching message folder in Outlook 2016 Using Outlook 5
M code to move selected Outlook contacts to another folder Using Outlook 3

Similar threads

Back
Top