I would like to make VBA script that can search and list hit-emails-information. I am on halfway and found my script only search in local replicated data instead of online original data. Therefore search results obviously limited to recent correspondences. I would highly appreciate advice from someone who can help on solving it to search online data thoroughly.
My script is like below.
Set objOutlook = Outlook.Application
Set objOutlookAcct = objOutlook.Session.Accounts(TargetAccount)
Set objOutlookStore = objOutlookAcct.DeliveryStore
Set MyInbox = objOutlookStore.GetDefaultFolder(olFolderInbox)
Set MyItems = MyInbox.Items
And MyItems is the subject population of search which only includes replicated local data.
CodePudding user response:
There is nothing you can do in Outlook Object Model to counter this - you can either change the sync options to "All Items" instead of a time range or you would have to switch to Extended MAPI (C or Delphi only) or Redemption (any language including VBA) to open the folder in the online mode before you search (but then it will be slower of course):
MAPI_NO_CACHE = &H0200
MAPI_BEST_ACCESS = &H0010
set OomFolder = Application.ActiveExplorer.CurrentFolder
set Session = CreateObject("Redemption.RDOSession")
Session.MAPIOBJECT = Application.Session.MAPIOBJECT
set RdoFolder = Session.GetFolderFromID(MyInbox.EntryID, , MAPI_NO_CACHE Or MAPI_BEST_ACCESS)
MsgBox "Number of items in the online folder: " & RdoFolder.Items.Count
CodePudding user response:
The Outlook object model doesn't provide anything for searching items online (on the server). Instead, you can sync the local data in Outlook with the server and then search for the updated/received data. The SyncObject class which represents a Send\Receive group for a user. The SyncObject.Start method begins synchronizing a user's folders using the specified Send\Receive group. For example:
Public Sub Sync()
Dim nsp As Outlook.NameSpace
Dim sycs As Outlook.SyncObjects
Dim syc As Outlook.SyncObject
Dim i As Integer
Dim strPrompt As Integer
Set nsp = Application.GetNamespace("MAPI")
Set sycs = nsp.SyncObjects
For i = 1 To sycs.Count
Set syc = sycs.Item(i)
strPrompt = MsgBox( _
"Do you wish to synchronize " & syc.Name &"?", vbYesNo)
If strPrompt = vbYes Then
syc.Start
End If
Next
End Sub
The SyncObject.SyncEnd event is fired immediately after Microsoft Outlook finishes synchronizing a user's folders using the specified Send/Receive group.
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 "Synchronization is complete."
End Sub
Also you may consider using a low-level API on which Outlook is based on - Extended MAPI or any third-party wrappers around that API such as Redemption. It simplifies your life much when dealing with Extended MAPI. In that case you can point out which data access you want to get (see Dmitry's reply to you).
