Error when trying to forward current email item

reubendayal

Member
Outlook version
Outlook 2016 32 bit
Email Account
Exchange Server 2013
Hi All,

I have created the below macro with all the wonderful posts from the Sliptick.com site, to use the current email in the active explorer and forward it with slight changes in the subject line and adding a message to the specific receivers.

But I seem to be receiving an error message each time I run this code and then after I select the debug option in the error message, and run the macro again, the macro works.

Code:
Public Sub ApprovalUpdate()


Dim objItem As Outlook.MailItem
Dim omail As Object
Dim NewEmail As MailItem
Dim Remail, AprMsg, Emsg, Remail2, Family As String
Dim NB, NB2 As Boolean

'***
'several steps to show a form for the user to insert/select specific info to include in the email message, etc.
'And IF statements that collect the right information to put in the email - All working well'
'****

Set objItem = GetCurrentItem()

Set omail = objItem.Forward

Set NewEmail = objItem.Forward

With NewEmail

    ....do whateever....
    .Body = Emsg & .Body
    .Recipients.ResolveAll
    .Display 'Send
End With

Unload ApprovalReceivers

Set objItem = Nothing
Set NewEmail = Nothing
Set omail = Nothing

MainAppl = ""

End Sub


Function GetCurrentItem() As Outlook.MailItem
    Dim objApp As Outlook.Application
          
    Set objApp = Application
    On Error Resume Next
    Select Case TypeName(objApp.ActiveWindow)
        Case "Explorer"
            Set GetCurrentItem = objApp.ActiveExplorer.Selection.Item(1)
        Case "Inspector"
            Set GetCurrentItem = objApp.ActiveInspector.CurrentItem
    End Select
      
    Set objApp = Nothing
End Function
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
What line does it stop on?

I have 3 errors, 2 might be just because I don't have your full code. With all 3 commented out, it works.

With NewEmail

'....do whateever.... <<== is not commented out in the code you posted. Delete the line or comment it out.
.Body = Emsg & .Body
.Recipients.ResolveAll
.Display 'Send
End With

'Unload ApprovalReceivers <== I don't have this userform

Set objItem = Nothing
Set NewEmail = Nothing
Set omail = Nothing

'MainAppl = "" <<== don't know what this is, possibly something in the userform.
 

reubendayal

Member
Outlook version
Outlook 2016 32 bit
Email Account
Exchange Server 2013
What line does it stop on?

I have 3 errors, 2 might be just because I don't have your full code. With all 3 commented out, it works.

With NewEmail

'....do whateever.... <<== is not commented out in the code you posted. Delete the line or comment it out.
.Body = Emsg & .Body
.Recipients.ResolveAll
.Display 'Send
End With

'Unload ApprovalReceivers <== I don't have this userform

Set objItem = Nothing
Set NewEmail = Nothing
Set omail = Nothing

'MainAppl = "" <<== don't know what this is, possibly something in the userform.

Hi Diane,

Thank you for replying.

I am sorry I didn't explain the full code very well.

The error is Run-Time error '2287': Application-defined or object-defined error. And the error always stops at the line -
Set omail = objItem.Forward

And when I go in to the debugger, and hover the cursor on omail or NewEmail, it shows them to be = Nothing. But by then the macro has created a forward of the original email item, without making any of the changes I want it to do via the With statement. And while I am in the debugger, if I hit F5, the macro runs again, and this time it would execute just as planned and the new email (forward) is created just as planned.

So to my non programmer knowledge, it appears like in the first try NewEmail is Nothing, but when I rerun the macro in the debugger it sort of gets focus on the email again and works fine.

Thanks.

Reuben
 

reubendayal

Member
Outlook version
Outlook 2016 32 bit
Email Account
Exchange Server 2013
Hi Diane,

Thank you for replying.

I am sorry I didn't explain the full code very well.

The error is Run-Time error '2287': Application-defined or object-defined error. And the error always stops at the line -
Set omail = objItem.Forward

And when I go in to the debugger, and hover the cursor on omail or NewEmail, it shows them to be = Nothing. But by then the macro has created a forward of the original email item, without making any of the changes I want it to do via the With statement. And while I am in the debugger, if I hit F5, the macro runs again, and this time it would execute just as planned and the new email (forward) is created just as planned.

So to my non programmer knowledge, it appears like in the first try NewEmail is Nothing, but when I rerun the macro in the debugger it sort of gets focus on the email again and works fine.

Thanks.

Reuben
Note: a quick correction - it is Run-Time error '287'.
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
i just noticed this:
Set omail = objItem.Forward

Set NewEmail = objItem.Forward


Remove the omail lines - you aren't using that object. It didn't cause any errors for me though.


When you hover over objitem, does it show the subject? If the macro stopped on set omail, newemail will be nothing.
Set objItem = GetCurrentItem()
 

reubendayal

Member
Outlook version
Outlook 2016 32 bit
Email Account
Exchange Server 2013
i just noticed this:
Set omail = objItem.Forward

Set NewEmail = objItem.Forward


Remove the omail lines - you aren't using that object. It didn't cause any errors for me though.


When you hover over objitem, does it show the subject? If the macro stopped on set omail, newemail will be nothing.
Set objItem = GetCurrentItem()
Tried that too... but the same result - Run-Time error '287' .
 

reubendayal

Member
Outlook version
Outlook 2016 32 bit
Email Account
Exchange Server 2013
Tried that too... but the same result - Run-Time error '287' .
I have a solution for the problems, finally!

I have two other macros in the "ThisOutlookSession", one is for the ReplyAll event and the other an oItem_Forward. And the item forward was triggering before the intended macro or something like that as at the end of that macro the clean up routine of setting the email object to nothing was the reason.

So I added a global integer in my other macro and added an If statement at the start of the Item.forward macro to exit sub on a certain value of the global integer.

And now my other macro seems to work as normal.

Thank you anyway Diane.
 
Top