Using visual basic to access subfolder in Inbox? - vba

Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
Set myOlItems = objNS.GetDefaultFolder(olFolderInbox).Items
I have used the code above to access the main outlook Inbox but how to access the folders in inbox and it's mail using vba!

Thats very close :)
To get all the mail items in a folder called "temp" under the Inbox try this
Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Dim olFolder As Outlook.MAPIFolder
Dim msg As Outlook.MailItem
Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
Set olFolder = objNS.GetDefaultFolder(olFolderInbox)
Set olFolder = olFolder.Folders("Temp")
For Each msg In olFolder.Items
Debug.Print msg.Subject
Next

And to drill further down, keep adding Set olFolder lines:
Set olFolder = objNS.GetDefaultFolder(olFolderInbox)
Set olFolder = olFolder.Folders("temp")
Set olFolder = olFolder.Folders("temp2")
Set olFolder = olFolder.Folders("temp3")
Gets you to \Inbox\temp\temp2\temp3\

I found that there were some items in my inbox that were not mail items causing the script to halt.
This little change allowed the script to keep running if something like a meeting invite is found:
Sub getmail()
Dim olApp As Outlook.Application
Dim objNS As Outlook.Namespace
Dim olFolder As Outlook.MAPIFolder
'Dim msg As Outlook.MailItem
Dim InboxItem As Object
Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
Set olFolder = objNS.GetDefaultFolder(olFolderInbox)
Set olFolder = olFolder.Folders("temp")
For Each InboxItem In olFolder.Items
Debug.Print InboxItem.Subject
Debug.Print InboxItem.EntryID
Next
End Sub
Thanks for your answer! helped me a lot!
(My apologies - wanted to comment, but don't have enough rep..)

Related

Why is the creation time property of my Outlook items returning an arbitrary date?

when I run the below module, I get a message box displaying "1/1/4051".
Sub CreateNoteItem()
Dim olApp As Outlook.Application
Dim olNS As Outlook.NameSpace
Dim olNoteItm As Outlook.NoteItem
Set olApp = Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
Set olNoteItm = olApp.CreateItem(olNoteItem)
MsgBox olNoteItm.CreationTime
End Sub
I was expecting a date value such as 44701, etc., or a string similar to when I display "Now" (m/dd/yyyy hh:mm:ss).
This happens not just for note items, but also mail items. My end goal is to use this creation time to then process any items with later creation times.
Thanks!
Call the Save method before getting any date-specific properties.
Sub CreateNoteItem()
Dim olApp As Outlook.Application
Dim olNS As Outlook.NameSpace
Dim olNoteItm As Outlook.NoteItem
Set olApp = Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
Set olNoteItm = olApp.CreateItem(olNoteItem)
olNoteItm.Save
MsgBox olNoteItm.CreationTime
End Sub

How to search for a folder using text in folder.description?

I want to find an Outlook folder using folder.description value.
In folder.description I have more than one value. The code should take only one.
Private Sub CLemailbackupsaved_Click()
Dim olApp As Outlook.Application
Dim olNS As Outlook.Namespace
Dim olFldr As Outlook.MAPIFolder
Dim objfolder As Outlook.MAPIFolder
Dim olItem As Object
Dim olMailItem As Outlook.MailItem
Dim intx As Long
'Dim reportid As String
Set olApp = New Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
Set olFldr = olNS.Folders("a#a.com")
Set olFldr = olFldr.Folders("Inbox")
Debug.Print olFldr.Name
For intx = 1 To olFldr.Folders.Count
If olFldr.Folders.Item(intx).Description = "* MR090 *" Then
Set objfolder = olFldr.Folders.Item(intx)
Exit For
End If
Next
Debug.Print objfolder.Name
Set olNS = Nothing
Set objfolder = Nothing
Set olFldr = Nothing
Set olApp = Nothing
End Sub
Folder.description example value:
MR091 MR090

VBA Save active email to subfolder in inbox

Im stuck with problem to move active email to subfolder in inbox.
Need to replace ("xxxx#xxx.xxx") to something as olFolderInbox or inbox, etc without type specific email adress in VBA code.
Dim objMail As Outlook.MailItem
Dim objNS As Outlook.NameSpace
Dim objFolderItem As Outlook.Folder
Set objNS = Application.GetNamespace("MAPI")
Set objFolderItem = objNS.Folders.Item("xxxx#xxx.xxx").Folders.Item("tmp")
objMail.Move objMoveItem
Try this
Option Explicit
Public Sub Exampls()
Dim olNs As Outlook.NameSpace
Set olNs = Application.GetNamespace("MAPI")
Dim Inbox As Outlook.MAPIFolder
Set Inbox = olNs.GetDefaultFolder(olFolderInbox)
Dim Item As MailItem
Set Item = ActiveExplorer.selection(1)
Item.Move Inbox.Folders("Temp")
End Sub

Copy a personal contact list to a public folder

I made code to copy a contact list to a public folder but if I am not on the contact source it does not work.
Sub Movecopycontacts()
Dim objOutlook As Outlook.Application
Dim objNamespace As Outlook.NameSpace
Dim objSourceFolder As Outlook.MAPIFolder
Dim objDestFolder As Outlook.MAPIFolder
Dim objItem As ContactItem
Set objOutlook = Application
Set objNamespace = objOutlook.GetNamespace("MAPI")
Set objSourceFolder = objNamespace.GetDefaultFolder(olFolderContacts)
Set objItem = Application.ActiveExplorer.Selection.Item(1)
Set objDestFolder = objNamespace.Folders("Public folder - oky#test.com").Folders("all public folder").Folders("test")
objItem.Move objDestFolder
Set objDestFolder = Nothing
End Sub
The error comes from:
Set objItem = Application.ActiveExplorer.Selection.Item(1)
That is correct - your code assumes that the item to be moved is selected. It does what it is supposed to do.
\What else do you want it to do?

Reference a folder by name

I need to get a folder by name, not by folder number counts. I tried getting with various methods.
Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
'Dim OlFolder As Outlook.MAPIFolder
Dim objFolder As Outlook.Folder
Dim myolItems As Outlook.Items
Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
'Set myOlItems = objNS.GetDefaultFolder(37).Folders("Vijay Baswal").Items
'Open the folder
Set objFolder = olApp.Session.GetDefaultFolder("Vijay Baswal")
Say under the Inbox was a folder named Clients and under that was a folder named Vijay Baswal
Set objFolder = objNS.GetDefaultFolder(olFolderInbox).Folders("Clients").Folders("Vijay Baswal")
OlDefaultFolders Enumeration http://msdn.microsoft.com/en-us/library/office/bb208072(v=office.12).aspx
The Inbox is olFolderInbox or 6. Appears there is no 37.
see below vba snippet to check how to read mail from specific folder
Dim olApp As Outlook.Application
Dim olNs As Outlook.NameSpace
Dim olFldr As Outlook.MAPIFolder
Dim olItms As Outlook.Items
Dim olMail As Variant
Dim outFolder As Outlook.Folder
Dim olItem As Outlook.MailItem
Dim i As Long
Set olApp = New Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
Set olFldr = olNs.Folders("folder1").Folders("fol2")
Set olItms = olFldr.Items
olItms.Sort "Subject"
i = 1
For Each olItem In olItms
'If InStr(olMail.Subject, "Criteria") > 0 Then
Dim szVar As String
szVar = olItem.Body
szVar1 = olItem.Subject
i = i + 1
'End If
Next olItem
Set olFldr = Nothing
Set olNs = Nothing
Set olApp = Nothing