A Microsoft Outlook email forum. Outlook Banter

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » Outlook Banter forum » Microsoft Outlook Email Newsgroups » Outlook and VBA
Site Map Home Register Authors List Search Today's Posts Mark Forums Read Web Partners

Move email using the Close event



 
 
Thread Tools Search this Thread Display Modes
  #11  
Old July 22nd 09, 04:52 PM posted to microsoft.public.outlook.program_vba
Alan Moseley
external usenet poster
 
Posts: 61
Default Move email using the Close event

I do apologise, I missed a line of code when cpying and pasting. The
contents of Class1 should be:-

Dim WithEvents myMailItem As MailItem
Dim WithEvents myInspectors As Inspectors
Dim myDestFolder As MAPIFolder
Private Sub Class_Initialize()
Set myInspectors = Outlook.Inspectors
Set myDestFolder =
Outlook.GetNamespace("MAPI").GetDefaultFolder(olFo lderInbox).Folders("Processed")
End Sub
Private Sub myInspector_NewInspector(ByVal Inspector As Inspector)
Dim myItem As Object
Set myItem = Inspector.CurrentItem
If TypeName(myItem) = "MailItem" Then
Set myMailItem = myItem
End If
Set myItem = Nothing
End Sub
Private Sub Class_Terminate()
Set myDestFolder = Nothing
Set myInspectors = Nothing
End Sub
Private Sub myMailItem_Close(Cancel As Boolean)
myMailItem.Move
End Sub

--
Alan Moseley IT Consultancy
http://www.amitc.co.uk

If I have solved your problem, please click Yes below. Thanks.


"Alan Moseley" wrote:

It will do because you are not correctly setting myItem in the first place.
Create yourself a new Class (let's say it's called Class1) and insert the
following code into it:-

Dim WithEvents myInspectors As Inspectors
Dim myDestFolder As MAPIFolder
Private Sub Class_Initialize()
Set myInspectors = Outlook.Inspectors
Set myDestFolder =
Outlook.GetNamespace("MAPI").GetDefaultFolder(olFo lderInbox).Folders("Processed")
End Sub
Private Sub myInspector_NewInspector(ByVal Inspector As Inspector)
Dim myItem As Object
Set myItem = Inspector.CurrentItem
If TypeName(myItem) = "MailItem" Then
Set myMailItem = myItem
End If
Set myItem = Nothing
End Sub
Private Sub Class_Terminate()
Set myDestFolder = Nothing
Set myInspectors = Nothing
End Sub
Private Sub myMailItem_Close(Cancel As Boolean)
myMailItem.Move mtDestFolder
End Sub

Now in the ThisOutlookSession code window include the following code:-

Private Sub Application_Quit()
Set myClass = Nothing
End Sub

Private Sub Application_Startup()
Set myClass = New Class1
End Sub

This code will currently move any mailitem opened to be moved to the
processed folder, so you will need to think about including some checking
(such as the item isnt already in the processed folder).

--
Alan Moseley IT Consultancy
http://www.amitc.co.uk

If I have solved your problem, please click Yes below. Thanks.


"Vasil Vasilev" wrote:

I tried to remove this... still no luck

The process fails on trying to execute
myItem.Move destFolder


"Alan Moseley" wrote:

I think I see your problem. When you open Outlook you are setting myItem to
be the CurrentItem of the ActiveInspector (within the Application_Startup
Sub). Surely when you open outlook you won't have an ActiveInspector. You
need to set myItem when opening a MailItem surely?

--
Alan Moseley IT Consultancy
http://www.amitc.co.uk

If I have solved your problem, please click Yes below. Thanks.


"Vasil Vasilev" wrote:

Hi Alan,

thanks for the answer.
Unfortunatelly the problem still persists. No change...


"Alan Moseley" wrote:

You are creating a separate process for your myApp object, which you don't
need and will cause problems. Your code doesn't actaully use the myApp
object anywhere so you don't need any of them, but if other areas of your
code do make use of them then change :-

Dim myApp As New Outlook.Application
to
Dim myApp As Outlook.Application

next change

Set myApp = CreateObject("Outlook.Application")
to
set myApp = Outlook

Lastly change

Set objNS = GetNamespace("MAPI")
to
Set objNS = Outlook.GetNamespace("MAPI")
or
Set objNS = myApp.GetNamespace("MAPI")

Does this prevent the problem?

--
Alan Moseley IT Consultancy
http://www.amitc.co.uk

If I have solved your problem, please click Yes below. Thanks.

Ads
  #12  
Old July 28th 09, 07:33 AM posted to microsoft.public.outlook.program_vba
Vasil Vasilev
external usenet poster
 
Posts: 6
Default Move email using the Close event

Alan,

thanks a lot for the code, but it still doesn't work

Meanwhile I found this...
http://support.microsoft.com/kb/929593/en-us

Finally, I end up writing an add-in, which does what I want on button click!

Thank you all for your help

Regards,
Vasil


"Alan Moseley" wrote:

I do apologise, I missed a line of code when cpying and pasting. The
contents of Class1 should be:-

Dim WithEvents myMailItem As MailItem
Dim WithEvents myInspectors As Inspectors
Dim myDestFolder As MAPIFolder
Private Sub Class_Initialize()
Set myInspectors = Outlook.Inspectors
Set myDestFolder =
Outlook.GetNamespace("MAPI").GetDefaultFolder(olFo lderInbox).Folders("Processed")
End Sub
Private Sub myInspector_NewInspector(ByVal Inspector As Inspector)
Dim myItem As Object
Set myItem = Inspector.CurrentItem
If TypeName(myItem) = "MailItem" Then
Set myMailItem = myItem
End If
Set myItem = Nothing
End Sub
Private Sub Class_Terminate()
Set myDestFolder = Nothing
Set myInspectors = Nothing
End Sub
Private Sub myMailItem_Close(Cancel As Boolean)
myMailItem.Move
End Sub

--
Alan Moseley IT Consultancy
http://www.amitc.co.uk

If I have solved your problem, please click Yes below. Thanks.


"Alan Moseley" wrote:

It will do because you are not correctly setting myItem in the first place.
Create yourself a new Class (let's say it's called Class1) and insert the
following code into it:-

Dim WithEvents myInspectors As Inspectors
Dim myDestFolder As MAPIFolder
Private Sub Class_Initialize()
Set myInspectors = Outlook.Inspectors
Set myDestFolder =
Outlook.GetNamespace("MAPI").GetDefaultFolder(olFo lderInbox).Folders("Processed")
End Sub
Private Sub myInspector_NewInspector(ByVal Inspector As Inspector)
Dim myItem As Object
Set myItem = Inspector.CurrentItem
If TypeName(myItem) = "MailItem" Then
Set myMailItem = myItem
End If
Set myItem = Nothing
End Sub
Private Sub Class_Terminate()
Set myDestFolder = Nothing
Set myInspectors = Nothing
End Sub
Private Sub myMailItem_Close(Cancel As Boolean)
myMailItem.Move mtDestFolder
End Sub

Now in the ThisOutlookSession code window include the following code:-

Private Sub Application_Quit()
Set myClass = Nothing
End Sub

Private Sub Application_Startup()
Set myClass = New Class1
End Sub

This code will currently move any mailitem opened to be moved to the
processed folder, so you will need to think about including some checking
(such as the item isnt already in the processed folder).

--
Alan Moseley IT Consultancy
http://www.amitc.co.uk

If I have solved your problem, please click Yes below. Thanks.


"Vasil Vasilev" wrote:

I tried to remove this... still no luck

The process fails on trying to execute
myItem.Move destFolder


"Alan Moseley" wrote:

I think I see your problem. When you open Outlook you are setting myItem to
be the CurrentItem of the ActiveInspector (within the Application_Startup
Sub). Surely when you open outlook you won't have an ActiveInspector. You
need to set myItem when opening a MailItem surely?

--
Alan Moseley IT Consultancy
http://www.amitc.co.uk

If I have solved your problem, please click Yes below. Thanks.


"Vasil Vasilev" wrote:

Hi Alan,

thanks for the answer.
Unfortunatelly the problem still persists. No change...


"Alan Moseley" wrote:

You are creating a separate process for your myApp object, which you don't
need and will cause problems. Your code doesn't actaully use the myApp
object anywhere so you don't need any of them, but if other areas of your
code do make use of them then change :-

Dim myApp As New Outlook.Application
to
Dim myApp As Outlook.Application

next change

Set myApp = CreateObject("Outlook.Application")
to
set myApp = Outlook

Lastly change

Set objNS = GetNamespace("MAPI")
to
Set objNS = Outlook.GetNamespace("MAPI")
or
Set objNS = myApp.GetNamespace("MAPI")

Does this prevent the problem?

--
Alan Moseley IT Consultancy
http://www.amitc.co.uk

If I have solved your problem, please click Yes below. Thanks.

 




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Inspecter Close Event dch3 Outlook and VBA 2 August 27th 07 11:34 AM
MailItem Close Event Robert Morley Outlook and VBA 8 April 19th 07 09:03 PM
Explorer' Close event not fired j Add-ins for Outlook 9 April 2nd 07 02:24 PM
Inspector Close Event is fired when spell checking is canceled. Arcady Outlook and VBA 6 December 28th 06 02:38 PM
Cancelling Outlook.Application.ActiveExplorer.Close event? Nathan Add-ins for Outlook 1 September 8th 06 07:14 PM


All times are GMT +1. The time now is 10:57 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.Search Engine Friendly URLs by vBSEO 2.4.0
Copyright ©2004-2025 Outlook Banter.
The comments are property of their posters.