Consulting

Results 1 to 10 of 10

Thread: Set an object as the outlook inbox

  1. #1
    VBAX Regular
    Joined
    Jun 2015
    Posts
    7
    Location

    Set an object as the outlook inbox

    I am a nooby and I haven't gotten very far. So, the only help I need is how to declare the inbox as an object.

    Dim oOlAp As Object
     oOlns As Object
     oOlInb As Object
    Dim oOlItm As Object
    
    
    Set oOlAp = GetObject(, "Outlook.application")
        Set oOlns = oOlAp.GetNamespace("MAPI")
        Set oOlInb = oOlns.GetDefaultFolder(olFolderInbox)
    With this code I get a Runtime error'5'

    What am I doing wrong?
    Thank you for the help!

    edit: Sorry to clarify this is from within the excel application.

  2. #2
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    With createobject("Outlook.application").GetNamespace("MAPI").GetDefaultFolder(6)
       msgbox .items.count
    End With

  3. #3
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Ber sure that, in Excel VBA, you have a reference set to a Microsoft Outlook Library, then try this. (Note the names I used. IMO, no VBA sub or function will ever be so long that you need RPN to know what type a variable is.

    Option Explicit '<<< Important at top of all code pages.
    
    Sub SamT()
    Dim outApp As Object
    Dim outNameSpace As Object
    Dim outInBox As Object
    Dim outItem As Object
     
     
    Set outApp = CreateObject("Outlook.Application")
    Set outNameSpace = outApp.GetNamespace("MAPI")
    Set outInBox = outNameSpace.GetDefaultFolder(olFolderInbox)
    
    End Sub
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  4. #4
    VBAX Regular
    Joined
    Jun 2015
    Posts
    7
    Location
    Thank you both for responding! So, I am trying to understand what is going on here. For some reason adding option explicit fixed my run time error 5. I believe it is because the issue was that olFolder.Inbox was not defined which is the error I then received. When I used the number 6 rather than olFolder.Inbox the code ran. However, it says I have 0 messages in my inbox. Here is the code.
    Option Explicit
    
    
    Sub Update()
    
    
    Dim OutApp As Object
    Dim ns As Object
    Dim olInbox As Object
    Dim olItem As Object
    Dim Atmt As Object
    Dim FileName As String
    Dim i As Integer
    
    
    Set OutApp = CreateObject("Outlook.Application")
    Set ns = OutApp.GetNamespace("MAPI")
    Set olInbox = ns.GetDefaultFolder(6)
      
    With olInbox
        MsgBox .items.Count
    End With
     
    End Sub
    Any ideas on why I had to use 6 or to why it says I have nothing in my inbox(which I do)?

    Edit: Is there some sort of system where I can rate you guys or give you good input for helping me?
    Second Edit: So, I see that you can make library references via tools? Is this what I need?
    Last edited by lcblank; 06-18-2015 at 04:59 PM.

  5. #5
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    I can rate you guys
    Click the Star at the bottom of posts.

    olFolderInbox is a Constant = 6

    In VBA press F2 to show the Object Browser. In the top box in the upper left corner, select OutLook. In the Box under that, enter OLDefaultFolders, then click the binoculars. In the bottom Right Pane, click on a Constant and it will show you the value in the bottom left.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  6. #6
    VBAX Regular
    Joined
    Jun 2015
    Posts
    7
    Location
    I can't find Outlook under all libraries. Also, any ideas why its not working correctly? I haven't been able to find the 2013 object library.

  7. #7
    VBAX Regular
    Joined
    Jun 2015
    Posts
    7
    Location
    Ok, sorry about all of the posts. I went ahead and just tested counting in outlook itself and got 0. It seems my gmail inbox that I have set up is not infact the default inbox. How would I refer to this folder?

  8. #8
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    ns.Folders("gmail Inbox")
    Or maybe
    olInbox.Folders("gmail inbox") 'Case Sensitive
    I don't use or code in Outlook (yet.) I'm just reading the helps.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  9. #9
    VBAX Regular
    Joined
    Jun 2015
    Posts
    7
    Location
    So, I figured it out. It ended up being very simple. I guess however gmail is set up, something I don't understand about imap, it creates its own folder. I've never seen anyone reference anything other than the default data file and its folders, but apparently namespace.Folders(2) gave me the gmail folder and I could just access my gmail inbox via from namespace.Folders(2).Folders(1).

    Thanks for all the help!

  10. #10
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645

Posting Permissions

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