79106034

Date: 2024-10-19 22:06:53
Score: 0.5
Natty:
Report link

I got the solution!

  1. Tim, you’re right that the form does not need to pass on the mailitem object. Just open the form and let it return a value based on the user choice in the form.
  2. The problem was that GetInspector is only possible when the Inspector window is displayed. The problem occurred while opening the modal form because the process of NewInspector stopped pending execution of the procedures called before the actual message was displayed. Opening the form non modal did not interfere with the NewInspector procedure. Because some procedures included modification of the message body (which needed GetInspector), such procedures are only available after the event Inspector.Activate (See example on https://www.vboffice.net/en/developers/newinspector-and-inspector-activate/)

The adjusted procedures are:

Private WithEvents objOpenInsp As Outlook.Inspector
Private WithEvents objInsp As Outlook.Inspectors

Private Sub objInsp_NewInspector(ByVal Inspector As Outlook.Inspector)
    If TypeName(Inspector.CurrentItem) = "MailItem" Then
        Set objOpenInsp = Inspector
    End If
End Sub

Private Sub objOpenInsp_Activate()
    Dim objItem As Outlook.MailItem
    Dim frm As frmLanguage
    Dim lngLang As Long
    
    Set objItem = objOpenInsp.CurrentItem
    If Len(objItem.EntryID) = 0 And Len(objItem.Subject) = 0 Then 
        'This procedure is only for new email messages. 
        'Note that if you leave the subject line empty and try to close
        'the objItem window, the Activate event is triggered too and may 
        'get stuck in a loop.
        Set frm = New frmLanguage
        frm.Show
        lngLang = frm.lngRetValue
        Unload frm
        If lngLang > 0 Then
            'MyNewMessage Inspector.CurrentItem, lngLang
            MyNewMessage objItem, lngLang
        Else 'User pressed Cancel or closed the form
            objItem.Subject = " " 'prevent loop
            objItem.Close olDiscard
        End If
    End If
    Set frm = Nothing
    Set objItem = Nothing
End Sub

Again, thx for your help!

Reasons:
  • Blacklisted phrase (1): thx
  • Long answer (-1):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: Art