when to reference and release current item across multiple forms

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

when to reference and release current item across multiple forms
 
Ben Williams


Guest


Hi I have a number of outlook ADXOlforms for journal item, what's best way to reference the current active item across all forms and release when the item is closed?

Thanks
Posted 14 Apr, 2015 06:49:49 Top
Andrei Smolin


Add-in Express team


Posts: 18825
Joined: 2006-05-11
Hello Ben,

If the form is embedded in an inspector window, the ADXOlFrom.InspectorObj property returns the corresponding Outlook.Inspector object. Then you use Inspector.CurrentItem and cast it to Outlook.JournalItem.

If however, the form is embedded in an explorer window, you use the ADXOlFrom.ExplorerObj property which returns the corresponding Outlook.Explorer object. Then you invoke Explorer.Selection, retrieve the first item from the selection and cast it to Outlook.JournalItem.

You can release the item in the ADXOlForm.AfterFormHide event. Make sure that you wrap the code releasing the item in a try/catch block.


Andrei Smolin
Add-in Express Team Leader
Posted 14 Apr, 2015 07:40:51 Top
Ben Williams


Guest


Thanks Andrei do I add that to each form?

Private Sub ADXOlFormDetails_ADXAfterFormHide(ByVal sender As Object, ByVal e As AddinExpress.OL.ADXOlForm.ADXAfterFormHideEventArgs) Handles Me.ADXAfterFormHide
        If toDetailsItem IsNot Nothing Then
            If Marshal.IsComObject(toDetailsItem) Then
                Marshal.ReleaseComObject(toDetailsItem)
            End If
        End If
    End Sub


    Private Sub ADXOlFormDetails_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            If Not IsNothing(Me.InspectorObj) Then
                If Me.InspectorObj.CurrentItem.Class = Outlook.OlObjectClass.olJournal Then
                    toDetailsItem = Me.InspectorObj.CurrentItem
                End If
            End If
            If IsNothing(toDetailsItem) Then Exit Sub
        Catch
        Finally
            If Not (toDetailsItem Is Nothing) Then
                Marshal.ReleaseComObject(toDetailsItem)
            End If
        End Try
    End Sub
Posted 14 Apr, 2015 08:07:31 Top
Andrei Smolin


Add-in Express team


Posts: 18825
Joined: 2006-05-11
Ben,

Ben Williams writes:
Thanks Andrei do I add that to each form?


I can't answer this question because I don't know what you need to achieve. The code above releases toDetailsItem when the form loads; then you release it once again when the form gets hidden. This doesn't make sense for me. Also note that Form_Load occurs only once for a given form instance while the ADXBeforeFormShow and ADXAfterFormHide events may occur several times.


Andrei Smolin
Add-in Express Team Leader
Posted 14 Apr, 2015 09:23:53 Top
Ben Williams


Guest


hi Andrei

what I would like to do is have 2 or 3 forms that depending on the journal type "Phone Call" or "Meeting" will be displayed.

each form will perform different tasks each task for the user to complete will create outlook journalitem userproperties for that item, so I need to reference Me.InspectorObj.CurrentItem in each form then release it when the tasks have been completed.

at the moment I've got each form performing the reference as above minus the release on load thanks for pointing that out.

Is it better to use ADXBeforeFormShow and ADXAfterFormHide for each form then? note im not allowing the forms to be closed just minimise.

thanks
Posted 15 Apr, 2015 04:12:22 Top
Andrei Smolin


Add-in Express team


Posts: 18825
Joined: 2006-05-11
Hello Ben,

Instead of caching a reference to the item, I would get that item to perform the job and release the item as soon as the job is done. That would simplify the stuff, wouldn't it?

Ben Williams writes:
Is it better to use ADXBeforeFormShow and ADXAfterFormHide for each form then? note im not allowing the forms to be closed just minimise.


Yes, it is.


Andrei Smolin
Add-in Express Team Leader
Posted 15 Apr, 2015 05:09:50 Top
Ben Williams


Guest


Thanks again Andrei

Just to double check that I've coded it correctly please see below

Private Sub ADXOlFormDetails_ADXBeforeFormShow() Handles Me.ADXBeforeFormShow

        Try
            If Not IsNothing(Me.InspectorObj) Then
                If Me.InspectorObj.CurrentItem.Class = Outlook.OlObjectClass.olJournal Then
                    toDetailsItem = Me.InspectorObj.CurrentItem
                    OutlookProperties()
                End If
            End If
            If IsNothing(toDetailsItem) Then Exit Sub
        Catch
        End Try

    End Sub

Private Sub ADXOlFormDetails_ADXAfterFormHide(ByVal sender As Object, ByVal e As AddinExpress.OL.ADXOlForm.ADXAfterFormHideEventArgs) Handles Me.ADXAfterFormHide
        Try
        Finally
            If toDetailsItem IsNot Nothing Then
                If Marshal.IsComObject(toDetailsItem) Then
                    Marshal.ReleaseComObject(toDetailsItem)
                    toDetailsProject = Nothing
                    toDetailsCreatedBy = Nothing
                    toDetailsModifiedBy = Nothing
                End If
            End If
        End Try

Posted 15 Apr, 2015 05:37:33 Top
Andrei Smolin


Add-in Express team


Posts: 18825
Joined: 2006-05-11
Ben,

Every Me.InspectorObj call returns the same COM object because Add-in Express caches it behind the scenes. On the other hand every Me.InspectorObj.CurrentItem call returns a new COM object that you need to release; this is because the ".CurrentItem" part calls into the Outlook object model. You need to modify that part of code to avoid creating a COM object which you actually don't need.


Andrei Smolin
Add-in Express Team Leader
Posted 15 Apr, 2015 06:07:18 Top
Ben Williams


Guest


You need to modify that part of code to avoid creating a COM object which you actually don't need.


Sorry to be a pain but how? removing currentitem returns nothing :(
Posted 15 Apr, 2015 06:17:30 Top
Andrei Smolin


Add-in Express team


Posts: 18825
Joined: 2006-05-11
Below is a raw sketch:

If Not IsNothing(Me.InspectorObj) Then 
    dim item as Object = Me.InspectorObj.CurrentItem
    If item.Class = Outlook.OlObjectClass.olJournal Then 
        toDetailsItem = item
        OutlookProperties() 
    else
       Marshal.ReleaseComObject(item)
    End If 
End If



Andrei Smolin
Add-in Express Team Leader
Posted 15 Apr, 2015 06:27:53 Top