0

I want to keep this in VBA. I'm seeking info on how to work around the following issue.

I get this error: The item's properties and methods cannot be used inside this event procedure. MS has stopped people being able to use the .Close, .Move and .Delete methods in the Inspector_Close event. I've seen suggestions to use threading to run a delayed macro, but can't find help on this, and suspect it may not be available in VBA.

My code is as follows:

Private Sub objInspector_Close()
Dim objNS As NameSpace
Dim objFolder As MAPIFolder
'On Error Resume Next
Set objNS = Application.Session

If Not mailSent Then
    If objInspector.CurrentItem.Class = olMail Then
        'Mail inspector is closing
        If objInspector.CurrentItem.Parent = "Inbox" Then   
            Set objFolder = objNS.PickFolder
            If Not objFolder Is Nothing And IsInDefaultStore(objFolder) _
               And objFolder.DefaultItemType = olMailItem Then
                Set objInspector.CurrentItem.Move = objFolder
            End If
        End If
    End If
Else
    mailSent = False
End If

Set objFolder = Nothing
Set objNS = Nothing
End Sub

The global mailSent Boolean is there to prevent this event code executing when I send / close an email.

The error occurs on Set objInspector.CurrentItem.Move = objFolder. Is there a way for me to delay this until the event ends or perhaps to set some flags on the email item and then run a macro over all emails in my inbox later to move them to folders.

I work on multiple projects and maintain multiple email folders and am looking for ways to automate my email management. I've seen other pages where folder names are derived from email subjects but I don't want to do that.

Thanks for your help.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
0123KT
  • 1

2 Answers2

0

You may consider adding a user property which can mark the message for moving etc. Then you can use the Find/FindNext or Restrict methods for searching marked items. You can read more about these methods in the following articles:

Also you can use the GetTable method of the Folder class which obtains a Table object that contains items filtered by Filter.

As you probably know the Outlook object model can't be used from another threads. You need to use a low-level API - Extended MAPI which supports secondary threads. Or any other third-party wrappers around that API, for example - Redemption.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • Thanks. The Find/ FindNext link you provided seems to be faulty. I'm looking for some nice easy VBA code. I'll have a try with the Restrict method and setting a user property to use in a macro I activate periodically. – 0123KT Feb 17 '15 at 03:03
0

You could abandon the idea of using a trigger and move "manually"

Option Explicit

Private Sub MoveCurrentItem()

Dim objNS As Namespace
Dim objFolder As folder
Dim currItem As Object
Dim uPrompt As String

Set objNS = Application.Session

On Error Resume Next
Set currItem = ActiveInspector.currentItem
On Error GoTo 0
If currItem Is Nothing Then GoTo ExitRoutine

If currItem.Class = olMail Then
    If currItem.Sent Then ' reading not composing
        If currItem.Parent = "Inbox" Then
            Set objFolder = objNS.PickFolder
            If Not objFolder Is Nothing And IsInDefaultStore(objFolder) _
               And objFolder.DefaultItemType = olMailItem Then
                currItem.Move objFolder
            End If
        End If
    End If
End If

ExitRoutine:
    Set currItem = Nothing
    Set objFolder = Nothing
    Set objNS = Nothing

End Sub
niton
  • 8,771
  • 21
  • 32
  • 52