outlook mailitem in explorer mode is old - vba

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

Related

Automatically Closing Outlook Windows via Script/Macro

I'm hoping someone on here can point me in the right direction with some expertise. I have a situation with a client's outlook where we need to close any open draft windows after a new message/draft is opened. Ideally after the 4th new window has opened.
Any "new message" window that opens after this we need the script to close 1st window that opened. Either killing the process, or something similar.
Recently have been looking into Outlook macros, but am unsure if they will help in this instance. (Maybe they are?). Being more familiar with Powershell, figured we could start there.
Looking to get help writing a powershell script, macro, etc to do this on the backend.
The Outlook object model provides all the required events, methods and properties for that. So, VBA macros is the right choice if you don't need to distribute the solution on multiple machines. Otherwise, you need to consider developing a COM add-in instead (for example, a VSTO based one should work for you). See Walkthrough: Create your first VSTO Add-in for Outlook for more information.
You can handle the NewInspector event which is fired whenever a new inspector window is opened, either as a result of user action or through program code. The event occurs after the new Inspector object is created but before the inspector window appears.
You can also check the number of opened inspector windows in Outlook by using the Inspectors.Count property which returns a long indicating the count of objects in the specified collection.
Finally, the Inspector.Close method closes the Inspector and optionally saves changes to the displayed Outlook item. For example, a VBA sample which closes the active inspector instance:
Sub CloseItem()
Dim myinspector As Outlook.Inspector
Dim myItem As Outlook.MailItem
Set myinspector = Application.ActiveInspector
Set myItem = myinspector.CurrentItem
myItem.Close olSave
End Sub

Outlook VBA draft emails without "save changes" dialog

I use VBA to create new MailItem elements and move them to my inbox.
Since they are not sent they are handled as drafts from Outlook's perspective.
This means, when I open such an element I cannot close it without the annoying saving prompt.
Is there a way to work with unsent mail elements to keep track of things?
I could also send these elements to my own mail address but this would sometimes take a bit longer until it's received. So I'd prefer to just create them locally.
With .Close you designate olDiscard, olPromptForSave or olSave.
https://learn.microsoft.com/en-us/office/vba/api/outlook.mailitem.close(method)
Option Explicit
Sub closeWithoutPromptToSave()
Dim currItem As MailItem
Set currItem = ActiveInspector.currentItem
currItem.Close olSave
End Sub

VBA: not all Outlook Emails show up

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.

Outlook ItemAdd event on IMAP folder fires only when folder is selected

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)

Problem Getting Outlook 2007 Running VBA Script

I'm trying to get Outlook to save the attachment in a daily email to a folder where I can have a file system watcher ready to parse and analyze the attachment (it's the report of a data integrity checker). I've set up a Rule that is supposed to run a VBA script, but it just doesn't run as far as I can tell. I've verified in VB6 that the code will in fact save some text to a file, so if Outlook actually runs the VBA script it should be able to do the same. But it doesn't! Can anyone see what the heck I'm doing wrong?
Dim WithEvents objInbox As Outlook.Items
Private Sub Application_Startup()
Set objInbox = Session.GetDefaultFolder(olFolderInbox).Items
End Sub
Sub SnagAttachment(theItem As MailItem)
On Error Resume Next
Dim fnum As Integer
fnum = FreeFile()
Open "c:\temp\success.txt" For Output As #fnum
Print #fnum, "Ran SnagAttachment Successfully"
Close #fnum
End Sub
Note that when I use the Rules wizard, and choose "run a script" the Sub SnagAttachment is listed as a script that can be selected.
How would you know if it's working or not when you put On Error Resume Next at the top of the procedure? You would never find out.
Here are the rules for creating a script that should be run as part of a Rule:
How to create a script for the Rules Wizard in Outlook
Also note the caveat found at How to process incoming messages in Microsoft Outlook:
A "run a script" rule is not a good choice for heavy traffic
applications, as Outlook is likely to skip applying the rule if too
many items arrive that meet the rule's conditions.
In order to get the script to work you need to change the security settings in Outlook. Go to Tools > Macro > Security and change it to "Warnings for all macros". Then restart Outlook.
Hope this helps
Try isolating the exact problem:
Check macro security settings. At maximum, it must be set no higher than "Warnings for all macros".
Try creating a new module with a single test sub:
Sub Test(Item as Outlook.MailItem)
MsgBox "test"
End Sub
Then set up a new rule to handle _all_ incoming messages running this sub as the only action, and temporarily disable all other rules. Then send a message to yourself. If you get no popup box as a result, this may be an indication of a bad Outlook install. Try reinstalling it, or calling MS up directly for support.
If the previous test was successful, try working with the `Scripting.FileSystemObject` object instead of `Freefile()` to create and populate files. This is just to test if there's some odd bug you're encountering here. Worth a shot, right?
Make sure your rule conditions are set correctly. There could be a glitch or misspelling in a condition which just drops all messages you want this script to run on.
I was experiencing the same issue, and it seems to me that if there is an error in your code, the script will not even start. This is as opposed to standard VBA where the debugger pops up for runtime errors. For example, I had a label at the end of my function that was missing the colon after it. This caused the script to not run at all (As opposed to running up to this line and then failing). I would suggest commenting out all of your code and starting with just a msgbox "hello world". I would leave this in your code as you debug it to know the code is running, but you will probably have to dismiss the message box many times. Iteratively add back lines of code until you discover where the problem is.