Link to home
Start Free TrialLog in
Avatar of Ackles
AcklesFlag for Switzerland

asked on

how to export messages from Outlook to Windows shell in .msg format

Hi,
I want to export e-mail messages from Outlook 2007 to Windows folder. I DON'T want to seprate the attachments, they are fine if they keep on residing inside the message.

It would also be much helpful if it could be made as a Scheduled Task so that user does not have to interact & it becomes an Automatic Process.

I found one Software (http://www.techhit.com/messagesave/) for doing this Activity, however if someone could suggest some code or something with which I can Avoid installing a Third Party software?

Best,
A
Avatar of Ackles
Ackles
Flag of Switzerland image

ASKER

What does Neglected Question mean? Why is it Neglected?
Avatar of David Lee
Hi, Ackles.

Neglected question means that an expert hasn't responded within a set amount of time, 12 hours I think.  

I can provide a script that'll do this, I just need a few more details.  What Outlook folder do you want to export from and doyou want to export all messages or only those that meet a certain condition?  Also, what folder do you want to export to?
Avatar of Ackles

ASKER

Hi,
I basically want to export all the e-mails from Outlook irrespective weather they have attachments or not. I can use a Rule to send Copy of all Incoming & Sent e-mails to a sub-folder or any other folder you want me to have.

My only criteria is that all the messages should be thrown to Windows Shell. If this Script can also Delete the messages after throwing or exporting to Windows Shell then it would be Fabulous as there will be no chance of Duplication. As far as the folder I want to export is concerned it would be more suitable if that can be our Network folder which is a Shared Folder on server.
The code below exports the current folder and all sub-folders to the file system.  Items are saved in .msg format and the items are deleted from Outlook as they're exported.  You didn't mention exporting to sub-folders, so this version exports all messages to the same file system folder.  Follow these instructions to use this code.

1.  Start Outlook
2.  Click Tools->Macro->Visual Basic Editor
3.  If not already expanded, expand Microsoft Office Outlook Objects and click on Module1
4.  Copy the code from the Code Snippet box and paste it into the right-hand pane of Outlook's VB Editor window
5.  Edit the code as needed.  I included comments wherever something needs to or can change
6.  Click the diskette icon on the toolbar to save the changes
7.  Close the VB Editor
8.  Select a folder and run the macro.  Selecting a top-level folder (e.g. mailbox or PST file) will export everything in that mailbox or PST file.

Sub RunExportFolder()
    ExportFolder Application.ActiveExplorer.CurrentFolder
End Sub
 
Sub ExportFolder(olkFolder As Outlook.Folder)
    'Change the folder path on the following line.  This is the path to the folder all items will be saved to.'
    Const FOLDER_PATH = "C:\eeTesting\Messages\"
    Dim olkItem As Outlook.MailItem, olkSubfolder As Outlook.Folder, intIndex As Integer
    For intIndex = olkFolder.Items.Count To 1 Step -1
        Set olkItem = olkFolder.Items(intIndex)
        olkItem.SaveAs FOLDER_PATH & ReplaceIllegalCharacters(olkItem.Subject) & ".msg", olMSG
        'olkItem.Delete
    Next
    For Each olkSubfolder In olkFolder.Folders
        ExportFolder olkSubfolder
    Next
    Set olkItem = Nothing
End Sub
 
Function ReplaceIllegalCharacters(strSubject As String) As String
    Dim strBuffer As String
    strBuffer = Replace(strSubject, ":", "")
    strBuffer = Replace(strBuffer, "\", "")
    strBuffer = Replace(strBuffer, "/", "")
    strBuffer = Replace(strBuffer, "?", "")
    strBuffer = Replace(strBuffer, Chr(34), "'")
    strBuffer = Replace(strBuffer, "|", "")
    ReplaceIllegalCharacters = strBuffer
End Function

Open in new window

Oops, hit Submit a little too fast.  Uncomment (i.e. remove the apostrophe) at the beginning of line #12.
Avatar of Ackles

ASKER

THANKS SO VERY MUCH!!! (Sorry Don't know your Name).

I will Accept this as a Solution to my desired Query after I check it on Monday on my Office Computer as I don't have Windows home, however if I am not being mean, can I request one more Additional thing:

Is it possible to make it as a Scheduled Task like say once in a day so that there is no User Interaction required?

If this can be done, you have saved me a lot of Trouble.

Thanks a lot once again, best regards,
A
You're welcome.  My name is David.

No, that's not too much to ask for.  Replace the code with the version below.  Follow these instructions to use it.

1.  Open Notepad
2.  Copy the code and paste it into Notepad
3.  Edit the code per the comments in the code
4.  Save the file with a .vbs extension
5.  Use Windows built-in task scheduler to set this up as an automatic task.  The command line will be something like

    CScript.exe Scriptname.vbs


Const olFolderInbox = 6
Dim olkApp, olkSes, olkRootFolder
On Error Resume Next
Set olkApp = GetObject(,"Outlook.Application")
If Err.Number <> 0 Then
    Set olkApp = CreateObject("Outlook.Application")
    Set olkSes = olkApp.GetNameSpace("MAPI")
    'Change the profile name on the following line as needed'
    olkSes.Logon "Outlook"
End If
'Change the folder path on the following line as needed'
Set olkRootFolder = olkSes.GetDefaultFolder(olFolderInbox)
ExportFolder olkRootFolder
Set olkRootFolder = Nothing
Set olkSes = Nothing
Set olkApp = Nothing
WScript.Quit
 
Sub ExportFolder()
    'Change the folder path on the following line.  This is the path to the folder all items will be saved to.'
    Const FOLDER_PATH = "C:\eeTesting\Messages\"
    Dim olkItem, olkSubfolder, intIndex
    For intIndex = olkFolder.Items.Count To 1 Step -1
        Set olkItem = olkFolder.Items(intIndex)
        olkItem.SaveAs FOLDER_PATH & ReplaceIllegalCharacters(olkItem.Subject) & ".msg", olMSG
        olkItem.Delete
    Next
    For Each olkSubfolder In olkFolder.Folders
        ExportFolder olkSubfolder
    Next
    Set olkItem = Nothing
End Sub
 
Function ReplaceIllegalCharacters(strSubject)
    Dim strBuffer
    strBuffer = Replace(strSubject, ":", "")
    strBuffer = Replace(strBuffer, "\", "")
    strBuffer = Replace(strBuffer, "/", "")
    strBuffer = Replace(strBuffer, "?", "")
    strBuffer = Replace(strBuffer, Chr(34), "'")
    strBuffer = Replace(strBuffer, "|", "")
    ReplaceIllegalCharacters = strBuffer
End Function

Open in new window

Avatar of Ackles

ASKER

David,
I have been never so Eager in my whole life to go to Office as I am today!!!

Will do the First Thing in the Morning & will let you know the Good News.
Kind regards,
A
Avatar of Ackles

ASKER

David,
I did slight modfification to the code given by you to satisfy  my need, i understood from the code that , the mails from standard folders INBOX , SENT ITEM only can be extracted and archived in windows folder ,is there any way ,we can extract the emails from the customized folder , example <Some Name>.
Please let me know if you have any questions.

Kind Regards
A

Const olFolderInbox = 6
Const FOLDER_PATH = "C:\mess\"
    Dim olkItem, olkSubfolder, intIndex
    Dim olkApp, olkSes, olkRootFolder

On Error Resume Next
Set olkApp = GetObject(, "Outlook.Application")

If Err.Number = 0 Then
    Set olkApp = CreateObject("Outlook.Application")
    Set olkSes = olkApp.GetNamespace("MAPI")
    'Change the profile name on the following line as needed'
    olkSes.Logon "Exchange Server"
End If

'Change the folder path on the following line as needed'

Set olkRootFolder = olkSes.GetDefaultFolder(olFolderInbox)
'Set olkRootFolder = olkSes.GetDefaultFolder(2)
'MsgBox olkRootFolder
ExportFolder olkRootFolder
Set olkRootFolder = Nothing
Set olkSes = Nothing
Set olkApp = Nothing
WScript.Quit

Sub ExportFolder(olkFolder)
    'Change the folder path on the following line.  This is the path to the folder all items will be saved to.'
    Dim a
    a = olkFolder.Items.Count
   ' MsgBox a
    'MsgBox "The count is " & olkFolder.Items.Count
    For intIndex = olkFolder.Items.Count To 1 Step -1
        Set olkItem = olkFolder.Items(intIndex)
        olkItem.SaveAs FOLDER_PATH & ReplaceIllegalCharacters(olkItem.Subject) & ".msg"
        'olkItem.Delete
    Next
    For Each olkSubfolder In olkFolder.Folders
        ExportFolder olkSubfolder
    Next
    Set olkItem = Nothing
End Sub
 
Function ReplaceIllegalCharacters(strSubject)
    Dim strBuffer
    strBuffer = Replace(strSubject, ":", "")
    strBuffer = Replace(strBuffer, "\", "")
    strBuffer = Replace(strBuffer, "/", "")
    strBuffer = Replace(strBuffer, "?", "")
    strBuffer = Replace(strBuffer, Chr(34), "'")
    strBuffer = Replace(strBuffer, "|", "")
    ReplaceIllegalCharacters = strBuffer
End Function

Do you want to export one folder at a time or all of the fodlers in a given mailbox or PST file?
Avatar of Ackles

ASKER

Hey David,
I want to Only Export One folder.

See this is my Logic:

I will make a Rule in Outlook to put a Copy of every Incoming & Sent Item in a Sub-folder in Inbox e.g. Exported. (Because at the same time I want it to be deleted to avoid Duplication!!!)

This "Exported" folder is the one where Script has to do the Activity.

Best,
A
ASKER CERTIFIED SOLUTION
Avatar of David Lee
David Lee
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ackles

ASKER

David,
Thanks a lot for your Kind Help!!!

On another note, I would like to ask if the same can be done on Exchange Server Side?
Would you like me to address this as a New Question or would it be possible for you to answer in this only?
Kind regards,
A
You're welcome.  Glad I could help out.

What do you mean when you say, "if the same can be done on Exchange Server Side"?  Do you mean doing this from a process running on the Exchange server?
Avatar of Ackles

ASKER

Hey David,
Yes, I mean if the same Process can be run on Exchange Server.

Well, this is what I am thinking.... We have many Accounts & we are running Exchange Server. Now, if we could throw all the e-mails to Windows Shell right when the e-mails hit Exchange Server (without deleting them), then we don't need to run the Utility on Client Side.

I am asking as we can easily get all these e-mails then to our Document Management System for Archiving these e-mails.

Best,
A
Avatar of Ackles

ASKER

Hey David,
Any updates if this is possible?
Best,
A