Getting reference to Outlook mailbox root in a macro - vba

I'm trying to move items into a specific folder.
I'm able to move the items into a child folder of the inbox using the typical
myNameSpace.GetDefaultFolder(olFolderInbox).Folders("myfolder")
I want to move items into an arbitrary folder. This currently works fine;
myNameSpace.Folders("Mailbox - Main").Folders("myfolder")
But I don't want to hardcode the path. I want to put this macro on a couple of systems and was hoping to avoid writing a unique version for each user.

If you want the root of the default store, use the Inbox parent:
myNameSpace.GetDefaultFolder(olFolderInbox).Parent.Folders("my‌​folder")

Related

Outlook VBA: how to retrieve the folder(s) from the taskbar Move menu list?

I am trying to retrieve via VBA the last entry (or all entries) of the directories list shown in the Move menu of Outlook. This menu is normally visible in the Home taskbar. The objective is thereafter to re-use this directory for instance to "change focus window to the last used folder" (from this list).
I have checked in the Microsoft documentation but could not find it, though I might have missed it.
I also didn't find it in the OlDefaultFolders list, which is logical since it's not a folder in the tree view anyway, but only a list of recently used folders.
I did look into this thread but the objective of the question author seems a bit different as there is no question of the Move menu: Get the folder where the last mailitem was moved in Outlook?
Did anyone had the same issue or find a way to circumvent this problem?
Maybe to store the last used folder elsewhere each and every time an email is moved... a bit overkill but that could work I guess.
Thanks in advance for your help!

How to Move Emails Containing Name of a Folder?

We manually create a folder each time we get a new client.
I'd like if an email contains a keyword that matches the name of a folder, then move the email to said folder.
E.g.
If email in inbox contains keyword "Apple" and an "Apple" folder exists, then move email to the "Apple" folder. Else nothing.
Similarly, if email in inbox contains keyword "Google" and a "Google" folder exists, then move the email to the "Google" folder. Else nothing.
Without having to set rules for each new folder that is created.
Yes, it is possible. In VBA you can scan all folders for email with a specific keyword and if a corresponding folder exists in Outlook you could move it to that folder. Let's consider what needs to be done for that. First, you need to scan all folders. The AdvancedSearch method of the Application class allows to perform a search based on a specified DAV Searching and Locating (DASL) search string. The key benefits of using the AdvancedSearch method in Outlook are:
The search is performed in another thread. You don’t need to run another thread manually since the AdvancedSearch method runs it automatically in the background.
Possibility to search for any item types: mail, appointment, calendar, notes etc. in any location, i.e. beyond the scope of a certain folder. The Restrict and Find/FindNext methods can be applied to a particular Items collection (see the Items property of the Folder class in Outlook).
Full support for DASL queries (custom properties can be used for searching too). To improve the search performance, Instant Search keywords can be used if Instant Search is enabled for the store (see the IsInstantSearchEnabled property of the Store class).
You can stop the search process at any moment using the Stop method of the Search class.
Read more about that in the Advanced search in Outlook programmatically: C#, VB.NET article.
So, you could run a search for items in the background and then at some point when the search is completed you may be notified.
To find the target folder you could iterate over all folders in Outlook recursively. See Enumerate folders for more information on that.
Finally, to move items you can use the Move method which moves a Microsoft Outlook item to a new folder.

Outlook automatic archive on network drive

Am I able to use Outlook to automatically link a folder in outlook with a folder on the network drive?
Let's say I want to use VBA to modify a folder called "New York Store" within outlook. I would like to modify the folder, so whenever I put an email into this folder, it will save the email on a destination on the computer such as "X:\Stores\Outlook Archive\New York Store", and when it has done so, it permanent delete the email from Outlook?
Is it possible to modify a folder (by VBA or something else) to save the email(s) within a folder, and when it has done so, delete the emails from Outlook permanently?
Please let me know, if this is possible :)
To answer your question: Yes. It is possible. You'll need to brush up on programmatic folder control and find a way to "watch" the folder for new items.
Is there a reason that there has to be a folder involved? You'd save a few steps with something like a custom button on the Mail Item ribbon to save to file and delete message.
Or, considerably fewer keystrokes:
F12EnterDel
It will work on one or more items, no additional folder required, and after the first time you save an item at your location X:\Stores\Outlook Archive\New York Store it will default to that folder.

VB Code to Close Open Subfolders in Outlook

Outlook has an habit of opening sub-folders if an email has been sent to it automatically (through rules). There is no way to turn off this feature.
Does anyone have ideas for code that would periodically (say every 30 seconds), automatically collapse all subfolders within, say, the sent items folder?
Thx
The Outlook object model doesn't provide anything for collapsing folders in the navigation pane.
The Starting Outlook with all folders collapsed/expanded states the following:
To keep the mailbox collapsed even when a new message is being delivered, make sure that your Inbox and other folders that receive email (for instance by a rule) have been added to your Favorites list.

Finding the type of Outlook.MAPIFolder

I'm building an outlook control for an application, and am populating a treelist by recursively adding child folders. These folders are declared as Outlook.MAPIFolder. But the application only allows import from actual emails, so I want to exclude folders containing calendar items. I can right click on those folders in outlook, go to properties, and see type as "Folder containing Calendar Items". But I don't seem to be able to get at that programmatically in VB.Net. Am I missing something simple?
If you only want mail folders just check whether the folder's DefaultItemType property is olMailItem or olPostItem.
DefaultItemType:
http://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook.mapifolder.defaultitemtype.aspx
OlItemType Enumeration:
http://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook.olitemtype.aspx
The MessageClass on the object will tell what type of object you are dealing with. The MessageClass also determines which folder the object will live in and what form will be displayed to render it.
Here is a reference:
http://msdn.microsoft.com/en-us/library/aa171490(office.11).aspx
HTH