Link to home
Start Free TrialLog in
Avatar of andyhowse
andyhowse

asked on

programatically export emails in outlook 2003

I want to be able to create a macro or code within outlook so that I can select all the items in a folder and export them to an access file ?
Can you do this sort of programming in outlook?


Avatar of David Lee
David Lee
Flag of United States of America image

Greetings, andyhowse.

Yes.  It's simply a matter of making a connection to the database using ADO and then looping through the items in the folder and inserting their contents into the Access table.  What type of Outlook folder are you looking to export?  Do you need to see some sample code or were you just looking to see if it's possible?

Cheers!
Avatar of andyhowse
andyhowse

ASKER

Hi Bludevilfan and thanks for your reply.

I am trying to export a sub folder of inbobox on a regular basis - 4 times a day.

I would appreciate some sample code if possible.

Thanks in advance.
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
Thanks BlueDevilFan,
I was thinking of it the other way round - a script or macro to run in outlook to export the emails .... but this will be better - could you give me an idea of how I could then move the processed emails into a 'processed' folder?

I have accepted the answer as Grade A but I didn't get an option to give the points - or do you get the points automatically when I accept?

Many thanks

Andyhowse
You're welcome.  The points go to the person who posted the accepted answer.  Since you accepted my post I got the points.  

Moving the processed messages requires a bit of a change to the code.  We can't use a For Each ... Next loop to process the messages because moving the processed messages will cause the loop to skip messages.  When moving items out of the set of items being processed we have to start at the end of the list and work backwards.  

Sub ExportMessagesToAccess()
    Dim olkFolder As Outlook.Items, _
        olkMessage As Outlook.MailItem, _
        adoCon As Object, _
        strFields As String, _
        varValues As Variant, _
        intCounter As Integer
    'Add or remove fields as desired
    strFields = "Bcc,Body,BodyFormat,Categories,Cc,CreationTime,HTMLBody,Importance,SenderName,Sensitivity,Subject,SentTo"
    Set adoCon = CreateObject("ADODB.Connection")
    'Change the name and path of the database on the next line
    adoCon.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\eeTesting\Outlook.Mdb;Persist Security Info=False"
    Set olkFolder = Session.GetDefaultFolder(olFolderInbox).Items
    For intCounter = olkFolder.Count to 1 Step -1
        Set olkMessage = olkFolder.Item(intCounter)
        With olkMessage
            varValues = "'" & FixTextField(.BCC) & "'" _
                & ",'" & FixTextField(.Body) & "'" _
                & "," & .BodyFormat _
                & ",'" & FixTextField(.Categories) & "'" _
                & ",'" & FixTextField(.CC) & "'" _
                & ",'" & .CreationTime & "'" _
                & ",'" & FixTextField(.HTMLBody) & "'" _
                & "," & .Importance _
                & ",'" & FixTextField(.SenderName) & "'" _
                & "," & .Sensitivity _
                & ",'" & FixTextField(.Subject) & "'" _
                & ",'" & FixTextField(.To) & "'"
        End With
        'Change the table name "Messages" on the following line as needed
        adoCon.Execute "INSERT INTO Messages (" & strFields & ") VALUES(" & varValues & ")"
        'Replace "Some Folder Object" with a variable containing a folder object
        olkMessage.Move "Some Folder Object"
    Next
    adoCon.Close
    Set adoCon = Nothing
    Set olkMessage = Nothing
    Set olkFolder = Nothing
    MsgBox "All done!"
End Sub
Thanks you are a star!