Page 1 of 2 12 LastLast
Results 1 to 40 of 54

Thread: Import E-Mails from Outlook to Access, and read them from Access?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2002
    Posts
    665

    Import E-Mails from Outlook to Access, and read them from Access?

    How do I import E-Mails from specified folder in Outlook to Access. I want to read the imported mails from Access also?
    How do I do that?

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2002
    Posts
    665
    Someone...?

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Try a search first. This has been asked before.
    Here is a thread I answered on this subject.
    It will also show you how to connect to Outlook using ADO.
    Modify the code to connect to the Inbox instead of the Contacts.

    Outlook
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2002
    Posts
    665
    Thanks RobDog888,

    But how can I modify the code to connect to the Inbox instead of the Contacts? I have tried, without luck.

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    This should do it for you. I changed the code to use the Outlook
    Object Model to read the emails and ADO to export them to
    Access.
    Add references to Microsoft ActiveX Data Objects 2.x Library
    and Microsoft Outlook x.x Object Library

    VB Code:
    1. Public Function Outlook_Contacts_2_Access()
    2.  
    3.     On Error GoTo No_Bugs
    4.  
    5.     Dim CnnA As ADODB.Connection
    6.     Dim goRs As ADODB.Recordset
    7.    
    8.     Dim oApp As Outlook.Application
    9.     Dim oNS As Outlook.NameSpace
    10.     Dim oInbox As Outlook.MAPIFolder
    11.     Dim oEmail As Outlook.MailItem
    12.  
    13.     Dim i As Integer
    14.    
    15.     Set oApp = New Outlook.Application
    16.     Set oNS = oApp.GetNamespace("MAPI")
    17.     Set oInbox = oNS.GetDefaultFolder(olFolderInbox)
    18.    
    19.     Dim sSQL As String
    20.    
    21.     Set CnnA = New ADODB.Connection
    22.     Set goRs = New ADODB.Recordset
    23.     'YOU WILL NEED TO CREATE YOUR TABLE IN ACCESS TO MATCH AND CALL IT INBOX
    24.     sSQL = "SELECT * FROM [Inbox] WHERE 1=2;"
    25.     goRs.Open sSQL, CnnA, adOpenKeyset, adLockOptimistic, adCmdText
    26.    
    27.     frmMain.prbProgress.Max = oInbox.Items.Count
    28.     i = 1
    29.     Do While i <= oInbox.Items.Count
    30.         Set oEmail = oInbox.Items(i)
    31.         DoEvents
    32.         goRs.AddNew
    33.         goRs!To = oEmail.To
    34.         goRs!CC = oEmail.CC
    35.         goRs!BCC = oEmail.BCC
    36.         goRs!Subject = oEmail.Subject
    37.         goRs!Body = oEmail.Body           'PLAIN TEXT BODY NOTES OR
    38.         goRs!HTMLBody = oEmail.HTMLBody   'HTML BODY NOTES (DEPENDS UPON THE EMAIL)
    39.         goRs!Importance = oEmail.Importance
    40.         goRs!Received = oEmail.ReceivedTime
    41.         goRs!MessageClass = oEmail.MessageClass 'EMAIL, MEETING RESPONSE, MEETING REQUEST, ETC.
    42.         goRs!ReceivedByName = oEmail.ReceivedByName
    43.         'CONTINUE ON WITH OTHER FIELDS YOU WANT
    44.         '...
    45.         goRs.Update
    46.         Set oEmail = Nothing
    47.         frmMain.prbProgress.Value = i
    48.         i = i + 1
    49.     Loop
    50.     Set oEmail = Nothing
    51.     Set oInbox = Nothing
    52.     Set oNS = Nothing
    53.     goRs.Close
    54.     Set CnnA = Nothing
    55.     Set goRs = Nothing
    56.     Exit Function
    57.  
    58. No_Bugs:
    59.    
    60.     MsgBox Err.Number & "-" & Err.Description, vbCritical, "Outlook Email Export"
    61. '    Resume Next
    62.  
    63. End Function
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2002
    Posts
    665
    Thanks again RobDog888!

    It works to import now.
    But I have two more questions for you, that I'm sure you have the answer to.

    If I want to read the mails from Access, how can I do that in a good way?

    If a mail have an attachment, how can I import that to access?

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    To answer your second question,
    VB Code:
    1. 'UPDATED CODE...
    2. Public Function Outlook_Contacts_2_Access()
    3. '   <GORS = ACCESS>
    4.     On Error GoTo No_Bugs
    5.  
    6.     Dim CnnA As ADODB.Connection
    7.     Dim goRs As ADODB.Recordset
    8.    
    9.     Dim oApp As Outlook.Application
    10.     Dim oNS As Outlook.NameSpace
    11.     Dim oInbox As Outlook.MAPIFolder
    12.     Dim oEmail As Outlook.MailItem
    13.  
    14.     Dim i As Integer
    15.     Dim ii As Integer 'ADDED
    16.    
    17.     Dim sAttachment As String 'ADDED
    18.    
    19.     Set oApp = New Outlook.Application
    20.     Set oNS = oApp.GetNamespace("MAPI")
    21.     Set oInbox = oNS.GetDefaultFolder(olFolderInbox)
    22.    
    23.     Dim sSQL As String
    24.    
    25.     Set CnnA = New ADODB.Connection
    26.     Set goRs = New ADODB.Recordset
    27.    
    28.     sSQL = "SELECT * FROM [Inbox] WHERE 1=2;"
    29.     goRs.Open sSQL, CnnA, adOpenKeyset, adLockOptimistic, adCmdText
    30.    
    31.     frmMain.prbProgress.Max = oInbox.Items.Count
    32.     i = 1
    33.     Do While i <= oInbox.Items.Count
    34.         Set oEmail = oInbox.Items(i)
    35.         DoEvents
    36.         goRs.AddNew
    37.         goRs!To = oEmail.To
    38.         goRs!CC = oEmail.CC
    39.         goRs!BCC = oEmail.BCC
    40.         goRs!Subject = oEmail.Subject
    41.         goRs!Body = oEmail.Body           'PLAIN TEXT BODY NOTES
    42.         goRs!HTMLBody = oEmail.HTMLBody   'HTML BODY NOTES
    43.         goRs!Importance = oEmail.Importance
    44.         goRs!Received = oEmail.ReceivedTime
    45.         goRs!MessageClass = oEmail.MessageClass 'EMAIL, MEETING RESPONSE, MEETING REQUEST, ETC.
    46.         goRs!ReceivedByName = oEmail.ReceivedByName
    47.         'ADDED
    48.         If oEmail.Attachments.Count > 1 Then
    49.             'LOOP THROUGH ALL ATTACHMENTS SAVING TO DIRECTORY AND ENTERING FILENAME INTO FIELD
    50.             For ii = 1 To oEmail.Attachments.Count
    51.                 oEmail.Attachments.Item(ii).SaveAsFile "C:\MyOutlookEmailExport\Attachments\" & oEmail.Attachments.Item(ii).FileName
    52.                 sAttachment = sAttachment & "C:\MyOutlookEmailExport\Attachments\" & oEmail.Attachments.Item(ii).FileName & vbNewLine
    53.             Next
    54.             goRs!Attachment = sAttachment
    55.         Else
    56.             goRs!Attachment = "None"
    57.         End If
    58.         '/ADDED
    59.         'CONTINUE ON WITH OTHER FIELD YOU WANT
    60.         '...
    61.         goRs.Update
    62.         Set oEmail = Nothing
    63.         frmMain.prbProgress.Value = i
    64.         i = i + 1
    65.     Loop
    66.     Set oEmail = Nothing
    67.     Set oInbox = Nothing
    68.     Set oNS = Nothing
    69.     goRs.Close
    70.     Set CnnA = Nothing
    71.     Set goRs = Nothing
    72.     Exit Function
    73.  
    74. No_Bugs:
    75.    
    76.     MsgBox Err.Number & "-" & Err.Description, vbCritical, "Outlook Email Export"
    77. '    Resume Next
    78.  
    79. End Function
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    To answer your first question,

    Easy way...
    If you want to be able to just read the information, write a query
    to bring up the email message you want and display the results
    in a table view or report. Quick and easy although maybe ugly.

    Hard way...
    Shell out Outlook, creating an email and populating the Outlook
    fields with the record data from Access. May create a duplicate if
    the user saves the email.

    Hardest and slowest way...
    Create an instance of Outlook and "Find" the email message
    based upon the Access info and "Display" it in an actual Outlook
    enviroment.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2002
    Posts
    665
    RobDog888, you are my hero!

    Now it almost works as I want. After I modified some rows, it became as I wanted.
    VB Code:
    1. If oEmail.Attachments.Count > 0 Then
    2.         For ii = 1 To oEmail.Attachments.Count
    3.                 oEmail.Attachments.Item(ii).SaveAsFile "C:\MyOutlookEmailExport\Attachments\" & oEmail.Attachments.Item(ii).FileName
    4.                 sAttachment = C:\MyOutlookEmailExport\Attachments\" & oEmail.Attachments.Item(ii).FileName & vbNewLine
    5.             Next
    6.             goRs!Attachment = sAttachment
    7.         Else
    8.             goRs!Attachment = "None"
    9.         End If

    Some final questions...

    If I run same function it dublicate the rows in the table. Can I make it add only new messages from the specified folder in the table?

    If I want to create an instance of Outlook and "Find" the email message based upon the Access info and "Display" it in an actual Outlook enviroment, do you have any example on that also?

  10. #10
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    The line of code...
    VB Code:
    1. sAttachment = sAttachment & "C:\MyOutlookEmailExport\Attachments\" & oEmail.Attachments.Item(ii).FileName & vbNewLine
    Needs to be put back because with the way you
    changed it if you have more than one attachment it will only
    retain the last one.

    You can have it add only new messages you will need to filter out the messages by .EntryID property.
    To start...
    VB Code:
    1. goRs!EntryID = oEmail.EntryID
    This is a unique 140 alpha-numeric string identifing the email in Outlook.
    Then you will need to perform a query on the Access db the next
    time you export to check for a matching .EntryID. If found, then
    skip. If not found then add it to the table.

    Hold on for a sample for the Find in Outlook.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  11. #11
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Here is a working example for your first question. I had to make
    some changes so I posted the entire code so it will be easier to
    see where the changes are.
    VB Code:
    1. Option Explicit
    2. 'MODULAR DECLARATIONS
    3. Private oApp As Outlook.Application
    4. Private oNS As Outlook.NameSpace
    5. Private oInbox As Outlook.MAPIFolder
    6. Private CnnA As ADODB.Connection
    7.  
    8. Private Sub Form_Load()
    9.     'MOVED INITIALIZATION OF OUTLOOK AND CONNECTION TO
    10.     'ACCESS SO IT WILL BE AVAILABLE TO THE NEW FUNCTION
    11.     'WITHOUT HAVING TO RECREATE IT.
    12.     Set oApp = New Outlook.Application
    13.     Set oNS = oApp.GetNamespace("MAPI")
    14.     Set oInbox = oNS.GetDefaultFolder(olFolderInbox)
    15.    
    16.     Set CnnA = New ADODB.Connection
    17.     CnnA.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & frmMain.txtDBPath & ";Persist Security Info=False"
    18.     CnnA.Open
    19.    
    20. End Sub
    21.  
    22. Private Function Outlook_Contacts_2_Access()
    23. '   <GORS = ACCESS>
    24.     On Error GoTo No_Bugs
    25.  
    26.     Dim goRs As ADODB.Recordset
    27.     Dim oEmail As Outlook.MailItem
    28.  
    29.     Dim i As Integer
    30.     Dim ii As Integer
    31.    
    32.     Dim sAttachment As String
    33.    
    34.     Dim sSQL As String
    35.    
    36.     Set goRs = New ADODB.Recordset
    37.    
    38.     sSQL = "SELECT * FROM [Inbox] WHERE 1=2;"
    39.     goRs.Open sSQL, CnnA, adOpenKeyset, adLockOptimistic, adCmdText
    40.    
    41.     frmMain.prbProgress.Max = oInbox.Items.Count
    42.     i = 1
    43.     Do While i <= oInbox.Items.Count
    44.         Set oEmail = oInbox.Items(i)
    45.         DoEvents
    46.         'TEST FOR EMAIL IN ACCESS
    47.         If FindOutlookEmail(oEmail.EntryID) = False Then
    48.             goRs.AddNew
    49.             goRs!To = oEmail.To
    50.             goRs!CC = oEmail.CC
    51.             goRs!BCC = oEmail.BCC
    52.             goRs!Subject = oEmail.Subject
    53.             goRs!Body = oEmail.Body           'PLAIN TEXT BODY NOTES
    54.             goRs!HTMLBody = oEmail.HTMLBody   'HTML BODY NOTES
    55.             goRs!Importance = oEmail.Importance
    56.             goRs!Received = oEmail.ReceivedTime
    57.             goRs!MessageClass = oEmail.MessageClass 'EMAIL, MEETING RESPONSE, MEETING REQUEST, ETC.
    58.             goRs!ReceivedByName = oEmail.ReceivedByName
    59.             If oEmail.Attachments.Count > 1 Then
    60.                 'LOOP THROUGH ALL ATTACHMENTS SAVING TO DIRECTORY AND ENTERING FILENAME INTO FIELD
    61.                 For ii = 1 To oEmail.Attachments.Count
    62.                     oEmail.Attachments.Item(ii).SaveAsFile "C:\MyOutlookEmailExport\Attachments\" & oEmail.Attachments.Item(ii).FileName
    63.                     sAttachment = sAttachment & "C:\MyOutlookEmailExport\Attachments\" & oEmail.Attachments.Item(ii).FileName & vbNewLine
    64.                 Next
    65.                 goRs!Attachment = sAttachment
    66.             Else
    67.                 goRs!Attachment = "None"
    68.             End If
    69.             'CONTINUE ON WITH OTHER FIELD YOU WANT
    70.             '...
    71.             goRs.Update
    72.         End If
    73.         Set oEmail = Nothing
    74.         frmMain.prbProgress.Value = i
    75.         i = i + 1
    76.     Loop
    77.     Set oEmail = Nothing
    78.     Set oInbox = Nothing
    79.     Set oNS = Nothing
    80.     goRs.Close
    81.     Set CnnA = Nothing
    82.     Set goRs = Nothing
    83.     Exit Function
    84.  
    85. No_Bugs:
    86.    
    87.     MsgBox Err.Number & "-" & Err.Description, vbCritical, "Outlook Email Export"
    88. '    Resume Next
    89.  
    90. End Function
    91.  
    92. Private Function FindOutlookEmail(ByVal oEmailEntryID As String) As Boolean
    93.  
    94.     Dim oRsAccessEmail As ADODB.Recordset 'EMAIL IN ACCESS TO CHECK AGAINST
    95.    
    96.     Dim i As Integer
    97.    
    98.     Set oRsAccessEmail = New ADODB.Recordset
    99.     oRsAccessEmail.Open "SELECT EntryID FROM Inbox WHERE EntryID = '" & oEmailEntryID & "';", CnnA, adOpenKeyset, adLockReadOnly, adCmdText
    100.     If oRsAccessEmail.BOF = True And oRsAccessEmail.EOF = True Then
    101.         FindOutlookEmail = False
    102.     Else
    103.         FindOutlookEmail = True
    104.     End If
    105.     Set oRsAccessEmail = Nothing
    106.  
    107. End Function
    Although this will only add new emails, it
    will not catch any changes to emails theat already have been
    exported.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  12. #12
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Answer to second question...
    VB Code:
    1. Private Function OpenOutlookEmail(ByVal oEmailEnrtyID As String)
    2.    
    3.     Dim oDisplayEmail As Outlook.MailItem
    4.    
    5.     Set oDisplayEmail = oInbox.Items.Find("[EntryID] = '" & oEmailEntryID & "'")
    6.     If TypeName(oDisplayEmail) <> "Nothing" Then
    7.         oDisplayEmail.Display 'THE USE CAN MAKE CHANGES AND SAVE THEM TO OUTLOOK
    8.     Else
    9.         MsgBox "Email not found in Outlook!", vbOKOnly + vbExclamation
    10.     End If
    11.  
    12. End Function
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2002
    Posts
    665
    This is like a dream, everything works!

    But I get a little error on function OpenOutlookEmail. Error 91...Do you know why?

    If this is a public database, everybody can't read the mail with function OpenOutlookEmail then, or am I wrong?

  14. #14
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Step throught the code by pressing F8 to execute each line of
    code and see exactly where you are getting the error.

    If the database is a public database then each use will not be
    able to "Display" in Outlook the email messages that are not in
    their own "Inbox in Outlook". Its Outlook security.

    One way around this may be to export the emails to Access and
    create a duplicate email message in Outlook in a shared
    or "Public Inbox". Then every use will be able to view the email
    messages from everyone.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  15. #15
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    The only possibility I can see if the oInbox object may not be set.
    Although this is being set at the Form_Load event.

    Be back in 30 mins. Going to lunch.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  16. #16

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2002
    Posts
    665
    Hi again RobDog888,

    I have a little problem when the mail is a MEETING RESPONSE type. I get error: Type mismatch. Error 13.

    And it stops on row:
    Set oEmail = oInbox.Items(i)

    And then if the mail has a picture in it self, I get error:
    Outlook cannot do this action on this type of attachment.

    Here I want to import the mail, but I want to skip the picture that is inside the mail. The mailtype is Rich Text.

    Can you still help me?
    Last edited by Pirre001; Oct 14th, 2003 at 05:17 AM.

  17. #17
    Lively Member binduau's Avatar
    Join Date
    Sep 2003
    Location
    Perth Australia
    Posts
    121

    Thumbs up this is fantastic code robdog

    Awesome!!




    bindu

  18. #18
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Thanks, binduau.

    Pirre001,
    I am working on a solution for the attachments right now.
    You only want to save the attachments that are of olByValue type, correct?

    The types are as follows...
    olByReference = linked attachment.
    olByValue = file attachment.
    olEmbeddeditem = something like a picture embedded in the body notes.
    olOLE = the type like if you were to view a Journal entry.

    The 91 error is fixed.

    I will post it in a few.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  19. #19
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    This is what I have. I created three objects to handel Email messages, Meeting Items, and Distribution List Items.

    These are the three most common types of Outlook Items in the Inbox.
    If the Outlook Inbox item is not of these three types then it will
    skip it.It may be a task or some other item.
    VB Code:
    1. Option Explicit
    2.  
    3. Private oApp As Outlook.Application
    4. Private oNS As Outlook.NameSpace
    5. Private oInbox As Outlook.MAPIFolder
    6. Private CnnA As ADODB.Connection
    7.  
    8. Private Sub cmdExportEmails_Click()
    9.  
    10.     Call Outlook_Emails_2_Access
    11.  
    12. End Sub
    13.  
    14. Private Sub Form_Load()
    15.  
    16.     Set oApp = New Outlook.Application
    17.     Set oNS = oApp.GetNamespace("MAPI")
    18.     Set oInbox = oNS.GetDefaultFolder(olFolderInbox)
    19.    
    20.     Set CnnA = New ADODB.Connection
    21.     CnnA.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & txtDBPath & ";Persist Security Info=False"
    22.     CnnA.Open
    23.    
    24. End Sub
    25.  
    26. Private Function Outlook_Emails_2_Access()
    27. '   <GORS = ACCESS>
    28.     On Error GoTo No_Bugs
    29.  
    30.     Dim goRs As ADODB.Recordset
    31.    
    32.     Dim oEmail As Outlook.MailItem
    33.     Dim oMeetingType As Outlook.MeetingItem
    34.     Dim oDistributionList As Outlook.DistListItem
    35.    
    36.     Dim vType As Variant
    37.    
    38.     Dim i As Integer
    39.     Dim ii As Integer
    40.    
    41.     Dim sAttachment As String
    42.    
    43.     Dim sSQL As String
    44.    
    45.     Set goRs = New ADODB.Recordset
    46.    
    47.     sSQL = "SELECT * FROM [Inbox] WHERE 1=2;"
    48.     goRs.Open sSQL, CnnA, adOpenKeyset, adLockOptimistic, adCmdText
    49.    
    50.     prbProgress.Max = oInbox.Items.Count
    51.     i = 1
    52.     Do While i <= oInbox.Items.Count
    53.         DoEvents
    54.         Select Case oInbox.Items(i).Class
    55.             Case olMail
    56.                 vType = "Email"
    57.             Case olMeetingRequest
    58.                 vType = "MeetingItem"
    59.             Case olMeetingResponseNegative
    60.                 vType = "MeetingItem"
    61.             Case olMeetingResponsePositive
    62.                 vType = "MeetingItem"
    63.             Case olMeetingResponseTentative
    64.                 vType = "MeetingItem"
    65.             Case olDistributionList
    66.                 vType = "DistListItem"
    67.         End Select
    68.         If vType = "Email" Then 'ONLY EMAIL TYPES
    69.             Set oEmail = oInbox.Items(i)
    70.             If FindOutlookEmail(oEmail.EntryID) = False Then
    71.                 goRs.AddNew
    72.                 goRs!To = oEmail.To
    73.                 goRs!CC = oEmail.CC
    74.                 goRs!BCC = oEmail.BCC
    75.                 goRs!Subject = oEmail.Subject
    76.                 goRs!Body = oEmail.Body           'PLAIN TEXT BODY NOTES
    77.                 goRs!HTMLBody = oEmail.HTMLBody   'HTML BODY NOTES
    78.                 goRs!Importance = oEmail.Importance
    79.                 goRs!Received = oEmail.ReceivedTime
    80.                 goRs!Class = oEmail.Class 'EMAIL, MEETING RESPONSE, MEETING REQUEST, DIST. LIST, ETC.
    81.                 goRs!ReceivedByName = oEmail.ReceivedByName
    82.                 If oEmail.Attachments.Count > 1 Then
    83.                     'LOOP THROUGH ALL ATTACHMENTS SAVING TO DIRECTORY AND ENTERING FILENAME INTO FIELD
    84.                     For ii = 1 To oEmail.Attachments.Count
    85.                         'DETERMINE ATTACHMENT CLASS TYPE ATTACHED, LINKED, EMBEDDED, OR OLE
    86.                         If oEmail.Attachments.Item(ii).Type = olByValue Then
    87.                             oEmail.Attachments.Item(ii).SaveAsFile "C:\MyOutlookEmailExport\Attachments\" & oEmail.Attachments.Item(ii).FileName
    88.                             sAttachment = sAttachment & "C:\MyOutlookEmailExport\Attachments\" & oEmail.Attachments.Item(ii).FileName & vbNewLine
    89.                         End If
    90.                     Next
    91.                     goRs!Attachment = sAttachment
    92.                 Else
    93.                     goRs!Attachment = "None"
    94.                 End If
    95.                 'CONTINUE ON WITH OTHER FIELD YOU WANT
    96.                 '...
    97.                 goRs.Update
    98.             End If
    99.             Set oEmail = Nothing
    100.         ElseIf vType = "MeetingItem" Then
    101.             Set oMeetingType = oInbox.Items(i)
    102.             If FindOutlookEmail(oMeetingType.EntryID) = False Then
    103.                 goRs.AddNew
    104.                 goRs!To = oMeetingType.Recipients.Item(1).Name
    105.                 goRs!CC = IIf(oMeetingType.Recipients.Count > 1, oMeetingType.Recipients.Item(2).Name, "")
    106.                 goRs!BCC = ""
    107.                 goRs!Subject = oMeetingType.Subject
    108.                 goRs!Body = oMeetingType.Body           'PLAIN TEXT BODY NOTES
    109.                 goRs!HTMLBody = ""
    110.                 goRs!Importance = oMeetingType.Importance
    111.                 goRs!Received = oMeetingType.ReceivedTime
    112.                 goRs!Class = oMeetingType.Class 'EMAIL, MEETING RESPONSE, MEETING REQUEST, DIST. LIST, ETC.
    113.                 goRs!ReceivedByName = ""
    114.                 If oMeetingType.Attachments.Count > 1 Then
    115.                     'LOOP THROUGH ALL ATTACHMENTS SAVING TO DIRECTORY AND ENTERING FILENAME INTO FIELD
    116.                     For ii = 1 To oMeetingType.Attachments.Count
    117.                         'DETERMINE ATTACHMENT CLASS TYPE ATTACHED, LINKED, EMBEDDED, OR OLE
    118.                         If oMeetingType.Attachments.Item(ii).Type = olByValue Then
    119.                             oMeetingType.Attachments.Item(ii).SaveAsFile "C:\MyOutlookEmailExport\Attachments\" & oMeetingType.Attachments.Item(ii).FileName
    120.                             sAttachment = sAttachment & "C:\MyOutlookEmailExport\Attachments\" & oMeetingType.Attachments.Item(ii).FileName & vbNewLine
    121.                         End If
    122.                     Next
    123.                     goRs!Attachment = sAttachment
    124.                 Else
    125.                     goRs!Attachment = "None"
    126.                 End If
    127.                 'CONTINUE ON WITH OTHER FIELD YOU WANT
    128.                 '...
    129.                 goRs.Update
    130.             End If
    131.             Set oMeetingType = Nothing
    132.         ElseIf vType = "DistListItem" Then
    133.             Set oDistributionList = oInbox.Items(i)
    134.             If FindOutlookEmail(oDistributionList.EntryID) = False Then
    135.                 goRs.AddNew
    136.                 goRs!To = oDistributionList.DLName
    137.                 goRs!CC = oDistributionList.MemberCount & "-Members"
    138.                 goRs!BCC = ""
    139.                 goRs!Subject = oDistributionList.Subject
    140.                 goRs!Body = oDistributionList.Body           'PLAIN TEXT BODY NOTES
    141.                 goRs!HTMLBody = ""
    142.                 goRs!Importance = oDistributionList.Importance
    143.                 goRs!Received = ""
    144.                 goRs!Class = oDistributionList.Class 'EMAIL, MEETING RESPONSE, MEETING REQUEST, DIST. LIST, ETC.
    145.                 goRs!ReceivedByName = ""
    146.                 If oDistributionList.Attachments.Count > 1 Then
    147.                     'LOOP THROUGH ALL ATTACHMENTS SAVING TO DIRECTORY AND ENTERING FILENAME INTO FIELD
    148.                     For ii = 1 To oDistributionList.Attachments.Count
    149.                         'DETERMINE ATTACHMENT CLASS TYPE ATTACHED, LINKED, EMBEDDED, OR OLE
    150.                         If oDistributionList.Attachments.Item(ii).Type = olByValue Then
    151.                             oDistributionList.Attachments.Item(ii).SaveAsFile "C:\MyOutlookEmailExport\Attachments\" & oDistributionList.Attachments.Item(ii).FileName
    152.                             sAttachment = sAttachment & "C:\MyOutlookEmailExport\Attachments\" & oDistributionList.Attachments.Item(ii).FileName & vbNewLine
    153.                         End If
    154.                     Next
    155.                     goRs!Attachment = sAttachment
    156.                 Else
    157.                     goRs!Attachment = "None"
    158.                 End If
    159.                 'CONTINUE ON WITH OTHER FIELD YOU WANT
    160.                 '...
    161.                 goRs.Update
    162.             End If
    163.             Set oDistributionList = Nothing
    164.         Else
    165.             MsgBox "Unsupported message type!", vbOKOnly + vbExclamation
    166.         End If
    167.         Set oEmail = Nothing
    168.         prbProgress.Value = i
    169.         i = i + 1
    170.     Loop
    171.    
    172.     Set oInbox = Nothing
    173.     Set oNS = Nothing
    174.     goRs.Close
    175.     Set CnnA = Nothing
    176.     Set goRs = Nothing
    177.     Exit Function
    178.  
    179. No_Bugs:
    180.    
    181.     MsgBox Err.Number & "-" & Err.Description, vbCritical, "Outlook Email Export"
    182. '    Resume Next
    183.  
    184. End Function
    185.  
    186. Private Function FindOutlookEmail(ByVal oEmailEntryID As String) As Boolean
    187.  
    188.     Dim oRsAccessEmail As ADODB.Recordset 'EMAIL IN ACCESS TO CHECK AGAINST
    189.    
    190.     Dim i As Integer
    191.    
    192.     Set oRsAccessEmail = New ADODB.Recordset
    193.     oRsAccessEmail.Open "SELECT EntryID FROM Inbox WHERE EntryID = '" & oEmailEntryID & "';", CnnA, adOpenKeyset, adLockReadOnly, adCmdText
    194.     If oRsAccessEmail.BOF = True And oRsAccessEmail.EOF = True Then
    195.         FindOutlookEmail = False
    196.     Else
    197.         FindOutlookEmail = True
    198.     End If
    199.     Set oRsAccessEmail = Nothing
    200.  
    201. End Function
    202.  
    203. Private Function OpenOutlookEmail(ByVal oEmailEnrtyID As String)
    204.    
    205.     Dim oDisplayEmail As Outlook.MailItem
    206.    
    207.     Set oDisplayEmail = oInbox.Items.Find("[EntryID] = '" & oEmailEntryID & "'")
    208.     If TypeName(oDisplayEmail) <> "Nothing" Then
    209.         oDisplayEmail.Display 'THE USE CAN MAKE CHANGES AND SAVE THEM TO OUTLOOK
    210.     Else
    211.         MsgBox "Email not found in Outlook!", vbOKOnly + vbExclamation
    212.     End If
    213.  
    214. End Function
    This should get you just about there.
    I wasn't able to test the dist. list. I didn't have the time.
    Please note: there are changes throughout the code.
    Need to get back to billable work.
    Let me know how it goes.

    Later, and enjoy.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  20. #20

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2002
    Posts
    665
    Jesus RobDog888!!

    What should I do without you?

    Big Thanks!

    That solve my problems...

    But I have two more questions, and belive me, thats my last questions...

    If I only want to save the attachments that are olByValue and olEmbeddeditem type, how do I do that?

    And if I in a easy want to change folder where I import the mail from, how do I do that?
    Now we use the default folder.

  21. #21
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    To change the import folder location:
    VB Code:
    1. Private Sub Form_Load()
    2.  
    3.     Set oApp = New Outlook.Application
    4.     Set oNS = oApp.GetNamespace("MAPI")
    5.     'Set oInbox = oNS.GetDefaultFolder(olFolderInbox)
    6.     'MUST BE A FOLDER WITH A DEFAULT MESSAGE TYPE OF POST!!!
    7.     'RIGHT CLICK ON THE DESIRED NEW FOLDER AND CLICK PROPERTIES.
    8.     'THEN LOOK AT THE BOTTON AND IT SHOULD SAY -
    9.     '"WHEN POSTING TO THIS FOLDER, USE: POST"
    10.     Set oInbox = oNS.Folders("[color=red]Your Custom Folder Name Here![/color]")
    11.    
    12.     Set CnnA = New ADODB.Connection
    13.     CnnA.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & txtDBPath & ";Persist Security Info=False"
    14.     CnnA.Open
    15.    
    16. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  22. #22
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Your welcome.

    If I only want to save the attachments that are olByValue and olEmbeddeditem type, how do I do that?
    The way the code is now, it only saves Attachments that are file attachments.
    To enable it to save Embedded items is a bit more difficult.
    Let me test something out and I will get back to you, but this is
    how to filter both types.
    VB Code:
    1. 'THIS IS A SAMPLE FROM THE IMPORT CODE
    2. 'LOOP THROUGH ALL ATTACHMENTS SAVING TO DIRECTORY AND ENTERING FILENAME INTO FIELD
    3. For ii = 1 To oEmail.Attachments.Count
    4.     'DETERMINE ATTACHMENT CLASS TYPE ATTACHED, LINKED, EMBEDDED, OR OLE
    5.     If oEmail.Attachments.Item(ii).Type = olByValue Then
    6.         oEmail.Attachments.Item(ii).SaveAsFile "C:\MyOutlookEmailExport\Attachments\" & oEmail.Attachments.Item(ii).FileName
    7.         sAttachment = sAttachment & "C:\MyOutlookEmailExport\Attachments\" & oEmail.Attachments.Item(ii).FileName & vbNewLine
    8.     ElseIf oEmail.Attachments.Item(ii).Type = olEmbeddeditem Then
    9.         'CODE WILL GO HERE
    10.  
    11.     End If
    12. Next
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  23. #23

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2002
    Posts
    665
    When I'm use:
    Set oInbox = oNS.Folders("MyFolder")
    I get error: The measure failed. ??
    And I use "POST" rule for this folder.


    I'm waiting with tension for the "Save Embedded items" solution...

  24. #24
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    I'm sorry, I forgot to tell you that you need to drill down the
    folder path to the target folder.
    VB Code:
    1. Set oApp = New Outlook.Application
    2. Set oNS = oApp.GetNamespace("MAPI")
    3. Set oPF = oNS.Folders("Public Folders")
    4. Set oAPF = oPF.Folders("All Public Folders")
    5. Set oInbox = oAPF.Folders("Public Emails")
    Also dont forget to Dim the objects also.
    Same as the oInbox.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  25. #25
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Here is the modified code snip for saving both types of
    attachments that you wanted.
    VB Code:
    1. If oEmail.Attachments.Count > 1 Then
    2.     'LOOP THROUGH ALL ATTACHMENTS SAVING TO DIRECTORY AND ENTERING FILENAME INTO FIELD
    3.     For ii = 1 To oEmail.Attachments.Count
    4.         'DETERMINE ATTACHMENT CLASS TYPE ATTACHED, LINKED, EMBEDDED, OR OLE
    5.         If oEmail.Attachments.Item(ii).Type = olByValue Or oEmail.Attachments.Item(ii).Type = olEmbeddeditem Then
    6.             oEmail.Attachments.Item(ii).SaveAsFile "C:\MyOutlookEmailExport\Attachments\" & oEmail.Attachments.Item(ii).FileName
    7.             sAttachment = sAttachment & "C:\MyOutlookEmailExport\Attachments\" & oEmail.Attachments.Item(ii).FileName & vbNewLine
    8.         End If
    9.     Next
    10.     goRs!Attachment = sAttachment
    11. Else
    12.     goRs!Attachment = "None"
    13. End If
    It turned out that it works the same for either type.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  26. #26
    Lively Member binduau's Avatar
    Join Date
    Sep 2003
    Location
    Perth Australia
    Posts
    121

    Open a Given Outlook folder and display it

    HI RobDog

    I don't suppose you know how to open an outlook folder and view it... i tried this with no success

    VB Code:
    1. strFileName = "Outlook.exe /select outlook:Orders"
    2. Shell strFileName, vbNormalFocus

    This works....

    VB Code:
    1. strFileName = "c:\msoffice2k\office\Outlook.exe /select outlook:Orders"
    2.    Shell strFileName, vbNormalFocus


    but ...


    this >> "Outlook.exe /select outlook:Orders <<<< works in the
    windows run dialog but i can't get it to run from shell..

    but ....

    what if a user puts outlook or office in a different place upon install... .... such as d:\blah... instead of C:\program files\blah


    i could get the default root install folder from the registry of office
    but i dunno how........

    this is where it is...

    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\8.0\Common\InstallRootSR1

    I know how to use getsettings....

    VB Code:
    1. DefEmailTemplate = GetSetting("EmailOrderProcessor", "EmailAddresses", _
    2. "DefaultEmailTemplateFolder", App.path & "\defaulttpl.tpl")

    but how do i configure that for the keys of ....

    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\8.0\Common\InstallRootSR1

    with respect


    bindu

  27. #27
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    In order to read a reqistry key that is not in the VB and VBA
    Program Settings key (GetSetting and SaveSetting location) you
    need to use the APIs for reading the registry.
    Try searching for RegOpenKey.

    The shelling issue you say works from the run dialog box, but not
    from the shell function?
    What is the actual location of the Order folder in Outlook?
    Is it a top level folder or is it nested down some path?

    PS. this should really be a new thread.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  28. #28

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2002
    Posts
    665
    Fantastic RobDog888!!!

    Everything gets better and better..;-)

    If the mails empty (no text inside) I get error.

    And I still get error 91 when I use function OpenOutlookEmail?

  29. #29
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    When you say that "If the mails empty..." do you mean that the
    Outlook email body is empty or the folder is empty?

    I found part of the problem with the function - OpenOutlookEmail.
    The passed variable declaration is spelled wrong (my dyslexia!).
    VB Code:
    1. Private Function OpenOutlookEmail(ByVal [color=red]oEmailEntryID[/color] As String)
    2.    
    3.     Dim oDisplayEmail As Outlook.MailItem
    4.    
    5.     Set oDisplayEmail = oInbox.Items.Find("[EntryID] = '" & oEmailEntryID & "'")
    6.     If TypeName(oDisplayEmail) <> "Nothing" Then
    7.         oDisplayEmail.Display 'THE USE CAN MAKE CHANGES AND SAVE THEM TO OUTLOOK
    8.     Else
    9.         MsgBox "Email not found in Outlook!", vbOKOnly + vbExclamation
    10.     End If
    11.  
    12. End Function
    Give me a minute and I will figure it out.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  30. #30

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2002
    Posts
    665
    Yepp, I mean that Outlook email body is empty.

    I have also seen that you spelled wrong there..
    But the error is still there?

  31. #31
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    I think you may have the field (Body and HTMLBody) definitions
    defined to not allow nulls or zero length strings.

    Here are the other fixes.
    VB Code:
    1. '...
    2.     '...
    3.     If FindOutlookEmail(oEmail.EntryID) = False Then
    4.         'ADDED FOR TESTING ONLY SO YOU CAN SEE HOW TO PASS THE ITEMS.
    5.         'TAKE OUT AND PLACE WHERE YOU ARE CALLING IT FROM
    6.         'OTHERWISE IT WILL OPEN ALL EXPORTED EMAILS
    7.         Call OpenOutlookEmail(oEmail.EntryID, CStr(oInbox.Items(i).Class))
    8.         goRs.AddNew
    9.         '...
    10.         '...
    11.         '...
    12. End Sub
    13.  
    14. '...
    15. '...
    16.  
    17. Private Function OpenOutlookEmail(ByVal oEmailEntryID As String, sType As String)
    18. 'PASS THE .ENTRYID AND THE .TYPE PROPERTY OF THE OUTLOOK ITEM
    19.     On Error GoTo No_Bugs
    20.    
    21.     Dim oDisplayEmail As Outlook.MailItem
    22.     Dim oDisplayMeetingItem As Outlook.MeetingItem
    23.     Dim oDistListItem As Outlook.DistListItem
    24.     Dim oItem As Object
    25.     Dim i As Integer
    26.     Dim bFound As Boolean
    27.    
    28.     'CHECK EACH ITEMS ENTRYID PROPERTY FOR A MATCH (.FIND NOT COMPATIBLE WITH .ENTRYID PROPERTY)
    29.     bFound = False
    30.     For i = 1 To oInbox.Items.Count
    31.         Set oItem = oInbox.Items(i)
    32.         If oItem.EntryID = oEmailEntryID Then
    33.             bFound = True
    34.             Exit For
    35.         Else
    36.             bFound = False
    37.         End If
    38.         Set oItem = Nothing
    39.     Next
    40.    
    41.     If bFound = True Then
    42.         Select Case CLng(sType)
    43.             Case olMail
    44.                 sType = "Email"
    45.             Case olMeetingRequest, olMeetingResponseNegative, olMeetingResponsePositive, olMeetingResponseTentative
    46.                 sType = "MeetingItem"
    47.             Case olDistributionList
    48.                 sType = "DistListItem"
    49.             Case Else
    50.                 sType = ""
    51.         End Select
    52.         Select Case sType
    53.             Case "Email"
    54.                 Set oDisplayEmail = oInbox.Items(i)
    55.                 If TypeName(oDisplayEmail) <> "Nothing" Then
    56.                     oDisplayEmail.Display 'THE USE CAN MAKE CHANGES AND SAVE THEM TO OUTLOOK
    57.                 Else
    58.                     MsgBox "Email not found in Outlook!", vbOKOnly + vbExclamation
    59.                 End If
    60.             Case "MeetingItem"
    61.                 Set oDisplayMeetingItem = oInbox.Items(i)
    62.                 If TypeName(oDisplayMeetingItem) <> "Nothing" Then
    63.                     oDisplayMeetingItem.Display 'THE USE CAN MAKE CHANGES AND SAVE THEM TO OUTLOOK
    64.                 Else
    65.                     MsgBox "Meeting Item not found in Outlook!", vbOKOnly + vbExclamation
    66.                 End If
    67.             Case "DistListItem"
    68.                 Set oDistListItem = oInbox.Items(i)
    69.                 If TypeName(oDistListItem) <> "Nothing" Then
    70.                     oDistListItem.Display 'THE USE CAN MAKE CHANGES AND SAVE THEM TO OUTLOOK
    71.                 Else
    72.                     MsgBox "Distribution List not found in Outlook!", vbOKOnly + vbExclamation
    73.                 End If
    74.             Case Else
    75.                 MsgBox "Invalid Outlook Item Type Passed!", vbOKOnly + vbCritical
    76.         End Select
    77.     Else
    78.         MsgBox "Item not found in Outlook!", vbOKOnly + vbInformation
    79.     End If
    80.     Exit Function
    81.    
    82. No_Bugs:
    83.  
    84.     MsgBox Err.Number & " = " & Err.Description, vbOKOnly + vbExclamation
    85.     Exit Function
    86.     Resume
    87. End Function
    Last edited by RobDog888; Oct 15th, 2003 at 01:38 PM.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  32. #32
    Lively Member binduau's Avatar
    Join Date
    Sep 2003
    Location
    Perth Australia
    Posts
    121

    Yeh a New thread maybe

    I cracked it anywhooo

    thanks


    bindu



  33. #33
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    binduau, what was the solution?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  34. #34

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2002
    Posts
    665
    RobDog888, I'm so satisfied now!!

    Thanks for your help!

    Guess what....I have a last question...

    If I want to import from the task folder, how do I do then?

    Now it's working to open and read a mail, is it possible to decrease that code a little?

  35. #35
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    I guess we could use late binding and have the function reduced that way.
    Short partial sample of OpenOutlookEmail function.
    VB Code:
    1. Dim oObject as Object 'INSTEAD OF
    2. 'Dim oDisplayEmail As Outlook.MailItem
    3. 'Dim oDisplayMeetingItem As Outlook.MeetingItem
    4. 'Dim oDistListItem As Outlook.DistListItem
    5.  
    6. 'THEN INSTEAD OF THREE BRANCHES OF CODE TO HANDEL EACH
    7. 'ONE YOU CAN HAVE JUST ONE
    8.  
    9. Set oObject = oInbox.Items(i)
    10. If TypeName(oObject) <> "Nothing" Then
    11.     oObject.Display 'THE USE CAN MAKE CHANGES AND SAVE THEM TO OUTLOOK
    12. Else
    13.     MsgBox "Email not found in Outlook!", vbOKOnly + vbExclamation
    14. End If
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  36. #36
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    To change the code to use the Task folder.
    Then you will need to write similar function to export the tasks to
    a Tasks table.
    VB Code:
    1. Option Explicit
    2.  
    3. Private oApp As Outlook.Application
    4. Private oNS As Outlook.NameSpace
    5. Private oInbox As Outlook.MAPIFolder
    6. Private oTasks As Outlook.MAPIFolder
    7. Private CnnA As ADODB.Connection
    8.  
    9. Private Sub cmdExportEmails_Click()
    10.  
    11.     Call Outlook_Emails_2_Access
    12.  
    13. End Sub
    14.  
    15. Private Sub Form_Load()
    16.  
    17.     Set oApp = New Outlook.Application
    18.     Set oNS = oApp.GetNamespace("MAPI")
    19.     Set oInbox = oNS.GetDefaultFolder(olFolderInbox)
    20.     Set oTasks = oNS.GetDefaultFolder(olFolderTasks)
    21.     'MUST BE A FOLDER WITH A DEFAULT MESSAGE TYPE OF POST!!!
    22.     'RIGHT CLICK ON THE DESIRED NEW FOLDER AND CLICK PROPERTIES.
    23.     'THEN LOOK AT THE BOTTON AND IT SHOULD SAY -
    24.     '"WHEN POSTING TO THIS FOLDER, USE: POST"
    25. '    Set oInbox = oNS.Folders("Your Custom Folder Name Here!")
    26.    
    27.     Set CnnA = New ADODB.Connection
    28.     CnnA.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & txtDBPath & ";Persist Security Info=False"
    29.     CnnA.Open
    30.    
    31. End Sub
    Glad to help.
    How about a honorable mention in your programs about credits?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  37. #37

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2002
    Posts
    665
    I will certainly make honorable mention about you in my programs.
    I need your e-mail and name to do that. I can't just write RobDog888 as your name...

    I have tried the task code you showed me, but I can't write a working function to export the tasks to a Tasks table. Can you show me a complete way to do this?
    Last edited by Pirre001; Oct 16th, 2003 at 03:09 PM.

  38. #38
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    First third of program that I have.
    VB Code:
    1. Option Explicit
    2.  
    3. Private oApp As Outlook.Application
    4. Private oNS As Outlook.NameSpace
    5. Private oInbox As Outlook.MAPIFolder
    6. Private oTasks As Outlook.MAPIFolder
    7. Private CnnA As ADODB.Connection
    8.  
    9. Private Sub cmdExportEmails_Click()
    10.  
    11.     Call Outlook_Emails_2_Access
    12.  
    13. End Sub
    14.  
    15. Private Sub Form_Load()
    16.  
    17.     Set oApp = New Outlook.Application
    18.     Set oNS = oApp.GetNamespace("MAPI")
    19.     Set oInbox = oNS.GetDefaultFolder(olFolderInbox)
    20.     Set oTasks = oNS.GetDefaultFolder(olFolderTasks)
    21.     'MUST BE A FOLDER WITH A DEFAULT MESSAGE TYPE OF POST!!!
    22.     'RIGHT CLICK ON THE DESIRED NEW FOLDER AND CLICK PROPERTIES.
    23.     'THEN LOOK AT THE BOTTON AND IT SHOULD SAY -
    24.     '"WHEN POSTING TO THIS FOLDER, USE: POST"
    25. '    Set oInbox = oNS.Folders("Your Custom Folder Name Here!")
    26.    
    27.     Set CnnA = New ADODB.Connection
    28.     CnnA.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & txtDBPath & ";Persist Security Info=False"
    29.     CnnA.Open
    30.    
    31. End Sub
    32.  
    33. Private Function Outlook_Emails_2_Access()
    34. '   <GORS = ACCESS>
    35.     On Error GoTo No_Bugs
    36.  
    37.     Dim goRs As ADODB.Recordset
    38.    
    39.     Dim oEmail As Outlook.MailItem
    40.     Dim oMeetingType As Outlook.MeetingItem
    41.     Dim oDistributionList As Outlook.DistListItem
    42.    
    43.     Dim vType As Variant
    44.    
    45.     Dim i As Integer
    46.     Dim ii As Integer
    47.    
    48.     Dim sAttachment As String
    49.    
    50.     Dim sSQL As String
    51.    
    52.     Set goRs = New ADODB.Recordset
    53.    
    54.     sSQL = "SELECT * FROM [Inbox] WHERE 1=2;"
    55.     goRs.Open sSQL, CnnA, adOpenKeyset, adLockOptimistic, adCmdText
    56.    
    57.     prbProgress.Max = oInbox.Items.Count
    58.     i = 1
    59.     Do While i <= oInbox.Items.Count
    60.         DoEvents
    61.         Select Case oInbox.Items(i).Class
    62.             Case olMail
    63.                 vType = "Email"
    64.             Case olMeetingRequest
    65.                 vType = "MeetingItem"
    66.             Case olMeetingResponseNegative
    67.                 vType = "MeetingItem"
    68.             Case olMeetingResponsePositive
    69.                 vType = "MeetingItem"
    70.             Case olMeetingResponseTentative
    71.                 vType = "MeetingItem"
    72.             Case olDistributionList
    73.                 vType = "DistListItem"
    74.         End Select
    75.         If vType = "Email" Then 'ONLY EMAIL TYPES
    76.             Set oEmail = oInbox.Items(i)
    77.             If FindOutlookEmail(oEmail.EntryID) = False Then
    78.                 'ADDED FOR TESTING ONLY SO YOU CAN SEE HOW TO PASS THE ITEMS.
    79.                 'TAKE OUT AND PLACE WHERE YOU ARE CALLING IT FROM
    80.                 'OTHERWISE IT WILL OPEN ALL EXPORTED EMAILS
    81.                 Call OpenOutlookEmail(oEmail.EntryID, CStr(oInbox.Items(i).Class))
    82.                 goRs.AddNew
    83.                 goRs!To = oEmail.To
    84.                 goRs!CC = oEmail.CC
    85.                 goRs!BCC = oEmail.BCC
    86.                 goRs!Subject = oEmail.Subject
    87.                 goRs!Body = oEmail.Body           'PLAIN TEXT BODY NOTES
    88.                 goRs!HTMLBody = oEmail.HTMLBody   'HTML BODY NOTES
    89.                 goRs!Importance = oEmail.Importance
    90.                 goRs!Received = oEmail.ReceivedTime
    91.                 goRs!Class = oEmail.Class 'EMAIL, MEETING RESPONSE, MEETING REQUEST, DIST. LIST, ETC.
    92.                 goRs!ReceivedByName = oEmail.ReceivedByName
    93.                 goRs!EntryID = oEmail.EntryID
    94.                 If oEmail.Attachments.Count > 1 Then
    95.                     'LOOP THROUGH ALL ATTACHMENTS SAVING TO DIRECTORY AND ENTERING FILENAME INTO FIELD
    96.                     For ii = 1 To oEmail.Attachments.Count
    97.                         'DETERMINE ATTACHMENT CLASS TYPE ATTACHED, LINKED, EMBEDDED, OR OLE
    98.                         If oEmail.Attachments.Item(ii).Type = olByValue Or oEmail.Attachments.Item(ii).Type = olEmbeddeditem Then
    99.                             oEmail.Attachments.Item(ii).SaveAsFile "C:\MyOutlookEmailExport\Attachments\" & oEmail.Attachments.Item(ii).FileName
    100.                             sAttachment = sAttachment & "C:\MyOutlookEmailExport\Attachments\" & oEmail.Attachments.Item(ii).FileName & vbNewLine
    101.                         End If
    102.                     Next
    103.                     goRs!Attachment = sAttachment
    104.                 Else
    105.                     goRs!Attachment = "None"
    106.                 End If
    107.                 'CONTINUE ON WITH OTHER FIELD YOU WANT
    108.                 '...
    109.                 goRs.Update
    110.             End If
    111.             Set oEmail = Nothing
    112.         ElseIf vType = "MeetingItem" Then
    113.             Set oMeetingType = oInbox.Items(i)
    114.             If FindOutlookEmail(oMeetingType.EntryID) = False Then
    115.                 goRs.AddNew
    116.                 goRs!To = oMeetingType.Recipients.Item(1).Name
    117.                 goRs!CC = IIf(oMeetingType.Recipients.Count > 1, oMeetingType.Recipients.Item(2).Name, "")
    118.                 goRs!BCC = ""
    119.                 goRs!Subject = oMeetingType.Subject
    120.                 goRs!Body = oMeetingType.Body           'PLAIN TEXT BODY NOTES
    121.                 goRs!HTMLBody = ""
    122.                 goRs!Importance = oMeetingType.Importance
    123.                 goRs!Received = oMeetingType.ReceivedTime
    124.                 goRs!Class = oMeetingType.Class 'EMAIL, MEETING RESPONSE, MEETING REQUEST, DIST. LIST, ETC.
    125.                 goRs!ReceivedByName = ""
    126.                 goRs!EntryID = oMeetingType.EntryID
    127.                 If oMeetingType.Attachments.Count > 1 Then
    128.                     'LOOP THROUGH ALL ATTACHMENTS SAVING TO DIRECTORY AND ENTERING FILENAME INTO FIELD
    129.                     For ii = 1 To oMeetingType.Attachments.Count
    130.                         'DETERMINE ATTACHMENT CLASS TYPE ATTACHED, LINKED, EMBEDDED, OR OLE
    131.                         If oMeetingType.Attachments.Item(ii).Type = olByValue Or oMeetingType.Attachments.Item(ii).Type = olEmbeddeditem Then
    132.                             oMeetingType.Attachments.Item(ii).SaveAsFile "C:\MyOutlookEmailExport\Attachments\" & oMeetingType.Attachments.Item(ii).FileName
    133.                             sAttachment = sAttachment & "C:\MyOutlookEmailExport\Attachments\" & oMeetingType.Attachments.Item(ii).FileName & vbNewLine
    134.                         End If
    135.                     Next
    136.                     goRs!Attachment = sAttachment
    137.                 Else
    138.                     goRs!Attachment = "None"
    139.                 End If
    140.                 'CONTINUE ON WITH OTHER FIELD YOU WANT
    141.                 '...
    142.                 goRs.Update
    143.             End If
    144.             Set oMeetingType = Nothing
    145.         ElseIf vType = "DistListItem" Then
    146.             Set oDistributionList = oInbox.Items(i)
    147.             If FindOutlookEmail(oDistributionList.EntryID) = False Then
    148.                 goRs.AddNew
    149.                 goRs!To = oDistributionList.DLName
    150.                 goRs!CC = oDistributionList.MemberCount & "-Members"
    151.                 goRs!BCC = ""
    152.                 goRs!Subject = oDistributionList.Subject
    153.                 goRs!Body = oDistributionList.Body           'PLAIN TEXT BODY NOTES
    154.                 goRs!HTMLBody = ""
    155.                 goRs!Importance = oDistributionList.Importance
    156.                 goRs!Received = ""
    157.                 goRs!Class = oDistributionList.Class 'EMAIL, MEETING RESPONSE, MEETING REQUEST, DIST. LIST, ETC.
    158.                 goRs!ReceivedByName = ""
    159.                 goRs!EntryID = oDistributionList.EntryID
    160.                 If oDistributionList.Attachments.Count > 1 Then
    161.                     'LOOP THROUGH ALL ATTACHMENTS SAVING TO DIRECTORY AND ENTERING FILENAME INTO FIELD
    162.                     For ii = 1 To oDistributionList.Attachments.Count
    163.                         'DETERMINE ATTACHMENT CLASS TYPE ATTACHED, LINKED, EMBEDDED, OR OLE
    164.                         If oDistributionList.Attachments.Item(ii).Type = olByValue Or oDistributionList.Attachments.Item(ii).Type = olEmbeddeditem Then
    165.                             oDistributionList.Attachments.Item(ii).SaveAsFile "C:\MyOutlookEmailExport\Attachments\" & oDistributionList.Attachments.Item(ii).FileName
    166.                             sAttachment = sAttachment & "C:\MyOutlookEmailExport\Attachments\" & oDistributionList.Attachments.Item(ii).FileName & vbNewLine
    167.                         End If
    168.                     Next
    169.                     goRs!Attachment = sAttachment
    170.                 Else
    171.                     goRs!Attachment = "None"
    172.                 End If
    173.                 'CONTINUE ON WITH OTHER FIELD YOU WANT
    174.                 '...
    175.                 goRs.Update
    176.             End If
    177.             Set oDistributionList = Nothing
    178.         Else
    179.             MsgBox "Unsupported message type!", vbOKOnly + vbExclamation
    180.         End If
    181.         Set oEmail = Nothing
    182.         prbProgress.Value = i
    183.         i = i + 1
    184.     Loop
    185.    
    186.     Set oInbox = Nothing
    187.     Set oNS = Nothing
    188.     goRs.Close
    189.     Set CnnA = Nothing
    190.     Set goRs = Nothing
    191.     Exit Function
    192.  
    193. No_Bugs:
    194.    
    195.     MsgBox Err.Number & "-" & Err.Description, vbCritical, "Outlook Email Export"
    196.     Resume
    197.  
    198. End Function
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  39. #39
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Second third.
    VB Code:
    1. Private Function FindOutlookEmail(ByVal oEmailEntryID As String) As Boolean
    2.  
    3.     Dim oRsAccessEmail As ADODB.Recordset 'EMAIL IN ACCESS TO CHECK AGAINST
    4.    
    5.     Dim i As Integer
    6.    
    7.     Set oRsAccessEmail = New ADODB.Recordset
    8.     oRsAccessEmail.Open "SELECT EntryID FROM Inbox WHERE EntryID = '" & oEmailEntryID & "';", CnnA, adOpenKeyset, adLockReadOnly, adCmdText
    9.     If oRsAccessEmail.BOF = True And oRsAccessEmail.EOF = True Then
    10.         FindOutlookEmail = False
    11.     Else
    12.         FindOutlookEmail = True
    13.     End If
    14.     Set oRsAccessEmail = Nothing
    15.  
    16. End Function
    17.  
    18. Private Function OpenOutlookEmail(ByVal oEmailEntryID As String, sType As String)
    19. 'PASS THE .ENTRYID AND THE .TYPE PROPERTY OF THE OUTLOOK ITEM
    20.     On Error GoTo No_Bugs
    21.    
    22.     Dim oOBJ As Object
    23.     Dim oItem As Object
    24.     Dim i As Integer
    25.     Dim bFound As Boolean
    26.    
    27.     'CHECK EACH ITEMS ENTRYID PROPERTY FOR A MATCH (.FIND NOT COMPATIBLE WITH .ENTRYID PROPERTY)
    28.     bFound = False
    29.     For i = 1 To oInbox.Items.Count
    30.         Set oItem = oInbox.Items(i)
    31.         If oItem.EntryID = oEmailEntryID Then
    32.             bFound = True
    33.             Exit For
    34.         Else
    35.             bFound = False
    36.         End If
    37.         Set oItem = Nothing
    38.     Next
    39.    
    40.     If bFound = True Then
    41.         Select Case CLng(sType)
    42.             Case olMail
    43.                 sType = "Email"
    44.             Case olMeetingRequest, olMeetingResponseNegative, olMeetingResponsePositive, olMeetingResponseTentative
    45.                 sType = "MeetingItem"
    46.             Case olDistributionList
    47.                 sType = "DistListItem"
    48.             Case Else
    49.                 sType = ""
    50.         End Select
    51.         Select Case sType
    52.             Case "Email", "MeetingItem", "DistListItem"
    53.                 Set oOBJ = oInbox.Items(i)
    54.                 If TypeName(oOBJ) <> "Nothing" Then
    55.                     oOBJ.Display 'THE USE CAN MAKE CHANGES AND SAVE THEM TO OUTLOOK
    56.                 Else
    57.                     MsgBox sType & " not found in Outlook!", vbOKOnly + vbExclamation
    58.                 End If
    59.             Case Else
    60.                 MsgBox "Invalid Outlook Item Type Passed!", vbOKOnly + vbCritical
    61.         End Select
    62.     Else
    63.         MsgBox "Item not found in Outlook!", vbOKOnly + vbInformation
    64.     End If
    65.     Exit Function
    66.    
    67. No_Bugs:
    68.  
    69.     MsgBox Err.Number & " = " & Err.Description, vbOKOnly + vbExclamation
    70.     Exit Function
    71.     Resume
    72. End Function
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  40. #40
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Last third.
    VB Code:
    1. Private Function Outlook_Tasks_2_Access()
    2. '   <GORS = ACCESS>
    3.     On Error GoTo No_Bugs
    4.  
    5.     Dim goRs As ADODB.Recordset
    6.    
    7.     Dim oTask As Outlook.TaskItem
    8.     Dim oTaskReqAccpt As Outlook.TaskRequestAcceptItem
    9.     Dim oTaskReqDcln As Outlook.TaskRequestDeclineItem
    10.     Dim oTaskReqItm As Outlook.TaskRequestItem
    11.     Dim oTaskReqUpd As Outlook.TaskRequestUpdateItem
    12.    
    13.     Dim vType As Variant
    14.    
    15.     Dim i As Integer
    16.     Dim ii As Integer
    17.     Dim iii As Integer
    18.    
    19.     Dim sAttachment As String
    20.     Dim sRecipients As String
    21.    
    22.     Dim sSQL As String
    23.    
    24.     Set goRs = New ADODB.Recordset
    25.    
    26.     sSQL = "SELECT * FROM [Tasks] WHERE 1=2;"
    27.     goRs.Open sSQL, CnnA, adOpenKeyset, adLockOptimistic, adCmdText
    28.    
    29.     prbProgress.Max = oTasks.Items.Count
    30.     i = 1
    31.     Do While i <= oTasks.Items.Count
    32.         DoEvents
    33.         Select Case oTasks.Items(i).Class
    34.             Case olTask
    35.                 vType = "Task"
    36.             Case olTaskRequestAccept
    37.                 vType = "TaskReqAccpt"
    38.             Case olTaskRequestDecline
    39.                 vType = "TaskReqDcln"
    40.             Case olTaskRequest
    41.                 vType = "TaskReqItm"
    42.             Case olTaskRequestUpdate
    43.                 vType = "TaskReqUpd"
    44.         End Select
    45.         If vType = "Task" Then 'ONLY TASK TYPES
    46.             Set oTask = oTasks.Items(i)
    47.             If FindOutlookTask(oTask.EntryID) = False Then
    48.                 'ADDED FOR TESTING ONLY SO YOU CAN SEE HOW TO PASS THE ITEMS.
    49.                 'TAKE OUT AND PLACE WHERE YOU ARE CALLING IT FROM
    50.                 'OTHERWISE IT WILL OPEN ALL EXPORTED TASKS
    51.                 Call OpenOutlookTask(oTask.EntryID, CStr(oTasks.Items(i).Class))
    52.                 goRs.AddNew
    53.                 goRs!To = oTask.Recipients.Item(0).Name
    54.                 sRecipients = ""
    55.                 For iii = 1 To oTask.Recipients.Count - 1
    56.                     sRecipients = sRecipients & oTask.Recipients.Item(iii).Name & "; "
    57.                 Next
    58.                 goRs!CC = sRecipients
    59.                 goRs!Subject = oTask.Subject
    60.                 goRs!Body = oTask.Body           'PLAIN TEXT BODY NOTES
    61.                 goRs!Importance = oTask.Importance
    62.                 goRs!StartDate = oTask.StartDate
    63.                 goRs!Class = oTask.Class 'TASK, TASKREQUESTACCEPT, TASKREQUESTDECLINE, ETC.
    64.                 goRs!ReceivedByName = oTask.ReceivedByName
    65.                 goRs!EntryID = oTask.EntryID
    66.                 If oTask.Attachments.Count > 1 Then
    67.                     'LOOP THROUGH ALL ATTACHMENTS SAVING TO DIRECTORY AND ENTERING FILENAME INTO FIELD
    68.                     For ii = 1 To oTask.Attachments.Count
    69.                         'DETERMINE ATTACHMENT CLASS TYPE ATTACHED, LINKED, EMBEDDED, OR OLE
    70.                         If oTask.Attachments.Item(ii).Type = olByValue Or oTask.Attachments.Item(ii).Type = olEmbeddeditem Then
    71.                             oTask.Attachments.Item(ii).SaveAsFile "C:\MyOutlookEmailExport\Attachments\" & oTask.Attachments.Item(ii).FileName
    72.                             sAttachment = sAttachment & "C:\MyOutlookEmailExport\Attachments\" & oTask.Attachments.Item(ii).FileName & vbNewLine
    73.                         End If
    74.                     Next
    75.                     goRs!Attachment = sAttachment
    76.                 Else
    77.                     goRs!Attachment = "None"
    78.                 End If
    79.                 'CONTINUE ON WITH OTHER FIELD YOU WANT
    80.                 '...
    81.                 goRs.Update
    82.             End If
    83.             Set oTask = Nothing
    84.         'MORE CODE GOES HERE FOLOWING SAME LOGIC
    85.         'ElseIf
    86.        
    87.         'ElseIf
    88.         'etc.
    89.         End If
    90.  
    91.  
    92. End Function
    93.  
    94. Private Function FindOutlookTask(ByVal oTaskEntryID As String) As Boolean
    95.  
    96.     Dim oRsAccessTask As ADODB.Recordset 'TASK IN ACCESS TO CHECK AGAINST
    97.    
    98.     Dim i As Integer
    99.    
    100.     Set oRsAccessTask = New ADODB.Recordset
    101.     oRsAccessTask.Open "SELECT EntryID FROM Tasks WHERE EntryID = '" & oTaskEntryID & "';", CnnA, adOpenKeyset, adLockReadOnly, adCmdText
    102.     If oRsAccessTask.BOF = True And oRsAccessTask.EOF = True Then
    103.         FindOutlookTask = False
    104.     Else
    105.         FindOutlookTask = True
    106.     End If
    107.     Set oRsAccessTask = Nothing
    108.  
    109. End Function
    110.  
    111. Private Function OpenOutlookTask(ByVal oTaskEntryID As String, sType As String)
    112. 'PASS THE .ENTRYID AND THE .TYPE PROPERTY OF THE OUTLOOK ITEM
    113.     On Error GoTo No_Bugs
    114.    
    115.     Dim oOBJ As Object
    116.     Dim oItem As Object
    117.     Dim i As Integer
    118.     Dim bFound As Boolean
    119.    
    120.     'CHECK EACH ITEMS ENTRYID PROPERTY FOR A MATCH (.FIND NOT COMPATIBLE WITH .ENTRYID PROPERTY)
    121.     bFound = False
    122.     For i = 1 To oInbox.Items.Count
    123.         Set oItem = oTasks.Items(i)
    124.         If oItem.EntryID = oEmailEntryID Then
    125.             bFound = True
    126.             Exit For
    127.         Else
    128.             bFound = False
    129.         End If
    130.         Set oItem = Nothing
    131.     Next
    132.    
    133.     If bFound = True Then
    134.         Select Case CLng(sType)
    135.             Case olTask
    136.                 sType = "Task"
    137.             Case olTaskRequestAccept, olTaskRequestDecline, olTaskRequest, olTaskRequestUpdate
    138.                 sType = "TaskItem"
    139.             Case Else
    140.                 sType = ""
    141.         End Select
    142.         Select Case sType
    143.             Case "Task", "TaskItem"
    144.                 Set oOBJ = oTasks.Items(i)
    145.                 If TypeName(oOBJ) <> "Nothing" Then
    146.                     oOBJ.Display 'THE USE CAN MAKE CHANGES AND SAVE THEM TO OUTLOOK
    147.                 Else
    148.                     MsgBox sType & " not found in Outlook!", vbOKOnly + vbExclamation
    149.                 End If
    150.             Case Else
    151.                 MsgBox "Invalid Outlook Item Type Passed!", vbOKOnly + vbCritical
    152.         End Select
    153.     Else
    154.         MsgBox "Item not found in Outlook!", vbOKOnly + vbInformation
    155.     End If
    156.     Exit Function
    157.    
    158. No_Bugs:
    159.  
    160.     MsgBox Err.Number & " = " & Err.Description, vbOKOnly + vbExclamation
    161.     Exit Function
    162.     Resume
    163. End Function
    Last edited by RobDog888; Oct 16th, 2003 at 03:49 PM.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width