Outlook: Export all mail into Explorer

This guide uses a VB script copied from https://techniclee.wordpress.com/2013/05/09/export-outlook-folders-to-the-file-system/ as it’s basis.

The script here only copies emails from Outlook with no option for deleting the emails, for another layer of safety the deletion is left to the user to complete manually.

The script will copy all emails from a selected folder, including subfolders, into Windows and retain each email’s attachments, sender and recipient addresses along with the timestamp information relating to the email (not the creation date within Windows).

I’ve copied the edited script below for reference, thanks to the Keble IT Manager and a few of my own edits too I have a slightly improved version which handles errors better, this is available at the end of this article.

The process for importing this script to users is manual, thankfully only a handful of people require it so it’s not a huge overhead. The deployment process is:

From within Outlook:

  1. Click [File] > [Options] > [Customize Ribbon] and [TICK] the [Developer Ribbon] check box, click [OK] to all open menus to close them
  2. Click the [Developer] ribbon menu item > [Visual Basic]
  3. [Insert] > [Module]
  4. Copy and paste the script below into the window
  5. Click the [Save] button and close the [Visual Basic Window] to return to Outlook
  6. Click [File] > [Options] > [Customize Ribbon]
  7. [UN-TICK] the [Developer Ribbon]
  8. Click the [New Tab] button, highlight the [New Tab (Custom)] entry in the [Main Tabs] list > click the [Rename] button > Name the tab “Email Export”, click [OK] to save
  9. Highlight the [New Group (Custom)] entry in the [Main Tabs] list >  click the [Rename] button >Name the group “Export Highlighted Folder to Windows” > Select an appropriate icon
  10. In the left column, from the [Choose commands from:] drop-down menu select [Macros]
  11. Click and drag the [Project1.CopyOutlookFoldertoFileSystem] entry onto the newly created new group named “Export Highlighted Folder to Windows”
  12. Highlight the [Project1.CopyOutlookFoldertoFileSystem] entry in the [Main Tabs] list >  click the [Rename] button >Name the command “Export”.
  13. Click [OK] to close all open menus and return to Outlook

This creates a new tab with the script loaded into a single command named “Export” on the Outlook ribbon. The script doesn’t *ever* delete mail from Outlook, it only ever copies. To use the script:

  1. Highlight a folder on the Outlook navigation pane that you want to copy to Windows
  2. Click the new “Export” button
  3. Select the Windows folder you want to export to
  4. Click [OK] and wait for the export to complete, it may take a few minutes for 1GB+ folders
  5. Once the transfer window disappears the copy is complete, inspect the folder in Windows to ensure the emails are there
  6. Having double checked the export worked you can down delete the source folder from Outlook or all the emails inside it.

 

'On the next line edit the starting folder as desired. If you leave it blank, then the starting folder will be the local computer.
Const STARTING_FOLDER = ""
 
Dim objFSO As Object
 
Sub CopyOutlookFolderToFileSystem()
 ExportController "Copy"
End Sub
 
'Sub MoveOutlookFolderToFileSystem()
' ExportController "Move"
'End Sub
 
Sub ExportController(strAction As String)
 Dim olkFld As Outlook.MAPIFolder, strPath As String, dest As String
 strPath = SelectFolder(STARTING_FOLDER)
 If strPath = "" Then
 MsgBox "You did not select a folder. Export cancelled.", vbInformation + vbOKOnly, "Export Outlook Folder"
 Else
 Set objFSO = CreateObject("Scripting.FileSystemObject")
 Set olkFld = Application.ActiveExplorer.CurrentFolder
 ExportOutlookFolder olkFld, strPath
 If LCase(strAction) = "move" Then
 olkFld.Delete
 Else
 dest = strPath & "\" & olkFld.Name
 MsgBox "Now check that the right number of messages and folders are in " & dest & ", then delete the folder from Outlook"
 End If
 End If
 Set olkFld = Nothing
 Set objFSO = Nothing
End Sub
 
Sub ExportOutlookFolder(ByVal olkFld As Outlook.MAPIFolder, strStartingPath As String)
 Dim olkSub As Outlook.MAPIFolder, olkItm As Object, strPath As String, strMyPath As String, strSubject As String, intCount As Integer, s As String, d As Date, f As String, itemlist As Outlook.items
 
 On Error Resume Next
 ' Set an initial date just in case first item is broken
 d = Date
 strPath = strStartingPath & "\" & olkFld.Name
 objFSO.CreateFolder strPath
 ' Folder exists
 If Err.Number = 58 Then
 Dim rc As Integer
 rc = MsgBox("The folder " & strPath & " already exists, proceed anyway?", vbOKCancel)
 If rc = 1 Then
 Err.Clear
 Else
 Exit Sub
 End If
 End If
 
 Set itemlist = olkFld.items
 itemlist.Sort "[SentOn]", False
 
 For Each olkItm In itemlist
 ' Start with clean slate
 Err.Clear
 s = RemoveIllegalCharacters(olkItm.Subject)
 If Err.Number <> 0 Then
 s = "[Error in subject]"
 Err.Clear
 End If
 
 f = RemoveIllegalCharacters(olkItm.SenderName)
 If Err.Number <> 0 Then
 f = "[Error in sender]"
 Err.Clear
 End If
 ' This may fail in very rare circumstances. It'll use the date of the previous item as we've not reset it. That's probably close enough as the items are sorted into ascending date order.
 d = olkItm.SentOn
 
 strSubject = "[From] " & f & " [Subject] " & s
 strFilename = strSubject & ".msg"
 intCount = 0
 Do While True
 strMyPath = strPath & "\" & strFilename
 If objFSO.FileExists(strMyPath) Then
 intCount = intCount + 1
 strFilename = strSubject & " (" & intCount & ").msg"
 Else
 Exit Do
 End If
 Loop
 olkItm.SaveAs strMyPath, olMSG
 ChangeTimeStamp strMyPath, d
 Next
 For Each olkSub In olkFld.Folders
 ExportOutlookFolder olkSub, strPath
 Next
 Set olkFld = Nothing
 Set olkItm = Nothing
End Sub
 
Function SelectFolder(varStartingFolder As Variant) As String
 ' This function is a modified version of the SelectFolder function written by Rob van der Woude (http://www.robvanderwoude.com/vbstech_ui_selectfolder.php)
 
 ' Standard housekeeping
 Dim objFolder As Object, objShell As Object
 
 ' Custom error handling
 On Error Resume Next
 
 ' Create a dialog object
 Set objShell = CreateObject("Shell.Application")
 Set objFolder = objShell.BrowseForFolder(0, "Select the folder you want to export to", 0, varStartingFolder)
 
 ' Return the path of the selected folder
 If TypeName(objFolder) <> "Nothing" Then SelectFolder = objFolder.self.Path
 
 ' Standard housekeeping
 Set objFolder = Nothing
 Set objShell = Nothing
 On Error GoTo 0
End Function
 
Function RemoveIllegalCharacters(strValue As String) As String
 ' Purpose: Remove characters that cannot be in a filename from a string.'
 ' Written: 4/24/2009'
 ' Author: BlueDevilFan'
 ' Outlook: All versions'
 RemoveIllegalCharacters = strValue
 ' RemoveIllegalCharacters = LCase(RemoveIllegalCharacters)
 RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, "<", "(")
 RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, ">", ")")
 RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, ":", "-")
 RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, Chr(34), "'")
 RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, "/", "")
 RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, "\", "")
 RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, "|", "")
 RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, "?", "")
 RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, "*", "")
 RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, "+", "")
 RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, "@", "_at_")
 RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, Chr(9), "")
 RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, "û", "u")
 RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, "ü", "u")
 RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, "à", "a")
 RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, "ç", "c")
 RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, "é", "e")
 RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, "è", "e")
 RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, "ê", "e")
End Function
 
Sub ChangeTimeStamp(strFile As String, datStamp As Date)
 Dim objShell As Object, objFolder As Object, objFolderItem As Object, varPath As Variant, varName As Variant
 varName = Mid(strFile, InStrRev(strFile, "\") + 1)
 varPath = Mid(strFile, 1, InStrRev(strFile, "\"))
 Set objShell = CreateObject("Shell.Application")
 Set objFolder = objShell.NameSpace(varPath)
 Set objFolderItem = objFolder.ParseName(varName)
 objFolderItem.ModifyDate = CStr(datStamp)
 Set objShell = Nothing
 Set objFolder = Nothing
 Set objFolderItem = Nothing
End Sub

courses.edx.org: PDF viewing, editing

Modifying PDFs with pdftk

At times, you may want to merge, split, or rotate PDF files; not all of these operations can be achieved while using a PDF viewer.  A great way to do this is to use the “PDF Toolkit”, pdftk, to perform a very large variety of sophisticated tasks. Some of these operations include:

  • Merging/Splitting/Rotating PDF documents
  • Repairing corrupted PDF pages
  • Pulling single pages from a file
  • Encrypting and decrypting PDF files
  • Adding, updating, and exporting a PDF’s metadata
  • Exporting bookmarks to a text file
  • Filling out PDF forms

In short, there’s very little pdftk cannot do when it comes to working with PDF files; it is indeed the Swiss Army knife of PDF tools.

Using pdftk

You can accomplish a wide variety of tasks using pdftk including:

Command Usage
pdftk 1.pdf 2.pdf cat output 12.pdf Merge the two documents 1.pdf and 2.pdf. The output will be saved to 12.pdf.
pdftk A=1.pdf cat A1-2 output new.pdf Write only pages 1 and 2 of 1.pdf. The output will be saved to new.pdf.
pdftk A=1.pdf cat A1-endright output new.pdf Rotate all pages of 1.pdf 90 degrees clockwise and save result in new.pdf.

Encrypting PDF Files

If you’re working with PDF files that contain confidential information and you want to ensure that only certain people can view the PDF file, you can apply a password to it using the user_pw option. One can do this by issuing a command such as:

$ pdftk public.pdf output private.pdf user_pw PROMPT

When you run this command, you will receive a prompt to set the required password, which can have a maximum of 32 characters. A new file, private.pdf, will be created with the identical content as public.pdf, but anyone will need to type the password to be able to view it.

 

Using Additional Tools

You can use other tools, such as pdfinfo, flpsed, and pdfmod to work with PDF files.

pdfinfo can extract information about PDF files, especially when the files are very large or when a graphical interface is not available.

flpsed can add data to a PostScript document. This tool is specifically useful for filling in forms or adding short comments into the document.

pdfmod is a simple application that provides a graphical interface for modifying PDF documents. Using this tool, you can reorder, rotate, and remove pages; export images from a document; edit the title, subject, and author; add keywords; and combine documents using drag-and-drop action.

For example, to collect the details of a document, you can use the following command:
$ pdfinfo /usr/share/doc/readme.pdf

 

Converting Between PostScript and PDF

Most users today are far more accustomed to working with files in PDF format, viewing them easily either on the Internet through their browser or locally on their machine. The PostScript format is still important for various technical reasons that the general user will rarely have to deal with.

From time to time you may need to convert files from one format to the other, and there are very simple utilities for accomplishing that task. ps2pdf and pdf2ps are part of the ghostscript package installed on or available on all Linux distributions. As an alternative, there are pstopdf and pdftops which are usually part of the poppler package which may need to be added through your package manager. Unless you are doing a lot of conversions or need some of the fancier options (which you can read about in the man pages for these utilities) it really doesn’t matter which ones you use.

Some usage examples:

Command Usage
pdf2ps file.pdf Converts file.pdf to file.ps
ps2pdf file.ps Convertsfile.ps to file.pdf
pstopdf input.ps output.pdf Converts input.ps to output.pdf
pdftops input.pdf output.pdf Converts input.pdf to output.ps

 

courses.edx.org: Linux Printing with lp

 

Using lp

lp and lpr accept command line options that help you perform all operations that the GUI can accomplish. lp is typically used with a file name as an argument.

Some  lp commands and other printing utilities you can use are listed in the table.

Command Usage
lp <filename> To print the file to default printer
lp -d printer <filename> To print to a specific printer (useful if multiple printers are available)
program | lp
echo string | lp
To print the output of a program
lp -n number <filename> To print multiple copies
lpoptions -d printer To set the default printer
lpq -a To show the queue status
lpadmin To configure printer queues

The lpoptions utility can be used to set printer options and defaults. Each printer has a set of tags associated with it, such as the default number of copies and authentication requirements. You can execute the command lpoptions help to obtain a list of supported options. lpoptions can also be used to set system-wide values, such as the default printer.

 

 

Command Usage
lpstat -p -d To get a list of available printers, along with their status
lpstat -a To check the status of all connected printers, including job numbers
cancel job-id
OR
lprm job-id
To cancel a print job
lpmove job-id newprinter To move a print job to new printer

 

Working with enscript

enscript is a tool that is used to convert a text file to PostScript and other formats. It also supports Rich Text Format (RTF) and HyperText Markup Language (HTML). For example, you can convert a text file to two column (-2) formatted PostScript using the command: enscript -2 -r -p psfile.ps textfile.txt. This command will also rotate (-r) the output to print so the width of the paper is greater than the height (aka landscape mode) thereby reducing the number of pages required for printing.

The commands that can be used with enscript are listed in the table below (for a file called ‘textfile.txt’).

Command Usage
enscript -p psfile.ps textfile.txt Convert a text file to PostScript (saved to psfile.ps)
enscript -n -p psfile.ps textfile.txt Convert a text file to n columns where n=1-9 (saved in psfile.ps)
enscript textfile.txt Print a text file directly to the default printer