Urgent: Outlook with VB.net

  • Thread starter Thread starter srinivas
  • Start date Start date
S

srinivas

Hi,
i'm trying to retreive the appointments from different profiles of
outlook.
My outlook has 3 profiles created.('Sample1','Sample2','Sample3')
my form contains 3 buttons.each one for each profile.
so when user clicks on a button my code has to show the appointments
for that particular profile that was given there.
i wrote a small procedure for that
here's the code:
----------------------------
Private Sub btnProfile1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnProfile1.Click
olProfile("Sample1")
End Sub

Private Sub btnProfile2_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles btnProfile2.Click
olProfile("Sample2")
End Sub

Private Sub btnProfile3_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles btnProfile3.Click
olProfile("Sample3")
End Sub


//This is the procedure....
Private Sub olProfile(ByVal profile As String)
MsgBox("Generating for the profile:" & profile)

Dim appProfile As Outlook.Application = New
Outlook.Application()
Dim nsProfile As Outlook.NameSpace =
appProfile.GetNamespace("mapi")

nsProfile.Logon(profile, , False, True)
Dim fldrProfile As Outlook.MAPIFolder =
nsProfile.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar)
Dim aProfile As Outlook.Items = fldrProfile.Items
Dim aptProfile As Outlook.AppointmentItem
Dim iProfile As Integer
MsgBox("Appointment Information from the profile" & profile)
MsgBox(aProfile.Count)
aptProfile = aProfile.GetFirst

For iProfile = 0 To aProfile.Count - 1
MsgBox("Subject:" & aptProfile.Subject & vbCrLf & "
Location:" & aptProfile.Location & vbCrLf & "Start Time:" &
aptProfile.Start & vbCrLf & " End Time:" & aptProfile.End)
aptProfile = aProfile.GetNext
Next
nsProfile.Logoff()
appProfile = Nothing
nsProfile = Nothing
aProfile = Nothing
aptProfile = Nothing

End Sub

----------------------------------------
When i click on one of button it's showing the appointments for that
profile.If i click another button then also it's working fine.
HERE's the problem:
From then onwards if i click any button it's showing the previous
profile's appointments only,irrespective of the button.
here i was caught....how to get rid of this...?
Is there any way to check whether outlook application was launched or
not...if so with what profile??
Can anyone please help me....
Thanks in advance...

Srinivas
 
srinivas,
For starters you are not really releasing the Outlook objects!
appProfile = Nothing
nsProfile = Nothing
aProfile = Nothing
aptProfile = Nothing
End Sub

Remember in .NET we have the GC, setting an object to Nothing, does exactly
that it sets it to Nothing. The GC will at its leisure release the object
(more importantly the COM object). This will cause Outlook to "remain" open,
hence you are getting the same session...

Instead of setting the (COM) objects to Nothing, you should call
ReleaseComObject (in a loop) for each of your COM Objects (objects returned
from Outlook).

See: System.Runtime.InteropServices.Marshal.ReleaseComObject

This will ensure that the Outlook COM objects are released allowed Outlook
to close. However I do not have multiple profiles to try your sample on...



The following site has a plethora of articles on using Outlook from .NET.

http://www.microeye.com/resources/res_outlookvsnet.htm

Although not necessarily .NET both of these have a lot of Outlook
programming examples & information:

http://www.outlookcode.com/
http://www.slipstick.com/dev/index.htm

Hope this helps
Jay
 
Back
Top