• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1075
  • Last Modified:

MailItem.Close error

Hi.

I have written a little AddIn for Outlook 2007 and I try to adapt it to Outlook 2003. It is looking good but I have a problem when closing the MailItem. Sample of the code below


It says that I cannot close within ItemSend. I looked around and I also tried with currentMailItem.GetInspector.Close(1) without any luck (just another error).

Anyone has an idea?

Tx


Geoffrey
Case Windows.Forms.DialogResult.Yes
    Cancel = True
    currentMailItem.Close(1)
    Exit Select
Case Windows.Forms.DialogResult.No
    Cancel = False
    Exit Select

Open in new window

0
__geof__
Asked:
__geof__
  • 6
  • 5
2 Solutions
 
Chris BottomleyCommented:
I wouldn't think you need the line
    currentMailItem.Close(1)
try commenting it out ... the send event handler ought to see the cancel status and close the object without sending.

Chris
0
 
__geof__Author Commented:
Tried this. The sending is canceled but the mail is still open.
0
 
Chris BottomleyCommented:
I'm no expert in VSTO but can you provide a larger code sample of the itemsend handler - perhaps something will be apparent?

Chris
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
__geof__Author Commented:
I attach most of the code below...
Public Class ThisAddIn
 
    Private Sub ThisAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup
 
    End Sub
 
    Private Sub ThisAddIn_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shutdown
 
    End Sub
 
    Private Sub Application_ItemSend(ByVal Item As Object, ByRef Cancel As Boolean) Handles Application.ItemSend
        Try
            Dim currentMailItem = CType(Me.Application.ActiveInspector.CurrentItem, Outlook.MailItem)
            Dim oFolder As Object
            Dim oFolderFastMail As Object
            Dim olNs As Outlook.NameSpace
            olNs = Application.GetNamespace("MAPI")
            oFolder = olNs.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail)
            Try
                oFolderMail = oFolder.Folders.Add("xxx")
            Catch ex As Exception
                oFolderMail = oFolder.Folders.Item("xxx")
            End Try
            If (currentMailItem.Attachments.Count > 0) Then
                ...getting size of attachments and do some stuff with it/them...
                If (size > 4000) Then
                    Dim dr As Windows.Forms.DialogResult
                    size /= 1048576
                    dr = MsgBox("Do you want to send with xxx? Size = " + size.ToString + " MB", MsgBoxStyle.YesNo, "Sending attachement(s)")
                    Select Case dr
                        Case Windows.Forms.DialogResult.Yes
                            Cancel = True
                            ...send the attachment(s) with our technology...
                            Dim c As Outlook.MailItem = currentMailItem.Copy()
                            c.Move(oFolderMail)
                            currentMailItem.Close(1) 'HERE IS THE ERROR
                            'currentMailItem.GetInspector.Close(Outlook.OlInspectorClose.olDiscard) THIS ONE NOT WORKING EITHER
                            Exit Select
                        Case Windows.Forms.DialogResult.No
                            Cancel = False
                            Exit Select
                    End Select
                End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
 
End Class

Open in new window

0
 
Chris BottomleyCommented:
Applying a lay eye, currentMailItem  is set to item.  In this case item is passed by value so perhaps pass by reference will make the object accessible?

Chris
0
 
__geof__Author Commented:
I'm new to VB but I understand what you say as changing Private Sub Application_ItemSend(ByVal Item As Object, ByRef Cancel As Boolean) Handles Application.ItemSend to Private Sub Application_ItemSend(ByRef Item As Object, ByRef Cancel As Boolean) Handles Application.ItemSend
But I cannot do this because ItemSend requires it this way.

Or did I misunderstand? Or there are other ways to do it?
0
 
Chris BottomleyCommented:
You are correct ... I had seen something which suggested it would be possible but certainly in teh equivalent VBA construct it wouldn't be allowed so i'm surprised.  SImply thought it was worth a quick check in case.

THinking about the outlook equivalent ... exactly the same happens so I am thinking in that domain to see what the answer is.

Chris
0
 
Chris BottomleyCommented:
Chris

Logically the send is being triggered by a piece of code in this instance ... is this so and if it is then after the send event is triggered check casncel and close the mailitem if set.

If not then what can you say about the process involved?

Chris
0
 
__geof__Author Commented:
SendItem must be triggered somewhere but it is not a bit of code I wrote, still plain old Outlook code there. But it is a good idea to check how this could be overridden. Someone knows how ItemSend is called?
0
 
Chris BottomleyCommented:
Itemsend is triggered within outlook not surprisingly when the send button is pressed directly or through code.

I have no idea though how VST Application_ItemSend function maps onto the outlook VBA Private Sub Application_ItemSend function.

Chris
0
 
__geof__Author Commented:
I got a workaround from Ken Slovak.

I create a timer that I start right before ItemSend finishes and create a method that handles Timer.Elapsed where I can use currentMailItem.GetInspector.Close(Outlook.OlInspectorClose.olDiscard)
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now