Outlook 2013 custom form with block Error if not "popped out"
I have a set of macros that worked in Outlook 2003, 2007 and 2010. In fact, it still works in 2013 except in a specific case.
The macro brings up a dialog box whenever you try to send an email to tag the subject line with keywords. The problem is that if I have just started Outlook and I open a new message or reply, by default, Outlook 2013 will bring it to the old "reading pane" and not to a new window. If I don't hit "Pop Out" and I try to submit, my macro crashes with this error:
"Runtime error" 91 "Object variable or block variable not set"
I tried to test the form loading first, but it seems to ANY call to my custom form, even userform.show, generates this error.
Oddly enough, if I remember "Pop Out" sent my first email, it works fine every time after I close / open Outlook. Even if I don't leave other emails. This happens only in the very first case.
Here's the start of my Trigger Event:
Dim Tags() As String
Dim T As Variant
Dim PC As Variant
Dim Rent As String
Dim Child As String
Dim nsourcefile As Integer
Dim email As MailItem
Dim PD As Variant
Dim Proj As String
Dim Desc As String
'Set email = Application.ActiveInspector.CurrentItem
Set email = Application.ActiveExplorer.Selection.Item(1)
'Checks to see if a project number (that not on the list) may be in the subject already
If Val(email.Subject) > 10000 Then
TagMsg.Height = tall
TagMsg.NewProjID = Format(Val(email.Subject), "00000")
TagMsg.NewProjDesc.SetFocus
Else
'Set height of form (prior to pressing "More" button
TagMsg.Height = short
End If
Noticed that I changed Set email = Application.ActiveInspector.CurrentItem to set email = Application.ActiveExplorer.Selection.Item (1). This seems to be fixed, but the VBA help says, "Don't make any assumptions about the return type of the Item method, your code should be able to handle multiple item types or a ConversationHeader object."
Note that the form is called by the ItemSend event.
source to share
First, including this code in the Initialize event was not a good move. It was necessary to move to the click event where it was really needed.
Then I found the code I needed from the other two posts, combined and shortened them.
Working with the current open letter
https://superuser.com/questions/795831/outlook-2013-vba-refer-to-editor-in-reading-pane
Final result
Dim oInspector As Inspector
Dim email As MailItem
Dim oexp As Explorer
Set oInspector = Application.ActiveInspector
Set oexp = Application.ActiveExplorer
If oInspector Is Nothing Then
'Set email = Application.ActiveExplorer.Selection.Item(1)
Set email = oexp.ActiveInlineResponse
If email Is Nothing Then
'MsgBox "No active inspector or inline response"
Exit Sub
End If
Else
Set email = oInspector.CurrentItem
End If 'oInspector is Nothing
If email.Sent Then
'MsgBox "This is not an editable email"
Else
'Checks to see if a project number (that not on the list) may be in the subject already
If Val(email.Subject) > 10000 Then
TagMsg.Height = tall
TagMsg.NewProjID = Format(Val(email.Subject), "00000")
TagMsg.NewProjDesc.SetFocus
Else
'Set height of form (prior to pressing "More" button
TagMsg.Height = short
End If
End If 'email.sent
Note. It still depends on the fact that it is being triggered by the ItemSend event and the active or current item will be the email I just clicked send to.
Thanks retailcoder for your comments.
source to share