Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I need to extract emails from a secondary Outlook account into access, but it works fine on all my folders except the folder I need to extract. I've compared permissions on both secondary account folders and they are the same! I've checked the trust and unchecked the download.

Dim objApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Dim colFolders As Outlook.Folders
Dim objFolder As Outlook.MAPIFolder
Dim objParentFolder As Outlook.MAPIFolder
Dim objMailItem As Outlook.MailItem
Dim strSQL As String
Dim strBody As String
Dim strSubject As String
Const strDoubleQuote As String = """"
Dim strFolderPath As String
Dim i As Byte
On Error GoTo Form_Load_Error

Set objApp = New Outlook.Application
Set objNS = objApp.GetNamespace("MAPI")

For i = 1 To objNS.Folders.Count
    Set objParentFolder = objNS.Folders(i)
    
    '';I use OutlookFolderNames to locate the folder in the user’s Outlook
    Set objFolder = OutlookFolderNames(objParentFolder, "Inbox")
    
    ' **Note that the objFolder shows that OutlookFolderNames found the "Inbox" as valid. I can change Inbox for any other folder and it works fine**
    If Not objFolder Is Nothing Then
        'Once the folder is located exit For
        Exit For
    End If
Next i

If Not objFolder Is Nothing Then
    For Each objMailItem In objFolder.Items
        With objMailItem

Set objFolder = Nothing
Set objNS = Nothing
Set objApp = Nothing
            
ExitProcedure:
     On Error GoTo 0
     Exit Sub
            
Form_Load_Error:
     MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Form_Load of VBA Document Form_frmImportEmails """

I wanted to post the function but I got a error saying there was TOO MUCH code!

question from:https://stackoverflow.com/questions/65835410/extract-emails-from-outlook-using-access-vba-not-working-on-only-one-folder

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
802 views
Welcome To Ask or Share your Answers For Others

1 Answer

The function to get the objFolder was picking up an inbox from another secondary (objParentFolder) even though none are visible in the actual Outlook screen. It must be a default action to have an inbox for each. All I needed to do was check if the objParentFolder = the correct secondary, then it checked for the 'Inbox' associated with it. I was thrown off by the fact no other inbox shows up in the secondaries when I open and look at outlook itself. They are hidden.

Thanks for your time Pete


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...