Client-side rules won't run if the mailbox is not open in Outlook and you can't run rules on a shared mailbox in your profile. However, if the shared mailbox is in your profile you can use an ItemAdd macro to watch the folder for new items and process them as they arrive.
This sample watches a shared mailbox for new items, adds a category to the item then forwards it to another address.
To get the correct mailbox name to use, right-click on the shared Inbox and choose Properties. The mailbox name is in the Location field.
Use the name only, not the \\.
Private WithEvents olInboxItems As Items Private Sub Application_Startup() Dim objNS As NameSpace Set objNS = Application.Session ' Get function fromhttp://slipstick.me/qf Set olInboxItems = GetFolderPath("Mailbox name in folder list\Inbox").Items Set objNS = Nothing End Sub Private Sub olInboxItems_ItemAdd(ByVal Item As Object) On Error Resume Next Item.Categories = "My Category" Item.Save Set myForward = Item.Forward 'this puts the name in the To field myForward.Recipients.Add "me@domain.com" myForward.Display ' use .Send to send it automatically End Sub
Don't forget to check your macro security settings; it needs to be set to Low for testing.
To test the macro without restarting Outlook, click in the Application_Startup macro and press Run.
How to use macros
First: You will need macro security set to low during testing.
To check your macro security in Outlook 2010 or 2013, go to File, Options, Trust Center and open Trust Center Settings, and change the Macro Settings. In Outlook 2007 and older, it’s at Tools, Macro Security.
After you test the macro and see that it works, you can either leave macro security set to low or sign the macro.
Open the VBA Editor by pressing Alt+F11 on your keyboard.
To put the code in a module:
- Right click on Project1 and choose Insert > Module
- Copy and paste the macro into the new module.
More information as well as screenshots are at How to use the VBA Editor
I have put the above codes by changing the shared mailbox name and forward too section however it gives an error stating "Only Valid in object Module" for "Private WithEvents olInboxItems As Items" . Please help.
I also need a macro where in all attachments can be downloaded from shared mailbox.
Hi Diane, My question is a bit more complex. I have a macro that uploads attachments from a shared mailbox into a master file on the network. Because there is no reliable method of determining which emails have the necessary attachments, the user sets a custom Category on the required emails. The macro loops through the mailbox, uploads attachments from the categorized emails, then switches the custom Category to "Upload Complete". All of this works perfectly... Unless... If another user happens to have one of the categorized emails open, the macro adds the "Upload Complete" category to the email, but cannot remove the "Ready For Upload". This causes the "Ready For Upload" Category count to hang at 1 and the macro loops forever. Note: I cannot simply count the "Ready For Upload" emails because the Email.Save process which is switching the category interrupts For Each loops so I have to use a Do Until loop. So, is there a way to close or deactivate an email from all users, or identify if another user has an email open, or lock an email by some means so others can't open it during this process? Any assistance you can provide is greatly… Read more »
Well.. you can't close it remoting using vba.
I am assuming redemption won't close it either, but have not looked at it... but if it can be done, I'd expect redemption to do it. http://www.dimastr.com/redemption/rdo_introduction.htm
Hi , I need to build a macro to track the mails moved from one mail box to other in a shared mail box in excel ,Can anyone please help me on this ?
Hi Diane,
Any idea how to automate the process to send the E-mails from Office 365.
Regards
Rajneesh
If its a shared mailbox, you need to have send as permission and use .sentonbehalfof - https://www.slipstick.com/developer/code-samples/send-email-address-vba/
Hi, I am stuck in one of the urgent project delivery. Requirement is: when new mail arrives, mail detail(Subject,received time) should be export into excefl but main issue is that user have 6 different shared inbox and these inbox we have 2 or 3 folder in each.
Outlook event for new mail is not working for these inbox , it is notifying new mails only for main inbox.
Please help!!!
you'll need to add each folder you need to watch to the automatic startup macro and create an items add macro for each one, giving each a different name. It's going to get real messy, real fast watching 12 - 18 folders - start with one folder, test, then add more. Make sure you are using unique object names for each folder too.
if the macro to save to excel is identical for each, the itemadd macros can call the same macro, which will reduce a lot of the mess.
You can set the parent folder once (so you only need 6 of these) - and share that with the subfolders:
Set Aliasfolder = Session.GetDefaultFolder(olFolderInbox)
set aliaisitems = aliasfolder.items
set aliassubfolder1items = aliasfolder.Folders("Sub1").Items
set aliassubfolder2items = aliasfolder.Folders("Sub2").Items
Private Sub aliaisitems_ItemAdd(ByVal Item As Object)
'do whatever
call savetoexel
end sub
Private Sub aliassubfolder1items_ItemAdd(ByVal Item As Object)
'do whatever
call savetoexel
end sub
Private Sub aliassubfolder2items_ItemAdd(ByVal Item As Object)
'do whatever
call savetoexel
end sub
etc
Hi Diana, Thanks for the response. I used "GetFolderPath" function for each folder that i got from your above code and it's working good and very helpful. My project is in progress now and it is in testing phase. Can you please suggest how can we assign the outlook code to button for whole team rather than import and export the module and assign the button one by one for each team memeber system.
Thanks for your support:):)
i swear i answered this the other day... guess i forgot to hit send. :) the only way to share code is either by import/export or copy/paste it or compiling it into an addin.
Hi, Im pretty new to VBA and Outlook, Here is the issue I'm having. We have public folder that each team member has access to and we have our own personal one. I used the following code to move email from Inbox to subfolder in public folder. It works perfectly on my machine but for everyone else the email stays in the Inbox. I cant see the email even if I restart outlook. It is also in the subfolder
Set ns = Application.GetNamespace("MAPI")
Set objOwner = ns.CreateRecipient("general@u.ca")
objOwner.Resolve
If objOwner.Resolved Then
Set newCalFolder = ns.GetSharedDefaultFolder(objOwner, olFolderInbox)
Set objDestFolder = newCalFolder.Folders("Myfolder")
End If
Set objItem = GetCurrentItem()
With objItem
.FlagStatus = olFlagComplete
.Move objDestFolder
End With
Any help is greatly appreciated
Did the other users add the shared mailbox in their profile the same way you did? Do they get any error messages?
Hi Diane,
I am using a shared inbox and I am trying to create a rule that will move mail to a specific folder based on certain words that appear in the body. In outlook when I try and create the rule it does not let me select the folder of the shared inbox. I can do it using OWA but the issue there is I want to be able to save the rules in case they are deleted in error. Any ideas?
if its a server side rule, you can create a profile for the shared account to create the rules, then go back to your own profile, otherwise you need to use a macro that watches the shared inbox, which is on this page. .
hi Diane,
Cud u plz share the code for watch the new mail arrival on shared inbox.
You'll use the method at https://www.slipstick.com/developer/working-vba-nondefault-outlook-folders/#shared to identify the folder to watch.
it replaces this line -
Set olInboxItems = GetFolderPath("Mailbox name in folder list\Inbox").Items
just make sure you are using the correct object references!
I am getting the sub or function error under
GetFolderPath you please help?