VBA: not all Outlook Emails show up - vba

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.

Related

outlook mailitem in explorer mode is old

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

Outlook MailItem cache issue when saving in vb.net

I have a small WinForms program that allows my users to create email blasts for our clients. The app has two options: one is an HTML editor to design the email (works great) and the second is to import .msg or .oft template.
Once the email is complete it is moved to a shared outlook mailing folder for a nightly macro send job.
Pretty simple stuff!
The problem: Once the template is open in the application outlook seems cache that version. If the user decided to get out make a change in the template Outlook doesn't pick up the update.
Note: If the users clicks on the "Preview" button they received the correct UPDATED version in their inbox. But when they submit the MailItem it picks up the old version.
Dim newItem as Outlook.MailItem = gobjOutlook.CreateItemFromTemplate(fileEmailTemplate.FileName)
The send command works fine newItem.Send()
But when I move it to the shared folder it gets the original version from somewhere.
Dim addFldr As Outlook.MAPIFolder
addFldr = StoreFLDR.Folders.Add(gobjNamespace.CurrentUser.Name & ": " & DateTime.Now.ToString())
newItem.Save()
newItem.Move(addFldr )
I have tried forcing the GC and SaveAs to another location and reload the template, no luck.
I'd suggest starting from releasing underlying COM objects instantly. Use System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Outlook object when you have finished using it. Then set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object.
You may find the How To: Create a new Outlook message based on a template article helpful. Anyway, it would be great to see your full source code related to Outlook.

Control already created Outlook message from excel VBA

longtime reader, first time messenger (not my first time making a bad joke though),
I would like to know if it is possible to gain control of an Outlook email message that has already been created. At work we have to download new work orders from a secure website, thanks mostly to this site, I have been able to set up a macro that logs in, finds the new work orders, and clicks the button to open the work order. Once this button is clicked, a new IE window is opened with a pdf file, and the "Send page by email" command is used to create a new outlook message. I have the outlook 12 reference (using Office 2007), and am able to take control of an existing outlook session to create a new email using:
Dim SendOrder As Outlook.Application
Set SendOrder = GetObject(, "Outlook.Application")
But I cannot figure out how to make it control the email message that was opened by IE. I tried using GetObject(, "Outlook.Application.MailItem), and a few other failed ideas, but 3 ideas were all I had, so I'm hoping someone on here can help me out with this, otherwise I'll probably have to save the file in IE and create a new email message, which seems like adding an extra step.
You're on the right path, I think. Something like this works with Outlook mailItems opened from Outlook. I have not tested it on mailItems opened from IE, though.
Sub GetAMailItem()
'## Requires reference to MS Outlook object library ##
Dim oApp As Outlook.Application
Dim mItem As MailItem
Set oApp = GetObject(, "Outlook.Application")
If TypeName(oApp.ActiveWindow) = "Inspector" Then
Set mItem = oApp.ActiveWindow.CurrentItem
End If
Set oApp = Nothing
End Sub
Found the guts of that code here, just made a modification or two to give you a structured example that might suit your needs.

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)

How to Set Outlook in Work Offline Mode by VBA(Macro) Running From MS Word VBA UserForm

I am trying to run a VBA code which it's supposed to let the user attach a file into a Mail Merge function.
In order to do this I need to put Outlook in Offline mode to keep the mails in outbox before attaching any files to mails. Now I would like to know if there is a method to put Outlook 2007 in Offline mode from a Word Macro?
First, add a reference to the Outlook Object Library. If Outlook will already be open:
Outlook.ActiveExplorer().CommandBars.FindControl(, 5613).Execute
if Outlook is closed:
Dim olApp As Outlook.Application
Set olApp = CreateObject("Outlook.Application")
olApp.GetNamespace("MAPI").Folders.GetFirst.GetExplorer.CommandBars.FindControl(, 5613).Execute