vba move email to folder if subject is like? - vba

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.

Related

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

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

How to switch views in Outlook

I'm trying to create an add-in for Microsoft Outlook. I'm at the beginning part of writing the add-in, and what I'd like to have happen is when the user clicks the button I've made, the view switches from whatever they're looking at (either the inbox, calendar, tasks, etc.) to their contacts list.
After much trial and error, this was as far as I got. But I know i'm a ways away.
Dim myNameSpace As Outlook.NameSpace = Nothing
myNameSpace.GetDefaultFolder(OlDefaultFolders.olFolderContacts).Display()
Set Application.ActiveExplorer.CurrentFolder property to the folder in question (e.g. the folder returned by GetDefaultFolder).
Here is it:
Dim contactsFolder as Outlook.Folder = Nothing
Dim myNameSpace As Outlook.NameSpace = Nothing
myNameSpace = Application.GetNamespace("MAPI")
contactsFolder = myNameSpace.GetDefaultFolder(OlDefaultFolders.olFolderContacts)
Application.ActiveExplorer.CurrentFolder = contactsFolder
The CurrentFolder property sets a Folder object that represents the current folder displayed in the explorer.

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.

VSTO Outlook: Cannot find subfolders of shared inbox

I am trying to move mails from any folder to a very specific subfolder of a shared inbox. After trying many different things that did not work I tried to loop through every single folder in the inbox und check if it has the name I am looking for. When I try moving the mailitem, I get the message that the element could not be moved. After searching a little bit longer for the cause I found out, that apparently no folder inside of my inbox exist and the for each loop exits without checking a single entry. So how am I supposed to access a specific subfolder which i only know the name of?
Relevant code:
Private Const destFolder = "myfoldername"
Public Function MoveMail()
SelectedItems = Globals.ThisAddIn.Application.ActiveExplorer.Selection
For Each Item In SelectedItems
Call MoveSelectedMail(Item)
Next Item
End Function
Function MoveSelectedMail(Item As Outlook.MailItem)
Item.Move(GetFolderToMove(destFolder))
End Function
Function GetFolderToMove(ByVal FolderName As String) As Outlook.Folder
Dim NS As Outlook.NameSpace
Dim objOwner As Outlook.Recipient
NS = Globals.ThisAddIn.Application.GetNamespace("MAPI")
objOwner = NS.CreateRecipient("NameofSharedMailbox")
objOwner.Resolve()
If objOwner.Resolved Then
Dim inbox As Outlook.Folder
inbox = NS.GetSharedDefaultFolder(objOwner, OlDefaultFolders.olFolderInbox)
For Each folder As Outlook.Folder In inbox.Folders
MsgBox(folder.Name)
If folder.Name = FolderName Then
Return folder
End If
Next folder
End If
End Function
This is the code I used in VBA but did not work when I started trying to do the same thing as VSTO addin:
Function GetFolderToMove(ByVal FolderPath As String) As Outlook.Folder
Dim NS As Outlook.NameSpace
Dim objOwner As Outlook.Recipient
Set NS = Application.GetNamespace("MAPI")
Set objOwner = NS.CreateRecipient("NameofSharedMailbox")
objOwner.Resolve
If objOwner.Resolved Then
Set GetFolderPath = NS.GetSharedDefaultFolder(objOwner, olFolderInbox).Folders(destFolder)
End If
End Function
What I tried but did not help me solve this problem:
Tried returning only the shared inbox and this worked, however, the inbox is not the folder I want to move the mails to.
In short: I am trying to move a mail to a subfolder of a shared inbox but there seem to be no subfolders according to the error messages.
Hoping you can help me.
Edit:
My Problem might be a little bit out of place as it seems that there might be a problem with the permissions my outlook account has. If the problem is going to be resolved that way, I will update this thread and close ist.
I solved it - I am not entirely sure how exactly this could have happened but it certainly had to do with the permissions I had.

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