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 |
|
Eugene Astafiev
Guest
|
Hello Glenn,
It looks like there is no running Outlook instance. Please make sure that you launched Outlook before. |
|
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 |
|
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. |
|
grom grom
Guest
|
Thanks Eugene thats exactly what I was looking for.
Glenn |
|