BackupEmails
This sub procedure is the main procedure that will be called by
btnFunny.
Its main purpose will be to loop through all the selected folders and
subfolders, to obtain all the emails, format the emails properly, and then of
course, save them. Add the next sub:
'''Main Sub to Backup All Emails
Sub BackupEmails()
Dim i As Long 'First Loop - Folders
Dim j As Long 'Second Loop - Subfolders
Dim lngRemoveSlash As Long 'Name, After Unecessary \ Was Removed
Dim strSubject As String 'Email Subject
Dim strEmailName As String 'Email Name
Dim strFileName As String 'Filename of Email
Dim strPath As String 'Path
Dim strSelFolder As String 'Selected Outlook Folder
Dim strSelFolderPath As String 'Selected Outlook Folder Path
Dim strSaveToFolder As String 'Where to Save to
Dim outNameSpace As Outlook.NameSpace 'Outlook Namespace
Dim objApp As Outlook.Application 'Outlook Application
Dim objMailFolder As Outlook.MAPIFolder 'Outlook Mail Folder
Dim objMailItem As Outlook.MailItem 'Outlook Email Item
Dim objFSO As Object 'File System Object
Dim objFolderToBackup As Object 'Which Folder Must We Backup
Dim colFolders As New Collection 'All Folders
Dim colEntryID As New Collection 'All Entities
Dim colStoreID As New Collection 'All Stores
objFSO = CreateObject("Scripting.FileSystemObject") 'Create FSO Object
objApp = New Outlook.Application 'Create Outlook Instance
outNameSpace = objApp.GetNamespace("MAPI") 'Get Appropriate Outlook Namespace
objFolderToBackup = outNameSpace.PickFolder 'Choose Folder to Backup
If objFolderToBackup Is Nothing Then 'Nothing Selected
Exit Sub
End If
strPath = CustomBrowseForFolder() 'Show & Get Output Folder
If strPath = "" Then 'If No Path Chosen
Exit Sub
End If
If Not Right(strPath, 1) = "\" Then 'Add \ Afterwards
strPath = strPath & "\"
End If
Call GetOutLookFolders(colFolders, colEntryID, colStoreID, objFolderToBackup) 'Get All Folders
For i = 1 To colFolders.Count 'Loop Through Folders
strSelFolder = StripCharacters(colFolders(i)) 'Remove Characters that aren't Allowed in Filenames
lngRemoveSlash = InStr(3, strSelFolder, "\") + 1 'Remove \
strSelFolder = Mid(strSelFolder, lngRemoveSlash, 256) 'Edited Filename
strSelFolderPath = strPath & strSelFolder & "\" 'Add \
strSaveToFolder = Left(strSelFolderPath, Len(strSelFolderPath) - 1) & "\" 'Get Parent Folder of Message
If Not objFSO.FolderExists(strSelFolderPath) Then 'If Folder doesn't Exist, Create it
objFSO.CreateFolder(strSelFolderPath)
End If
objMailFolder = objApp.Session.GetFolderFromID(colEntryID(i), colStoreID(i)) 'Get All Subfolders in Chosen Folder
For j = 1 To objMailFolder.Items.Count 'Establish Count
objMailItem = objFolderToBackup.Items(j) 'Get Subfolder Items, ie, Emails in Folder
strSubject = objMailItem.Subject 'Get Subject
strEmailName = StripCharacters(strSubject) 'Strip Invalid Characters out of FileName
strFileName = strSaveToFolder & strEmailName & ".msg" 'Name of Message to be Saved
strFileName = Left(strFileName, 256)
objMailItem.SaveAs(strFileName, 3) 'Save
Next j
Next i
End Sub
That was a mammoth chunk of code! The functionality of this sub is very simple
actually. We first identify the folders and emails for backup via the GetOutLookFolders sub. Then, we format the message names properly. This means
that we get rid of characters that aren't allowed in filenames via the
StripCharacters function and some further string manipulation. if you are fairly
new to string manipulation, check out this
FAQ. Lastly, we save the emails. For
this we need the CustomBrowseForFolder function, which produces a BrowseForFolder
dialog.
GetOutlookFolders
The second sub looks like:
'''Get All Outlook Folders
Sub GetOutLookFolders(ByVal colAllFolders As Collection, ByVal colAllEntryIDs As Collection, ByVal colAllStoreIDs As Collection, ByVal strSelFolder As Outlook.MAPIFolder)
Dim strSubFolder As Outlook.MAPIFolder 'Get Chosen Folder
colAllFolders.Add(strSelFolder.FolderPath) 'Path
colAllEntryIDs.Add(strSelFolder.EntryID) 'Entry ID
colAllStoreIDs.Add(strSelFolder.StoreID) 'Store ID
For Each strSubFolder In strSelFolder.Folders 'Loop Through Folders
GetOutLookFolders(colAllFolders, colAllEntryIDs, colAllStoreIDs, strSelFolder) 'Get Subfolders
Next strSubFolder
strSubFolder = Nothing 'Release Object
End Sub
As I mentioned, this sub loops through our wanted folder and all its
subfolders. Now on to the Functions.
CustomBrowseForFolder
By its name, it should be pretty obvious to you that we are making a Browse
For Folder dialog, here is the code:
'''Display Browse for Folder Dialog
Function CustomBrowseForFolder(Optional ByVal strDefaultLoc As String = "C:\Documents and Settings\hannes\Desktop\To Backup") As String
Dim objShell As Object 'Shell Object
objShell = CreateObject("Shell.Application"). _
CustomBrowseForFolder(0, "Please Choose a Folder to Backup", 0, strDefaultLoc) 'Create BFF Dialog
CustomBrowseForFolder = objShell.self.Path 'Set Initial Path
objShell = Nothing 'Release Object
End Function
In order to call the Browse For Folder dialog, we need the Shell object. We
then specify its title and initial directory, that is it.
StripCharacters
This one is tricky. StripCharacters makes use of
Regular Expressions to
filter out unwanted characters. As I mentioned, Windows is very strict with
filenames, and there are certain characters, which might appear in your emails'
titles that are not allowed. This is why we need to do this. The code follows:
'''Strip Invalid Characters from Subjects
Function StripCharacters(ByVal strSubs As String) As String
Dim objRegEx As Object 'Regular Expression Object
objRegEx = CreateObject("vbscript.regexp") 'Create Regular Expression Object
'Regex Pattern to Identify Illegal Characters
objRegEx.Pattern = "[\" & Chr(34) & "\!\@\#\$\%\^\&\*\(\)\=\+\|\[\]\{\}\`\'\;\:\<\>\?\/\,]"
objRegEx.IgnoreCase = True 'Ignore Case
objRegEx.[Global] = True
StripCharacters = objRegEx.Replace(strSubs, "") 'Replace Illegal Character with Empty String
objRegEx = Nothing 'Release Reg Ex Object
End Function
The pattern is of importance here, feel free to
add more filter characters in there.
We can build our DLL now by choosing Build
Solution from Visual Studio's Build menu. There should be no errors.
If you do encounter any errors, make sure you have written all the code as it is
in this article. I am providing a working sample, in any case - if you are
really battling to get it working.
Setup & Installation
The final piece of the puzzle we need to make
this Add In work, is to create a Setup Application. The Setup is actually the
most important step. The reason why I say so is that it not only installs the
Add In, but it registers all the components for this Add In, so that it can
work, without any hassles.
Add a Setup Project to your project, by clicking
File, Add Project, New Project. Under Other Projects, select
Setup.
This will add it to your project. You should see
it in your Solution Explorer. Right click your setup project in the
Solution Explorer and choose Add, File, Primary Output and
select your project when prompted. All the necessary files to make the Add In
work, have been added. Build your Setup project. There should be no errors. If
there are, make sure you did not miss a step. I am including a working Setup
application for this Add In, in this article. After the Build was successful,
right click your Setup project again, and choose Install. This installs
your Add In.
Obviously, you can run the Setup from outside the
Visual Studio IDE as well. When you launch Microsoft Outlook, and have the
Advanced toolbar visible, you will see a button similar to the following
picture:
[SaveYourFunnies.jpg]
Figure 10: Save Your Funnies button
Click on it and select a folder to backup. Choose
a location to backup to, and click OK. Outlook will pop a box asking
you permission to save. It is the only annoyance in there, but you can set a
time limit ( for which to allow this application to run ) in this box.
Conclusion
I hope my article was useful, and that you have
enjoyed and learned something from it. Stay tuned for Part 2, which will cover
Outlook 2007 and Visual Basic 2010. Until next time, cheers!
Comments
There are no comments yet. Be the first to comment!