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.
Related
in the navigation pane in Outlook 2016 I can right-click a sub-folder and select "move up" or "move down" to change the order. But I can't find the corresponding VBA command to do this.
Does anyone have an idea where to look? (It's not in Outlook.Folder or Outlook.View, which were my first guesses)
So far I have this:
Sub MoveToTop(newFolderName as String)
Dim nsp As Outlook.NameSpace
Dim newFolder As Outlook.Folder
Dim rootFolder as Outlook.Folder
Set nsp = Application.GetNamespace("MAPI")
Set rootFolder = ns.GetDefaultFolder(olFolderInbox)
Set newFolder = rootFolder.Items.Find (newFolderName)
'now I need something like:
newFolder.MoveUp
End Sub
I am missing the link between the view in the navigation pane and the folders in the MAPI namespace
Thanks a lot,
Bjoern
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.
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.
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
In Outlook 2013 using Developer tab -> Design a Form, I created a custom form (with no mods yet) from the delivered Message form and placed it in my Personal Forms Library. Outlook tells me that the Message class is: IPM.Note.MyForm
I've created a macro and set up a new ribbon button to run the macro. I would like the macro to open a new instance of my custom form, but I can't get it working.
With the following code I can get the macro to open a new instance of the delivered Message Form:
Set newItem = Application.CreateItem(olMailItem)
newItem.Display
Set newItem = Nothing
I can't get it to open my custom form. I've tried the following as arguments to CreateItem: olMailItem.MyForm and IPM.Note.MyForm.
The macro editor intellisense has about 9 options for arguments to CreateItem, all of them appear to be delivered objects/forms, and it errors if one of these options aren't used.
I've done very little vba and office macros, is there some way to get this macro to open my custom form? Thanks.
See Items.Add http://msdn.microsoft.com/en-us/library/office/ff861028(v=office.15).aspx
Sub AddForm()
Dim myNamespace As outlook.NameSpace
Dim myItems As outlook.Items
Dim myFolder As outlook.Folder
Dim myItem As outlook.MailItem
Set myNamespace = Application.GetNamespace("MAPI")
Set myFolder = myNamespace.GetDefaultFolder(olFolderInbox)
Set myItems = myFolder.Items
Set myItem = myItems.Add("IPM.Note.MyForm")
End Sub