How to Move Emails Containing Name of a Folder? - vba

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.

Related

Add a hidden tag to outgoing mail

I create a mail by VBA when sending invoices and automated newsletters by Outlook (2019).
For reimporting the mails from the Sent folder into the database it would be convenient to read a hidden tag including the customer number which I would like to embed into the mail.
You can add a custom property either using MailItem.UserProperties.Add or by setting your custom property directly using MailItem.PropertyAccessor.SetProperty.
Keep in mind adding a user property can force Outlook to send in the RTF format (the infamous winmail.dat attachment), so the latter solution is preferable. You just need to make sure you get the DASL property name (to be used with MailItem.PropertyAccessor.SetProperty) right. You can use the former approach (MailItem.UserProperties.Add), take a look at the message in the Sent Items folder with OutlookSpy (I am its author - click IMessage button), then use the DASL property name replacing MailItem.UserProperties.Add with MailItem.PropertyAccessor.SetProperty

How to VBA Code To Create A Search Folder That Looks For Message Class?

we have a Document Management System that when an email is sent or recieved and is filed it gets given a Message class that contains EMS, I want a code that will create a search folder that will search "Sent Items" for a message class that contains EMS?
You can use the AdvancedSearch method. The AdvancedSearch method and related features in the Outlook object model do not create a Search Folder that will appear in the Outlook user interface. However, you can use the Save method of the Search object that is returned to create a Search Folder that will appear in the Search Folders list in the Outlook user interface.

How to email dropbox app file paths as clickable hyperlinks?

The company uses dropbox instead of local servers. We often send files to colleagues, but each user has a unique path to the file stored in the dropbox desktop app (e.g. C:\Users\username\Dropbox...).
I was thinking about writing a batch script to copy a file path and strip the beginning of the path. I would then need another script to add the user specific prefix to the file path, and then open the folder/file in file explorer.
I believe I found a way to add a batch file into the menu that pops up when you right click a file/folder by editing the registry. I was thinking it would be possible to write an Outlook add-in for the receiving user to prepend their drive/username etc. to the file path and then open the file/file explorer if a path to a folder is sent.
I was hoping to get some input on this approach, or to know if there is an easier way to handle this. I haven't started any of the code yet.
Why don't you just start using the Dropbox add-in for Outlook?
Dropbox has created an add-in that integrates with Microsoft Outlook. By connecting the Dropbox add-in to your Outlook account, you can:
Replace email attachments with a shared link to any file, big or small
Save email attachments that you receive directly to your Dropbox account
Ensure those with access to a file see updates to it (since shared links always point to the latest version of a file, whereas attachments are just a static copy)
Enabling the integration adds a Dropbox icon to the Outlook compose window. When you click this icon a pop-up window appears, allowing you to select from the contents of your Dropbox. A shared link to any selected file or folder will appear in the body of the email.
While this link looks like an attachment, it's, in fact, a shared link. This means no slow-down in sending emails, and no space limitations in your inbox.
Anyway, if you still want to develop an add-in for extracting an attachment's file path you need to start with Walkthrough: Create your first VSTO Add-in for Outlook.
The NewMailEx event of the Application class which is fired once for every received item that is processed by Microsoft Outlook. The item can be one of several different item types, for example, MailItem, MeetingItem and etc. The EntryIDsCollection string contains the Entry ID that corresponds to that item.
The NewMailEx event fires when a new message arrives in the Inbox and before client rule processing occurs. You can use the Entry ID returned in the EntryIDCollection array to call the NameSpace.GetItemFromID method and process the item. Use this method with caution to minimize the impact on Outlook performance.

Automatically take specific data out of specific emails and put it into an excel form

I'm completely new to VBA.
Is it possible to write some code that would search for emails from a specific sender and search inside that email for a specific line in a table, which it would then put into an already existing excel form?
Any advice on where to get started learning this or if it's even possible would be greatly appreciated.
it is possible to make that. You have three types to search something like that.
You have the View, AdvancedSearch and the Explorer.Search.
The View is only for one folder.
The Filter for the View is called Distributed Authoring Search and Location (DASL). Here is a good link for that filter type: https://msdn.microsoft.com/en-us/library/ms874577(v=exchg.65).aspx
The AdvancedSearch is like the View, but you can search over all folders and all nameboxes. BUT there is problem! When you use AdvancedSearch you have to create an folder and in this folder are your results. The filter of the folder is unchangebel, so after creading you cant change anything. The folder will update Automaticly if there are new Mail or something else. The AdvancedSearch uses the same filter as View.
The last one is the Explorer.Search. With that search you can search like the AdvancedSearch, but you have not to create an folder. This one uses the Advanced Query Syntax (AQS). Here is a good link for that filter type: https://msdn.microsoft.com/en-us/library/cc513841(v=office.12).aspx Each of the searches can filter for all typs of objects. So you can Search for things in Mails.
Text in mails is Called in..
- DASL textdescription.
- AQS contents.
I hope I could help you by starting.

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.