3

I have a set of macros that have 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 key words. The problem is, if I just started Outlook, and I bring up a new email or reply - the default in Outlook 2013 is to bring it into the former "Reading Pane" rather than in a new window. If I do not hit "Pop Out" and I try to send, my macro crashes with this error:

"Run-time error '91' Object variable or with block variable not set"

I tried to check for loading the form first - but it seem ANY call to my userform, even userform.show, generates this error.

Oddly, if I remember to "Pop Out" my first email, it runs fine everytime after until I close/reopen Outlook. Even if I don't "Pop Out" other emails. It's only on the very first one that this occurs.

Here's the beginning of my Initialize 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's 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 I changed Set email = Application.ActiveInspector.CurrentItem to Set email = Application.ActiveExplorer.Selection.Item(1). This seems to have fixed it, but the VBA help states "Do not make any assumptions about the Item method return type; your code should be able to handle multiple item types or a ConversationHeader object."

Note that the form is being invoked by the ItemSend event.

Community
  • 1
  • 1
Trashman
  • 1,424
  • 18
  • 27
  • 2
    It might help to see your actual code, with the error-throwing line... – Mathieu Guindon Nov 04 '14 at 16:42
  • TagMsg.show - my form is named "TagMsg" - any call to it, even just .show causes the error – Trashman Nov 13 '14 at 21:28
  • 2
    If you have any code in the `Initialize` or `Activate` or `Layout` handlers, it would help us help you if you included it here. – Mathieu Guindon Nov 13 '14 at 21:32
  • 1
    What code runs when you "pop out" an email? – Mathieu Guindon Nov 13 '14 at 23:38
  • no code runs when I "pop out" an email. The code only runs when I hit "send" – Trashman Dec 05 '14 at 19:43
  • 2
    It might help to actually embed some code into your post... – Mathieu Guindon Dec 05 '14 at 19:44
  • thanks, retailcoder... I have a call to Application.ActiveInspector.CurrentItem in my Initialize event that's actually causing the error. I forgot that the highlighted line may be a level up from the actual error, I added breakpoints to find it. I guess it's not using an "ActiveInspector" object when it's in the preview window... – Trashman Dec 05 '14 at 19:49
  • 1
    Feel free to [edit] your post any time; as it stands (without the code) it's unlikely that your question is answerable :( – Mathieu Guindon Dec 05 '14 at 20:04
  • 1
    I changed Set email = Application.ActiveInspector.CurrentItem to Set email = Application.ActiveExplorer.Selection.Item(1) and it appears to be working. Unfortunately, activeinspector.currentitem used to have the guarantee of being the active, open email. Now neither of these do. How can I be sure I have the right object? email is an object of type mailitem. – Trashman Dec 05 '14 at 20:12

1 Answers1

1

First off, putting that code into the Initialize event wasn't a good move. Needed to be moved into a click event where it was actually needed.

Then, I found the code I needed from two other posts, combined and shortened them.

Working with current open email

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's 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: This still relies on the fact that it is called by the ItemSend event and the active or current item will be the email I just pressed "send" on.

Thank you, retailcoder for your comments.

Community
  • 1
  • 1
Trashman
  • 1,424
  • 18
  • 27