PDA

View Full Version : [SOLVED] Set an object as the outlook inbox



lcblank
06-18-2015, 11:41 AM
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.

snb
06-18-2015, 02:31 PM
With createobject("Outlook.application").GetNamespace("MAPI").GetDefaultFolder(6)
msgbox .items.count
End With

SamT
06-18-2015, 02:51 PM
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

lcblank
06-18-2015, 04:47 PM
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?

SamT
06-18-2015, 05:03 PM
I can rate you guys
Click the Star at the bottom of posts.

olFolderInbox is a Constant = 6 :dunno

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.

lcblank
06-18-2015, 05:17 PM
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.

lcblank
06-18-2015, 05:34 PM
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?

SamT
06-18-2015, 06:09 PM
ns.Folders("gmail Inbox")
Or maybe

olInbox.Folders("gmail inbox") 'Case Sensitive

:dunno I don't use or code in Outlook (yet.) I'm just reading the helps.

lcblank
06-18-2015, 07:58 PM
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!

snb
06-19-2015, 12:22 AM
You might have a look over here:

http://www.snb-vba.eu/VBA_Outlook_external_en.html#H1