Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Detect completion of Outlook Send/Recieve programatically (vbs?)

1,737 views
Skip to first unread message

Jaswicki

unread,
Oct 2, 2007, 9:17:01 AM10/2/07
to
How can I detect when the Outlook Send and Receive process is completed using
VBScript or VB? Basically I have a program that needs to wait until the
Outlook profile is completely synchronized before I can run a part of my
code. Any idea how to detect that?

Ken Slovak - [MVP - Outlook]

unread,
Oct 2, 2007, 9:38:09 AM10/2/07
to
You can't. If the code starts a send/receive as a sync action you can handle
an event when the sync completes. You get no events when a scheduled sync or
user started sync occurs.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007
Reminder Manager, Extended Reminders, Attachment Options
http://www.slovaktech.com/products.htm


"Jaswicki" <Jasw...@discussions.microsoft.com> wrote in message
news:4EF3FA8F-5A04-4887...@microsoft.com...

Jaswicki

unread,
Oct 2, 2007, 10:41:01 AM10/2/07
to
OK, if I initiate a send/recieve programatically while there is another synch
occuring will I get the handle?

Ken Slovak - [MVP - Outlook]

unread,
Oct 2, 2007, 2:09:53 PM10/2/07
to
Only to the synch you start in code, for the other one no. And how would you
know that a synch is ongoing to begin with?

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007
Reminder Manager, Extended Reminders, Attachment Options
http://www.slovaktech.com/products.htm


"Jaswicki" <Jasw...@discussions.microsoft.com> wrote in message

news:49D368EE-34C3-4275...@microsoft.com...

Jaswicki

unread,
Oct 3, 2007, 11:32:00 AM10/3/07
to
OK from what I read the SyncEnd() event is not accessible from VBScript. I
rewrote the script as a VB.net Console Application (I need no user
interaction due to SMS deployment. I have everything working other than that
the event never gets triggered even when I see the sync is complete in
Outlook and Any timer event I use to wait for the event to trigger causes my
CPU to peg at 99% utilization which causes the sync to run extremely slowly.
I have pasted my code below any help would be greatly appreciated.

________________________________________________________________
Imports Outlook = Microsoft.Office.Interop.Outlook


Module Module1
Public Interface SyncObjectEvents
End Interface

Public bSendReceiveEnded As Boolean

'Private WithEvents objDefaultSendReceive As Outlook.SyncObject


Sub instance_SyncEnd()
MsgBox("triggered")
bSendReceiveEnded = True
End Sub

Sub Main()
On Error Resume Next

' Create an Outlook application.
Dim i As Integer
Dim myTimer As New System.Timers.Timer()
myTimer.Interval = 3000
Dim oApp As Outlook._Application = New Outlook.Application
' Create the name space.
Dim oNS As Outlook._NameSpace = oApp.GetNamespace("mapi")
Dim oSyncs As Outlook.SyncObjects
Dim oSync As Outlook.SyncObject
Dim instance As SyncObjectEvents
Dim myAPF = oNS.Folders("Public Folders").Folders("All Public
Folders").Folders("North America").Folders("Business functions/Projects
(Cross-site)")
Dim pfFolder = oNS.Folders("Public Folders").Folders("Favorites")
Dim chkfold = oNS.Folders("Public
Folders").Folders("Favorites").Folders("NA Field Force Automation")
If chkfold Is Nothing Then


'If chkfold Is Nothing Then
' Reference SyncObjects.
myAPF.Folders("NA Field Force Automation").AddToPFFavorites()

bSendReceiveEnded = False
oSyncs = oNS.SyncObjects
oSync = oSyncs.Item("All Accounts")
' Send and receive.
oSync.Start()
Do While bSendReceiveEnded = False
myTimer.Enabled = True
Loop

Dim favfold = oNS.Folders("Public
Folders").Folders("Favorites").Folders("NA Field Force Automation")
favfold.display()
'End If
End If

' Clean up.
chkfold = Nothing
myAPF = Nothing
oSync = Nothing
oSyncs = Nothing
oNS = Nothing
oApp = Nothing
End Sub

End Module
____________________________________________________________________

"Ken Slovak - [MVP - Outlook]" wrote:

Ken Slovak - [MVP - Outlook]

unread,
Oct 3, 2007, 11:49:59 AM10/3/07
to
Why are you using a timer to wait for synching to finish? Synching is
asynchronous and will fire an event when it's finished. Just handle the
event that fires when the synch finishes.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007
Reminder Manager, Extended Reminders, Attachment Options
http://www.slovaktech.com/products.htm


"Jaswicki" <Jasw...@discussions.microsoft.com> wrote in message

news:75A9F220-AD77-4C1E...@microsoft.com...

Jaswicki

unread,
Oct 3, 2007, 12:11:02 PM10/3/07
to
I tried that as well but I never seem to catch the event no matter if that
timer was there or not. Do you see anything in my syncend event handler that
is incorrect?

"Ken Slovak - [MVP - Outlook]" wrote:

Ken Slovak - [MVP - Outlook]

unread,
Oct 3, 2007, 6:39:16 PM10/3/07
to
Imports Outlook = Microsoft.Office.Interop.Outlook

Module Module1
Public Interface SyncObjectEvents
End Interface

Public bSendReceiveEnded As Boolean

Private WithEvents instance As Outlook.SyncObject

instance = oSyncs.Item("All Accounts")
' Send and receive.
instance.Start()
'Do While bSendReceiveEnded = False
' myTimer.Enabled = True
' Loop

Dim favfold = oNS.Folders("Public
Folders").Folders("Favorites").Folders("NA Field Force Automation")
favfold.display()
'End If

Else
MessageBox.Show("chkfold IsNot Nothing")
End If

' Clean up.
chkfold = Nothing
myAPF = Nothing
'oSync = Nothing
oSyncs = Nothing
oNS = Nothing
oApp = Nothing
End Sub

End Module


--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007
Reminder Manager, Extended Reminders, Attachment Options
http://www.slovaktech.com/products.htm


"Jaswicki" <Jasw...@discussions.microsoft.com> wrote in message

news:25931AB9-0314-4AE9...@microsoft.com...

Jaswicki

unread,
Oct 4, 2007, 8:50:01 AM10/4/07
to
Thanks Ken but I created a nwe project and pasted in your code just to be on
the safe side and it still drops through before the sync is competed. Any
idea why?

"Ken Slovak - [MVP - Outlook]" wrote:

Ken Slovak - [MVP - Outlook]

unread,
Oct 4, 2007, 9:30:28 AM10/4/07
to
If this is a console app then it's just running to completion before the
synch ends. You could just do your loop without the timer but setting that
Boolean flag in the synch end event as a means of exiting the loop.

If you do use something like a loop I'd put a DoEvents in the loop so you
cede control to other processes while the loop is running.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007
Reminder Manager, Extended Reminders, Attachment Options
http://www.slovaktech.com/products.htm


"Jaswicki" <Jasw...@discussions.microsoft.com> wrote in message

news:6A8FDDBD-0C77-4AA9...@microsoft.com...

Jaswicki

unread,
Oct 4, 2007, 9:58:03 AM10/4/07
to
Thanks! So if I had used a normal Windows .ent application project it would
have waited for the syncend rather than needing the loop?
I added the following loop...

Do While bSendReceiveEnded = False
System.Windows.Forms.Application.DoEvents()
Loop
The problem is that this loop still peaks my processor to about 90%. Isn't
there a way to get vb to just wait without running the proc like this?

"Ken Slovak - [MVP - Outlook]" wrote:

Ken Slovak - [MVP - Outlook]

unread,
Oct 4, 2007, 10:37:27 AM10/4/07
to
It depends on the type of app you're running. I usually use COM addin
programs that run until Outlook closes or the user disconnects the addin. In
cases like that there's no problem because a Sub Main() type procedure isn't
there to run to completion.

An alternative might be to use a form that has a Close button and showing
that form modally would prevent the app from completing.

This isn't a VB thing, the same thing would happen using VB6 or C# or Delphi
or C++ or whatever in standalone code. You have to find a way to prevent the
code from completing and shutting down a standalone app. It's the same with
any event driven application.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007
Reminder Manager, Extended Reminders, Attachment Options
http://www.slovaktech.com/products.htm


"Jaswicki" <Jasw...@discussions.microsoft.com> wrote in message

news:31528D67-3B5C-473B...@microsoft.com...

Jaswicki

unread,
Oct 4, 2007, 11:06:01 AM10/4/07
to
Thanks again. I really appreciate the time you have put in helping me with
this. I actually just walked out of a meeting where they decided to scrap the
functionallity this remaining step would have given so, though I will
continue to try and figure out my issue, it is going to the back burner now.
If, hopefully when, I find the solution I will come back and post it here for
others to benefit from.

"Ken Slovak - [MVP - Outlook]" wrote:

Alban

unread,
Aug 7, 2008, 11:52:05 PM8/7/08
to
Hi,

I face the same problem than Jaswicki. I used the code, I tryed it in many
configuration, I have never gotten any sycn event fired (neither start, end
or progress).

Here is a standalone code that should work according to your discussion. Any
clue?


Imports Outlook = Microsoft.Office.Interop.Outlook

Module Module1
Public Interface SyncObjectEvents
End Interface

Public bSendReceiveEnded As Boolean

Private WithEvents instance As Outlook.SyncObject

Sub instance_SyncEnd()
MsgBox("triggered")
bSendReceiveEnded = True
End Sub

Sub Main()
On Error Resume Next
' Create an Outlook application.

Dim oApp As Outlook._Application = New Outlook.Application
' Create the name space.
Dim oNS As Outlook._NameSpace = oApp.GetNamespace("mapi")
Dim oSyncs As Outlook.SyncObjects

bSendReceiveEnded = False
oSyncs = oNS.SyncObjects
instance = oSyncs.Item(3)

' Send and receive.
instance.Start()


Do While bSendReceiveEnded = False
System.Windows.Forms.Application.DoEvents()
Loop

oSyncs = Nothing


oNS = Nothing
oApp = Nothing
End Sub

End Module


I added to the project references to the .COM 'Microsoft Outlook 11.0 Object
Library' and to .NET system.windows.forms

I am compiling with VB 2005 express edition and using Outlook 2003

I have been searching around without any clue... So thanks for any help

0 new messages