VBA or Visual Basic or VSTO for outlook sync?

A

Alban

Hi

I have been struggling for a while with the following problem: I am trying
to write a code that will launch 'send/receive' for an Outlook account. The
purpose is to sync back outlook to an exchange server after a 1-way sync from
google mail witout opening outlook.

I have downloaded and installed visual basic 2005 express edition and could
create a console application that perform this operation. I used the
syncobect from outlook. However, the events from outlook do not fire and I
absolutely need the syncend event to wait before ending the application. I
have been intensively trying to find a bug in my code without success

So here is my question: is my code wrong or does it come from the tools I
have been using to create the application. Do I need to use VBA or the VSTO
extension to visual studio. In that case do you know if there is a free way
to do that?

Bonus question: if you have in mind another way to automatically sync
outlook to exchange, it could also be a solution!

Thanks for reading and your help

Alban
 
D

Dmitry Streblechenko

So what are the relevant snippets of your code?
Have you tried to create a UI app rather than a command line one?

--
Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
-
 
A

Alban

Here is my code. I also linked the COM microsoft OUtlook library. I need a
console as I would like to then schedule the task without any user
interaction.

I am still confused with catching event. The more I read on the web, the
less I clarify the way events are treated in VB.NEt, VB, VBA and the role of
VSTO...


Imports Outlook = Microsoft.Office.Interop.Outlook

Module Module1

Public bSendReceiveEnded As Boolean
Public WithEvents mySync As Outlook.SyncObject

Sub mySync _SyncEnd()
MsgBox("triggered")
bSendReceiveEnded = True
End Sub

Sub Main()

' 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
mySync = 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
 
A

Alban

I made a typo in the previous post, sorry please read
mySync .start()
instead of
instance.start()
 
D

Dmitry Streblechenko

Does it work if you replace the DoEvents loop with a call to MessageBox()
(which also runs the message loop)?

--
Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
-
 
A

Alban

1/ I do not have such a function, am I missing a reference?
2/ what do you mean by "which also runs the message loop"?
3/ Apparently, it is not related to the loop. I have made a windows
application and launch the previous code at the click of a button. No event
is catched, whatever event I try to catch from outlook.

Note: I installed outlookspy toolbar I could see outlook event firing from
action generated by my code while my code could not see them
 
D

Dmitry Streblechenko

What is your code that uses button click?
Is there a particular reason why you use sync object with thee index 3/ Have
you tried 1 (which in most cases is "Send/Receive All")?

--
Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
-
 
A

Alban

Thanks for your help, I hope we'll make it.
The windows application has a single form with one button nicely nammed
'button1'.
As for the accounts, I use a special account dedicated to only synchronize
the calendar from local to the server. Note that everything is the same if I
use 1. The synchronization does actually work but I Still need the events.

Code:

Imports Outlook = Microsoft.Office.Interop.Outlook

Public Class Form1


Public bSendReceiveEnded As Boolean = False
Public WithEvents oApp As Outlook._Application
Public WithEvents mySync As Outlook._SyncObject

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
syncMail()
End Sub

Public Sub syncMail()
' Create an Outlook application.
oApp = New Outlook.Application
mySync = oApp.Session.SyncObjects.Item(3)

' Send and receive.
mySync.Start()

oSyncs = Nothing
oNS = Nothing
oApp = Nothing
End Sub


Private Sub mySync_SyncEnd()
bSendReceiveEnded = True
MsgBox("triggered")
End Sub

Private Sub mySync_SyncStart()
MsgBox("triggered")
End Sub

Private Sub mySync_OnError(ByVal Code As Long, ByVal Description As
String)
MsgBox("Unexpected sync error" & Code & ": " & Description)
End Sub

End Class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top