Outlook Export

FREDDY67

Registered User.
Local time
Today, 08:15
Joined
Apr 23, 2007
Messages
52
I have come across the following code to export e-mails into access, the problem I have is that I wish to export the inbox & bypass the folder selection process, any help would be appreciated.

PHP:
Sub ExportMailByFolder()
  'Export specified fields from each mail
  'item in selected folder.
  Dim ns As Outlook.NameSpace
  Dim objFolder As Outlook.MAPIFolder
  Set ns = GetNamespace("MAPI")
  Set objFolder = ns.PickFolder
  Dim adoConn As ADODB.Connection
  Dim adoRS As ADODB.Recordset
  Dim intCounter As Integer
  Set adoConn = CreateObject("ADODB.Connection")
  Set adoRS = CreateObject("ADODB.Recordset")
  'DSN and target file must exist.
  adoConn.Open "DSN=OutlookData;"
  adoRS.Open "SELECT * FROM email", adoConn, _
       adOpenDynamic, adLockOptimistic
  'Cycle through selected folder.
  For intCounter = objFolder.Items.Count To 1 Step -1
   With objFolder.Items(intCounter)
   'Copy property value to corresponding fields
   'in target file.
    If .Class = olMail Then
      adoRS.AddNew
      adoRS("Subject") = .Subject
      adoRS("Body") = .Body
      adoRS("FromName") = .SenderName
      adoRS("ToName") = .To
      adoRS("FromAddress") = .SenderEmailAddress
      adoRS("FromType") = .SenderEmailType
      adoRS("CCName") = .CC
      adoRS("BCCName") = .BCC
      adoRS("Importance") = .Importance
      adoRS("Sensitivity") = .Sensitivity
      adoRS.Update
     End If
    End With
   Next
  adoRS.Close
  Set adoRS = Nothing
  Set adoConn = Nothing
  Set ns = Nothing
  Set objFolder = Nothing
End Sub


Thanks

Freddy
 
Basically there is not native way in outlook to get a folder by name as far as I know. The way I do not is to go through the folders and check for the name once you have a collections of folders.

For example my outlook has the following and my INBOX is in the second level.


Code:
'add all your declarations
Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")


For Each objFolder In objNS.Folders
    
    For Each MyFolder In objFolder.Folders
    
    If MyFolder.Name = "Inbox" Then
        
        'myfolder now holds your object folder for your inbox
        
    End If
    
    
    Next MyFolder
    
    
    Next objFolder
 
Darbid

Many thanks for your reply, I apologize for my ignorance but I can't figure out where your code fits into the original & what gets deleted from the original?

Sorry I'm a relative novice.

Cheers

Freddy
 
Ok but I am not far behind you, I am learning too.

See this line "Set objFolder = ns.PickFolder"

This brings up a window to choose a folder. So this we want to replace

but our goal as far as I understand you is to get objFolder to be the
Inbox and then you will go through each item in your inbox.

The next thing to worry about is the structure of your folders or if this
is for other people their structure. in the examplt I gave you i pretty much
gave you a hack. My folders are "Personal Folders" and then within that is my INBOX.

first I think we will need another object like

Code:
Dim oFolder as Outlook.folder

so with the above not there replace that with this (i cannot test this right now but if you step through it you will see if it is exiting the FORS. I am not exactly sure how to exit the second for properly but I think this will work.

Code:
For Each oFolder In objNS.Folders
                                                                                                                                                                                                                                            
    For Each objFolder In oFolder.Folders
                                                                                                                                                                                                                                                
        If objFolder.Name = "Inbox" Then
            Set oFolder = Not hing
            Exit For
        End If
        
    Next objFolder
                                                                                                                                                                                                                                                
    If oFolder Is Nothing Then
        Exit For
    End If
                                                                                                                                                                                                                                            
Next oFolder



 
Darbid

Thanks for your guidance, I spent a while messing around with the code but just kept getting more & more frustrated. So I have been trawling the net looking for outlook help, & on "http://msdn.microsoft.com/en-us/library/aa220100(office.11).aspx" I came across the GetDefaultFolder Method, I then replaced the line you pointed out:

"Set objFolder = ns.PickFolder"

With

"Set objFolder = ns.GetDefaultFolder(olFolderInbox)"

which works perfectly for my needs.


Once again thanks for your help with the above.

Freddy
 
ahhh sorry for giving you a bum steer. When I was looking at your problem I read something different. Maybe what I said applies to non-standard folders but to the inbox it does not.

Anyway sorry again.
 

Users who are viewing this thread

Back
Top Bottom