I'm running in a little problem with Outlook VBA programming, and would like to know if there's a solution, or if this is just another "known issue".
Context:
I have configured an Outlook e-mail account to access my web email provider through IMAP. In Outlook, I can properly see my web email folders. My provider's spam filter moves spam messages into the Spam folder.
I would like to automatically move messages that get put into the Spam folder into another folder, in my local pst file.
I have it working 99% (through the code provided below for reference).
Issue:
I can see that there are messages in the Spam folder (there is a bold unread message count beside the folder name), but the ItemAdd even will only fire when I click on the folder. At that point, I see the contents of the spam folder, and then see all of the new spam being moved to my local folder.
Is there another trigger source beside ItemAdd I could use for running my code without having to click on the folder? Is there an event that gets triggered when the unread count for a folder changes?
Technical details:
Windows 8 OS
Using Outlook 2002 (Yes, I know...)
I'm an experienced C/C++ developer, but minimal experience in VBA, and none with Outlook.
VBA code:
Public WithEvents myItems As Outlook.Items
Public Sub Application_Startup()
Dim myNameSpace As Outlook.NameSpace
Const mailboxName As String = "Mail.com"
Const subfolderName As String = "Spam"
' Reference the items in the MAPI spam folder
' Because myOlItems is declared "WithEvents" the ItemAdd event will fire below.
Set myNameSpace = Application.GetNamespace("MAPI")
On Error GoTo noSpamFolder
Set myItems = myNameSpace.Folders(mailboxName).Folders(subfolderName).Items
On Error GoTo 0
Exit Sub
noSpamFolder:
MsgBox "Unable to find folder <" & mailboxName & "/" & subfolderName & ">"
End Sub
Private Sub myItems_ItemAdd(ByVal Item As Object)
Dim suspectFolder As Outlook.MAPIFolder
' Check to make sure it is an Outlook mail message, otherwise
' subsequent code will probably fail depending on what type
' of item it is.
If TypeName(Item) = "MailItem" Then
' Move message to the 'suspect' folder
On Error GoTo noSuspectFolder
Set suspectFolder = Outlook.Session.GetDefaultFolder(olFolderInbox).Folders("suspect")
On Error GoTo 0
Item.Move suspectFolder
End If
Exit Sub
noSuspectFolder:
MsgBox "Unable to find folder <suspect> as a sub-folder of default inbox folder"
End Sub
I Was struggling with a similar issue to move mail-items after they were sent and used your code to perform this task (thx!). There were several issues which still had to be resolved.
First of all, the items were moved, but immediately after they were placed into the trash folder. This seems to be an IMAP issue (Gmail) and may be resolved by changing the Internet E-mail Settings of the mailbox account from "Move deleted items to the following folder on the server" to "Mark items for deletion but do not move them automatically".
The second challenge was, like yours, trigger the code to do its work. In the account configuration the save sent emails option is disabled (since this is automatically performed by the Gmail server). I needed to sync the Sent items (MAPI) folder with the Send items (IMAP) folder. I achieved this by configuring the "send/receive" groups for this email account (in the group All account) and selecting the Sent items folder.
Now this folder is synced without the necessity to open the folder for syncing. I hope that this will also resolve your issue.
Peter
That makes sense - the IMAP provider in Outlook syncs the folder only when it is selected or accesed through the Outlook Object Model.
I don't think there is much you can do short of polling the folder every once in a while (and releasing the MAPIFolder object in between the hits)
Related
I need to save an Outlook file attachment with a static file name to a specific network location automatically when that email arrives. This file will be saved in a network location for upload using a monthly SSI server package. I hope to do this automatically without any interaction but not opposed to manually running a macro. I am unsure of references that are needed in VBA to get this to execute. I am also unfamiliar with the "ThisOutlookSession" configuration that I've seen in similar threads
I have attempted to use the existing script that I've seen here with no luck. ( I can get them to run without error but do not get any results ) I want to search all incoming email and only have it take action if the email has an attachment and that attachment has a specific unchanging file name. I have the developer tab enabled in Outlook and can access VBA through it. Looking for a solid simple solution. Constants are the file name and extension as well as the network folder. Variables would be the sender and date of delivery. Office 365 running Windows 10 in a professional environment. Any help or direction would be greatly appreciated.
You can handle incoming emails in Outlook in the NewMailEx event handler. The event fires 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, or SharingItem. The EntryIDsCollection string contains the Entry ID that corresponds to that item. Use the Entry ID string to call the NameSpace.GetItemFromID method and process the item. The NewMailEx event fires when a new message arrives in the Inbox and before client rule processing occurs.
Also, as a possible workaround, you may consider handling the ItemAdd event on the Inbox folder. This event is fired when one or more items are added to the specified collection. This event does not run when a large number of items are added to the folder at once. For example:
Public WithEvents myOlItems As Outlook.Items
Public Sub Application_Startup()
Set myOlItems = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderContacts).Items
End Sub
Private Sub myOlItems_ItemAdd(ByVal Item As Object)
MsgBox Item.Attachments.Count
End Sub
I have an intermittent updating problem with an outlook mailitem in the explorer mode on a public folder of an exchange server. The subject (or perhaps any other part as well) of a mailitem does not seem to update itself at times.
Specifically, the subject of the mailitem shown in the explorer mode is dissimilar to that shown in the inspector mode. In other words, the explorer mode is old, therefore needs updating or synchronization.
Hitting F9 (send/receive) to update the public folder does not seem to help.
I would like to know if this anomaly can be detected (and perhaps perform manual updates/synchronization) using an outlook macro.
Any ideas are welcome.
Some properties, factors unknown, update on saving only.
The subject of the mail in the explorer view should change when you manually close the item, if you agree to save changes. Should be the same if you manually saved regularly.
If you want more than a simple save:
Option Explicit
Sub MarkInUseSaveCurrentItem()
Dim currItem As Object
Set currItem = ActiveInspector.currentItem
If InStr(LCase(currItem.subject), LCase("Barok is working on this")) = 0 Then
currItem.subject = "Barok is working on this. To avoid conflicts do not open. " & currItem.subject
End If
currItem.Save
End Sub
I'm trying to run a macro that moves emails received before today to a cabinet folder whenever Outlooks starts. The problem is that the Application_Startup event handler happens before Outlook is completely loaded and folders have synced. Consequently, all the emails that came in last night aren't moved to the cabinet when I open Outlook in the morning.
To fix this I created a custom class to instantiate an Outlook.syncObject which syncs all folders and provides an event handler when the syncing is complete. I create an object from this class within the Application_Startup event handler. However, this sync doesn't seem to actually retrieve any emails and also seems to complete before Outlook has even loaded.
It seems like being able to execute code after Outlook has done all of it's startup processes would be a common feature request. Thanks for any help.
This sample code simply shows me how many unread emails are in my inbox. If I close Outlook, send myself an email, then open Outlook, I need Outlook to load and a full sync to occur before generating the messagebox with the number of unread emails in my inbox.
Oulook Application_Startup event handler:
Dim mySyncInstance As New mySync
Private Sub Application_Startup()
mySyncInstance.Initialize_handler
End Sub
Custom mySync Class Code:
Dim WithEvents mySync As Outlook.syncObject
Sub Initialize_handler()
Set mySync = Application.Session.SyncObjects.item(1)
mySync.Start
End Sub
Private Sub mySync_SyncEnd()
MsgBox Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).items.Restrict("[UnRead] = True").Count & _
" Emails are unread in the main inbox."
End Sub
I'm trying to run a macro that moves emails received before today to a cabinet folder whenever Outlooks starts.
You may consider handling the NewMailEx event of the Application class which is fired when a new item is received in the Inbox. So, you can get the mail item and decide whether you need to move it in a subfolder or not. This event fires 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, or SharingItem. The EntryIDsCollection string contains the Entry ID that corresponds to that item.
Also you can create a rule and assign a VBA macro sub. The incoming instance of the mail item object will be passed as a parameter. For example:
public sub test(mail as MailItem)
' do whatever you need
end sub
As an alternative way you can handle the ItemAdd event of the Items class which is fired when one or more items are added to the specified collection. This event does not run when a large number of items are added to the folder at once (more than 16).
Why not use Items.ItemAdd event on the Inbox folder?
I wrote a small piece of code, that checks an additional inbox (besides my main-adress in Outlook) for E-Mails.
The problem shows up, when using the code on a pc, where only the additional inbox is added to outlook (as the main inbox). Obviously, the code can't retrieve all Emails, only the older ones. That's awkward, because the path to the subfolders and even the emails seem to be found, but not the newer ones. I can see them in Outlook with no problem.
Does anyone have an idea, why this is happening? As I told, the same code works with no problem on a PC with an Outlook-Installation, with another main-inbox and the inbox that needs to be checked as additional one.
That's the code I use to access Outlook and the Emails:
Dim objFolder As Outlook.Folder
Dim objOL As Outlook.Application
Set objOL = CreateObject("Outlook.Application")
Set objFolder = objOL.GetNamespace("MAPI").Folders.Item("test#test.de").Folders.Item("Posteingang").Folders.Item("Subfolder-Name").Folders.Item("Subfolder-Name-2")
With objFolder.Items(1)
...
I had this same issue: in my case the emails were in microsoft exchange but were not downloaded to the local outlook.
If you can refresh the outlook it should resolve the problem
Why are you always retrieving the first item in the Items collection? Would you not want to loop through the items?
set objItems = objFolder.Items
objItems.Sort "[ReceivedTime]"
'now objItems is sorted
I have found that changing the cache mode setting in Outlook helps with this problem of emails being in Microsoft Exchange not being accessed thru Excel VBA, especially Step (3) below.
https://support.microsoft.com/en-us/office/turn-on-cached-exchange-mode-7885af08-9a60-4ec3-850a-e221c1ed0c1c
Click File > Account Settings > Account Settings.
Click the Exchange or Microsoft 365, and then click Change.
Under Offline Settings, check Use Cached Exchange Mode.
(If you're a Microsoft 365 subscriber with semi-annual updates, under Offline Settings, check Use Cached Exchange Mode to download email to an Outlook data file.
I would like to do some operation when a new mail comes to mailbox. For that I am using Item_Add() Event
Private Sub myOlItems_ItemAdd(ByVal Item As Object)
... Do Something
Exit Sub
Can anyone tell me How to set the Handler for "inbox" folder under "gilbertojperera#gmail.com"?
Problem:
The issue I am facing here is that this even get fired only when a new mail comes in "Inbox" folder under "Outlook Data File". But I used to receive mails in the other "Inbox" folder marked by second red arrow (third last folder)
Due to this problem My Item_Add() event is not being triggered & resulting to my outlook macro Fail.
Please do help if you have some valuable inputs.
It sounds like you only initialized the handler for the Inbox DefaultFolder (olFolderInbox). You also must add a handler to the Inbox folder for gilbertojperera#gmail.com.