0

I'm having issues accessing emails in a specific folder within Outlook.

When I put the emails in the Inbox, the following line of code works.

Set OtlkApp = GetObject(, "Outlook.application")
Set ns = OtlkApp.GetNamespace("MAPI")
Set fldr = ns.GetDefaultFolder(olFolderInbox)

However, when I try to access a folder called "tempfiles", it could not find the object when I tried opening it using this code.

Set fldr = ns.Folders("tempfiles")

Is the syntax correct? Am I missing something?

Community
  • 1
  • 1
santaaimonce
  • 134
  • 2
  • 16
  • I know of no simple assignment statement that will allow you to access a randon folder. You might have many folders with the same name; how will Outlook know which you want. There are a pair of macros at the bottom of this answer of mine which will get any folder within the hierarchy. [How to copy Outlook mail message into excel using VBA or Macros](http://stackoverflow.com/a/12146315/973283). – Tony Dallimore Feb 07 '15 at 22:00

1 Answers1

2

Depending on the level of tempfolders (same level or subfolder of Inbox) you extend the folders property:

'INBOX FOLDER
Set inboxfldr = ns.GetDefaultFolder(olFolderInbox)

'TEMPFILES SUBFOLDER
Set tempfilesfldr = inboxfldr.Folders("tempfiles")

'TEMPFILES FOLDER (SAME LEVEL AS DEFAULT FOLDERS -INBOX, CALENDAR, TASKS, ETC.)
Set tempfilesfldr = inboxfldr.Parent.Folders("tempfiles")
Parfait
  • 104,375
  • 17
  • 94
  • 125