update outlook cal aptmts works stand alone but not as com addin

Add-in Express™ Support Service
That's what is more important than anything else

update outlook cal aptmts works stand alone but not as com addin
 
grom grom


Guest


I have the program below that update calendar items. It works as a stand alone applicaiton. If I try to added the method to a commandbar commandbutton the program throws an exception get the outlook object (OutlookApp = Marshal.GetActiveObject("Outlook.Application"). Any ideas?

Code:

Private Sub CarryForwardAppointments()

' carry forward calendar appointments

Dim OutlookApp As Outlook._Application = Nothing
Dim NewMail As Outlook.MailItem = Nothing
Dim Recipient As Outlook.Recipient = Nothing
Dim Recipients As Outlook.Recipients = Nothing
Dim OlType As Type = Type.GetTypeFromProgID("Outlook.Application", False)

Dim CanQuit As Boolean = True

Try

red

OutlookApp = Marshal.GetActiveObject("Outlook.Application")



Catch

End Try

If Not (OutlookApp Is Nothing) Then
CanQuit = False
End If

Try
If (OutlookApp Is Nothing) Then
OutlookApp = Activator.CreateInstance(OlType)
End If
Catch

End Try

Dim objNameSpace As Outlook.NameSpace
objNameSpace = OutlookApp.GetNamespace("MAPI")

Dim objMAPIFolder As Outlook.MAPIFolder
objMAPIFolder = objNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar)

Dim objItems As Outlook.Items

objItems = objMAPIFolder.Items

Dim objAppointmentItem As Outlook.AppointmentItem

Dim intCount As Integer

For intCount = 0 To objItems.Count - 1

If intCount = 0 Then
objAppointmentItem = objItems.GetFirst
Else
objAppointmentItem = objItems.GetNext
End If

Console.WriteLine(objAppointmentItem.Subject)

Console.WriteLine(objAppointmentItem.Categories)

If objAppointmentItem.Categories = "1 - Carry Forward" Then

Dim datTomorrow As Date

datTomorrow = Today.AddDays(1)

Dim objTimeSpan As TimeSpan

objTimeSpan = objAppointmentItem.Start.TimeOfDay

datTomorrow = New Date(datTomorrow.Year, datTomorrow.Month, datTomorrow.Day, objTimeSpan.Hours, objTimeSpan.Minutes, objTimeSpan.Seconds)

objAppointmentItem.Start = datTomorrow

objAppointmentItem.Save()

End If

Marshal.ReleaseComObject(objAppointmentItem)

Next

Marshal.ReleaseComObject(objItems)
Marshal.ReleaseComObject(objNameSpace)

If Not (OutlookApp Is Nothing) Then
If (CanQuit) Then
OutlookApp.Quit()
End If
Marshal.ReleaseComObject(OutlookApp)
End If

End Sub
Posted 04 Nov, 2009 16:44:22 Top
Eugene Astafiev


Guest


Hello Glenn,

It looks like there is no running Outlook instance. Please make sure that you launched Outlook before.
Posted 05 Nov, 2009 01:41:48 Top
grom grom


Guest


The program is called from an adx comm addin from within outlook. I run outlook and click the toolbar button and when it tried to instantiate a outlook object it throws and exception (operation unavailable exception from hresult:0x800401e3(mk_e_unavailable). Not sure if within the com addin it can see the outlook application running. Wondering if theres an alternate way of geting reference to the running outlook object.

Thanks,
Glenn
Posted 05 Nov, 2009 11:55:12 Top
Eugene Astafiev


Guest


Hello Glenn,

OutlookApp = Marshal.GetActiveObject("Outlook.Application")


Why do you need to use the GetActiveObject method?

The add-in module provides you with OutlookApp property. You can pass an Outlook instance to the CarryForwardAppointments method.
Posted 05 Nov, 2009 13:37:04 Top
grom grom


Guest


Thanks Eugene thats exactly what I was looking for.

Glenn
Posted 05 Nov, 2009 14:53:38 Top