How to move incoming mail to specific folder based on the subject? - vba

How do I move incoming mail from inbox to specific folder based on the subject name?
If Subject contains Urgent word then move this mail to QuickLook subfolder. Not needed for existing mails in Inbox.
I want to use run as script in my rules wizard for a new rule so it applies to every message that I receive in Inbox.
I know I can achieve it by Outlook rules but need this as macro as per my requirement.

I got the answer. Save the below script as Module.
Public Sub MoveUrgentMails(myItem As Outlook.MailItem)
Dim myNameSpace As Outlook.NameSpace
Dim myInbox As Outlook.Folder
Dim myDestFolder As Outlook.Folder
Set myNameSpace = Application.GetNamespace("MAPI")
Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
Set myDestFolder = myInbox.Folders("QuickLook")
If InStr(myItem.Subject, "Urgent") > 0 Then
myItem.Move myDestFolder
End If
End Sub

Related

VBA: How to iterate through Outlook Inbox subfolder

I am attempting to pull all emails from an Outlook inbox subfolder and then pull the subject of each mail item.
I am defining the folders as such:
'Outlook folder- deifinitions
Dim olApp As Outlook.Application
Set olApp = New Outlook.Application
Dim olNs As Outlook.Namespace
Set olNs = olApp.GetNamespace("MAPI")
Dim olRecip As Outlook.Recipient
Set olRecip = olNs.CreateRecipient("email#outlook.com")
Dim Inbox As Outlook.MAPIFolder
Set Inbox = olNs.GetSharedDefaultFolder(olRecip, olFolderInbox)
Dim InboxSubfolder As Outlook.Folder
Set InboxSubfolder = Inbox.Folders("To Be Logged")
I am getting
Run-time Error '-2147221219 (8004011d)' The operation failed because
of a registry or installation problem. Restart Outlook and try again.
Debug always highlights this line.
Set Inbox = olNs.GetSharedDefaultFolder(olRecip, olFolderInbox)
This is on a fresh install of both Windows and Outlook. I have restarted both Outlook and the computer. Any suggestions?
If the Inbox folder comes from the account configured in Outlook you need to use the NameSpace.GetDefaultFolder method which returns a Folder object that represents the default folder of the requested type for the current profile; for example, obtains the default Inbox folder for the user who is currently logged on.
Dim olApp As Outlook.Application
Set olApp = New Outlook.Application
Dim olNs As Outlook.Namespace
Set olNs = olApp.GetNamespace("MAPI")
Dim Inbox As Outlook.MAPIFolder
Set Inbox = olNs.GetDefaultFolder(olFolderInbox)
Dim InboxSubfolder As Outlook.Folder
Set InboxSubfolder = Inbox.Folders("To Be Logged")
Also you may find the required store in Outlook in case of multiple accounts configured using the Store.GetDefaultFolder method which returns a Folder object that represents the default folder in the store and that is of the type specified by the FolderType argument.
This method is similar to the GetDefaultFolder method of the NameSpace object. The difference is that this method gets the default folder on the delivery store that is associated with the account, whereas NameSpace.GetDefaultFolder returns the default folder on the default store for the current profile.

Outlook VBA Access Folders in Shared Mailbox

I have some VBA code in Outlook which behaves perfectly for the main Mailbox - however the same code is struggling when I add a secondary mailbox - this is Outlook 2016.
It seems to be struggling with reading the sub folders - I can get it to read mail items in the Inbox, but not the sub folders.
Code :
Dim sharedemail As Outlook.Recipient
Dim myOlApp As New Outlook.Application
Dim myNameSpace As Outlook.NameSpace
Dim myInbox As Outlook.MAPIFolder
Dim myDestFolder Outlook.MAPIFolder
Dim strSubject As String
Dim i As Integer
Set myNameSpace = myOlApp.GetNamespace("MAPI")
Set sharedemail = myNameSpace.CreateRecipient("recip#domain.com")
Set myInbox = myNameSpace.GetSharedDefaultFolder(sharedemail, olFolderInbox)
For itemCount = myInbox.items.Count To 1 Step -1 'Iterates from the end backwards
Set item = myInbox.items(itemCount)
strSubject = UCase(item.Subject)
Select Case True
Case InStr(strSubject, UCase("Holiday Request")) > 0
'Set destination folder
Set myDestFolder = myInbox.Folders("HolidayRequests")
'move the email out of inbox
item.Move myDestFolder
End Select
Next
It stalls at the Set myDestFolder line as it can't seem to select that sub folder - as I say same code seems to work fine in main Inbox?
Thanks
Keep in mind that Outlook keeps shared default folders in the primary OST file, and it does not sync the subfolders.
You can either
Uncheck the "Download shared folders" checkbox
Use Extended MAPI (C++ or Delphi) - that would be fairly complex as you'd need to retrieve the autodiscover XML for that mailbox and construct the store entry id appropriately.
use Redemption (I am its author) - its version of RDOSession.GetSharedDefaultFolder returns an online version of the folder (RDOFolder) with all its subfolders.

outlook shared mailbox contacts in a excel list with VBA

I'm looking for a vba code in excel, he should contacts from a shared mailbox (different exchange account called shared mailbox in outlook) transfer to a excel list, with the normal contacts from outlook it works I used this:
Set nsOutlook = applOutlook.GetNamespace("MAPI")
Set cfOutlook = nsOutlook.GetDefaultFolder(olFolderContacts)
with Set olcontacts = cfOutlook.Folders("name")
I have used the "name" where is displayed in outlook
it not works he not find the folder.
is there a solution without recipient?
because there will be several user-all with the same shared mailbox
I hope you can help me.
The Outlook object model provides the GetSharedDefaultFolder method of the Namespace class which returns a Folder object that represents the specified default folder for the specified user. For example:
Sub ResolveName()
Dim myNamespace As Outlook.NameSpace
Dim myRecipient As Outlook.Recipient
Dim CalendarFolder As Outlook.Folder
Set myNamespace = Application.GetNamespace("MAPI")
Set myRecipient = myNamespace.CreateRecipient("Dan Wilson")
myRecipient.Resolve
If myRecipient.Resolved Then
Call ShowCalendar(myNamespace, myRecipient)
End If
End Sub
Sub ShowCalendar(myNamespace, myRecipient)
Dim CalendarFolder As Outlook.Folder
Set CalendarFolder = myNamespace.GetSharedDefaultFolder(myRecipient, olFolderCalendar)
CalendarFolder.Display
End Sub

vba move email to folder if subject is like?

I'm using the following code to move emails from my inbox to another folder called Supplier.
It currently works if the subject is 'Introduction' but not if the subject is 'my introduction'
what I want to do is add a line that says if subject or body is LIKE 'introduction' or is Like 'introduce' or Like 'Supply' etc...
Also I have multiple accounts in my outlook, at the moment this code only works for my default account, but I want it to work for my account called 'Purchasing#Hewden.co.uk', is there a way I can change this? my 'supplier' folder is within the inbox of my purchasing#hewden.co.uk account and I want to move the email from this inbox to the supplier folder.
Sub MoveItems()
Dim myNameSpace As Outlook.NameSpace
Dim myInbox As Outlook.Folder
Dim myDestFolder As Outlook.Folder
Dim myItems As Outlook.Items
Dim myItem As Object
Set myNameSpace = Application.GetNamespace("MAPI")
Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
Set myItems = myInbox.Items
Set myDestFolder = myInbox.Folders("Supplier")
Set myItem = myItems.Find("[Subject] = 'Introduction'")
While TypeName(myItem) <> "Nothing"
myItem.Move myDestFolder
Set myItem = myItems.FindNext
Wend
End Sub
I know that the question asked for code however this can be done much easier through the use of rules. If this answer is not wanted I will delete it.
Anyway you should read up on this and this to learn about rules, they are truly awesome.
You're going to want to create a new rule and have it filter based on a keyword within the subject line, you can just write 'introduction' and Outlook will know to look for that word.
You also have to specify where the email goes and, in my case, you also need to specify what kind of notification (if any) is displayed when new mail arrives.
All of this should be available under Rules -> Create New Rule.

microsoft outlook macro to move incoming emails from inbox to a particular folder

May I know what code should I put in the macro to be able immediately transfer the files from my inbox to another particular folder after I've seen it come in my inbox? I do not wish to automatically forward it to another folder, I want it happen once I've pressed a particular combination of keys. Help please? Am not well-adept with Visual Basic?
You don't need a macro to do this, it can be accomplished with rules.
I'm assuming Outlook 2013, but this will basically apply to most versions:
Go to Inbox > Rules > Create Rule > Advanced Options
Checkmark 'Where my name is in the To box' (or any other option you choose)
Click Next
Checkmark 'move it to a specified folder', and then click the blue url on the name 'specified folder', and choose the folder
Click Next
Choose any other rules you want, and click Next
Verify your rule setup, and click Finish
' http://msdn.microsoft.com/en-us/library/office/ff860683(v=office.15).aspx
Sub MoveMessageToTestFolder()
' Works on one selected item
Dim myNameSpace As Outlook.Namespace
Dim myInbox As Outlook.Folder
Dim myDestFolder As Outlook.Folder
Dim myItem As Object
Set myNameSpace = Application.GetNamespace("MAPI")
Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
' Add As many .Folders("SubfolderName") as needed
Set myDestFolder = myInbox.Folders("Test")
Set myItem = Application.ActiveExplorer.Selection.Item(1)
If TypeOf myItem Is mailitem Then
myItem.Move myDestFolder
End If
Set myNameSpace = Nothing
Set myInbox = Nothing
Set myDestFolder = Nothing
Set myItem = Nothing
End Sub
http://www.slipstick.com/developer/how-to-use-outlooks-vba-editor/
http://www.howto-outlook.com/howto/macrobutton.htm