Discussion:
Help writing a macro to move emails
(too old to reply)
l***@skiz.net
2008-02-06 07:04:06 UTC
Permalink
Hi:
I already have some macros which move emails to predetermined folders
and these macros are run with a keyboard shortcut. It works fine.

I'd like to write a macro which will do the following: When I close
an email (and go to the next one), I'd like to move the email I just
closed to folder xyz. So if i did not flag the current email or move
it to another folder, I want my macro to move it for me :).

Is this possible?

Thanks
-Ed
Anand.V.V.N
2008-02-06 13:39:01 UTC
Permalink
Hello,
You can use the same macro that you are using to move the mail to different
folder, just it in a different event, I am not sure which event is fired when
you move to a different mail item, do check that.
Anand
--
"Who will guard the guards?"
Post by l***@skiz.net
I already have some macros which move emails to predetermined folders
and these macros are run with a keyboard shortcut. It works fine.
I'd like to write a macro which will do the following: When I close
an email (and go to the next one), I'd like to move the email I just
closed to folder xyz. So if i did not flag the current email or move
it to another folder, I want my macro to move it for me :).
Is this possible?
Thanks
-Ed
unknown
2010-01-22 13:49:54 UTC
Permalink
i got this code http://www.telekawaru.com/outlookimap/
it looks for an event on delete and then move the deleted email to TRASH while the email is hidden in the folder it was deleted. purging happens later.

when i compile it in outlook 2003 i get "user defined type not define". i can not figure out what is missing. think there is a reference missing, but do not know which one.

i am not a script writer. did normal c programming 10 years back.

do you know what is wrong?

thanks



AnandVV wrote:

RE: Help writing a macro to move emails
06-Feb-08

Hello,
You can use the same macro that you are using to move the mail to different
folder, just it in a different event, I am not sure which event is fired when
you move to a different mail item, do check that.
Anand
--
"Who will guard the guards?"


"***@skiz.net" wrote:

Previous Posts In This Thread:

On Wednesday, February 06, 2008 8:39 AM
AnandVV wrote:

RE: Help writing a macro to move emails
Hello,
You can use the same macro that you are using to move the mail to different
folder, just it in a different event, I am not sure which event is fired when
you move to a different mail item, do check that.
Anand
--
"Who will guard the guards?"


"***@skiz.net" wrote:

On Wednesday, February 06, 2008 9:20 AM
Ken Slovak - [MVP - Outlook] wrote:

Re: Help writing a macro to move emails
If the item is open and being closed then the Inspector it's in is also
closing. Handle the NewInspector event of the Inspectors collection and
handle the Inspector.Close event or the MailItem.Close event for the
MailItem in the Inspector to do what you want.
--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007
Reminder Manager, Extended Reminders, Attachment Options
http://www.slovaktech.com/products.htm


<***@skiz.net> wrote in message news:e01bfc11-ee02-4e4c-a7f8-***@s37g2000prg.googlegroups.com...

On Wednesday, February 06, 2008 4:10 PM
Ken Slovak - [MVP - Outlook] wrote:

Re: Help writing a macro to move emails
You can't set the value of an object to a string value.

You need to do everything I mentioned, handling
Application.Inspectors.NewInspector and testing for
Inspector.CurrentItem.Class to be olMail. That will give you a handle to the
mail item and then you need to instantiate your class so it can handle the
Close event of the mail item.
--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007
Reminder Manager, Extended Reminders, Attachment Options
http://www.slovaktech.com/products.htm


<***@skiz.net> wrote in message news:e92ccb88-7461-43f2-aaaa-***@e4g2000hsg.googlegroups.com...

On Thursday, February 07, 2008 1:12 AM
landa wrote:

Help writing a macro to move emails
Hi:
I already have some macros which move emails to predetermined folders
and these macros are run with a keyboard shortcut. It works fine.

I'd like to write a macro which will do the following: When I close
an email (and go to the next one), I'd like to move the email I just
closed to folder xyz. So if i did not flag the current email or move
it to another folder, I want my macro to move it for me :).

Is this possible?

Thanks
-Ed

On Thursday, February 07, 2008 1:12 AM
landa wrote:

Re: Help writing a macro to move emails
Thanks. I've tried that. This is what I have:
In a Class Module call EdClass:
Public WithEvents myEdItem As Outlook.MailItem
Private Sub Class_Initialize()
Set myItem = Application.ActiveInspector.CurrentItem
End Sub
Private Sub myEdItem_Close(Cancel As Boolean)
If Not myItem.Saved Then
MsgBox " The item was closed."
End If
End Sub


Then in a standard Module, I have:
Dim myClass As New EdClass
Sub Register_Event_Handler()
Set myClass.myEdItem = "Outlook.mailitem"

End Sub


But I get an error that an Object is needed when trying to instantiate
the class (set myClass...).

Any thoughts?

Thanks
-Ed

On Thursday, February 07, 2008 1:12 AM
landa wrote:

Re: Help writing a macro to move emails
'Hope I'm not pushing here :). So I'm taking small steps. I tried to
write something to test to see if the current inspector was mail (as
you suggested). I believe if I can get this down, the rest will be
easy. I guess I'm not grok;ing the class thing. I took a C++ class a
while back and was good at it. In this case, I'm not sure if the class
gets instantiated with the "Set" command... and if so, what is its
"handle" ?

Class:EdInspectorEventClass
Public WithEvents myOlInspectors As Outlook.Inspectors
Public Sub Initialize_handler()
Set myOlInspectors = Application.Inspectors
End Sub
Private Sub myOlInspectors_NewInspector(ByVal Inspector As
Outlook.Inspector)
If (Inspector.CurrentItem.Class = olMail) Then
MsgBox ("You're in mail.")
End If
End Sub

In Module2:
Sub DoSomething()
Set EdInspectorEventClass = Outlook.Inspectors
EdInspectorEventClass.Initialize_handler ' *****This is where
it fails and says object does not support this property.*****
End Sub

On Thursday, February 07, 2008 9:38 AM
Ken Slovak - [MVP - Outlook] wrote:

Re: Help writing a macro to move emails
Class:EdInspectorEventClass
Public WithEvents myOlInspectors As Outlook.Inspectors
Public WithEvents myMail As Outlook.MailItem

Public Sub Initialize_handler()
Set myOlInspectors = Application.Inspectors
End Sub

Private Sub myOlInspectors_NewInspector(ByVal Inspector As
Outlook.Inspector)
If (Inspector.CurrentItem.Class = olMail) Then
MsgBox ("You're in mail.")
Set myMail = Inspector.CurrentItem
End If
End Sub

Private Sub myMail_Close(Cancel As Boolean)
' do whatever you want
End Sub

In Module2:

Public myClass As EdInspectorEventClass
Sub DoSomething()
Set EdInspectorEventClass = New EdInspectorEventClass
EdInspectorEventClass.Initialize_handler
End Sub

To start things off the DoSomething procedure must be called to instantiate
an instance of the EdInspectorEventClass class and to call
EdInspectorEventClass.Initialize_handler. From there the code in
EdInspectorEventClass will run and handle any NewInspector event.

Of course you will also need to release your class at some point (set it to
Nothing). And the code as shown will handle one open Inspector at a time,
not multiple open Inspectors. For that you'd need to use a more complex
approach of wrapper classes stored in a collection.

Personally, if I was writing this and I wanted the code to start
automatically I'd put the Module2 code in the ThisOutlookSession class and
call DoSomething from the Application_Startup() event handler. I'd also
probably restructure it like this:

' move all this into ThisOutlookSession:
Public WithEvents myOlInspectors As Outlook.Inspectors
Public myInspectorsCollection As New Collection

Public Sub Initialize_handler()
Set myOlInspectors = Application.Inspectors
End Sub

Private Sub myOlInspectors_NewInspector(ByVal Inspector As
Outlook.Inspector)
If (Inspector.CurrentItem.Class = olMail) Then
MsgBox ("You're in mail.")
Dim oInspClass As New EdInspectorEventClass
Set oInspClass.myMail = Inspector.CurrentItem
myInspectorsCollection.Add oInspClass
End If
End Sub

Class:EdInspectorEventClass
Public WithEvents myMail As Outlook.MailItem

Private Sub myMail_Close(Cancel As Boolean)
' do whatever you want
End Sub

That will let you handle item.Close() on multiple open Inspectors and the
collection will keep each Inspector and Mail item alive.
--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007
Reminder Manager, Extended Reminders, Attachment Options
http://www.slovaktech.com/products.htm


<***@skiz.net> wrote in message news:ed3ed638-e5c7-4ef9-b114-***@b2g2000hsg.googlegroups.com...

On Thursday, February 07, 2008 12:59 PM
Ken Slovak - [MVP - Outlook] wrote:

Re: Help writing a macro to move emails
Each open item is an Inspector. A folder view is an Explorer. You can have
multiple Inspectors and Explorers open at the same time. The active members
are ActiveInspector and ActiveExplorer.

Never assume there will always be at least one Explorer or Inspector. There
are cases where you can have an Inspector with no Explorers and certainly no
Inspectors with at least one Explorer open.
--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007
Reminder Manager, Extended Reminders, Attachment Options
http://www.slovaktech.com/products.htm


<***@skiz.net> wrote in message news:a7c28288-af1f-473d-b20e-***@j78g2000hsd.googlegroups.com...

On Saturday, February 09, 2008 8:57 AM
landa wrote:

Re: Help writing a macro to move emails
Sorry.... I know that was wrong... but I'm stumped.

In Class called "EdClass"
Public WithEvents myOlInspectors As Outlook.Inspectors
Public Sub Initialize_handler()
Set myOlInspectors = Application.Inspectors
End Sub
Private Sub myOlInspectors_NewInspector(ByVal Inspector As
Outlook.Inspector)
If (Inspector.CurrentItem.Class = olMail) Then
MsgBox ("You're in mail.")
End If
End Sub


In Module2
Dim myClass As New EdClass
Sub Register_Event2_Handler()
Set myClass.myOlInspectors = Application.Inspectors 'gives me an
error.
End Sub

From the immediate window, I tried calling: myClass.Initialize_handler
and that gave me the same error.


HELP :)


Thanks
-Ed

On Saturday, February 09, 2008 8:57 AM
landa wrote:

Re: Help writing a macro to move emails
Thanks Ken. I modified your module2 code to read: myclass = new ...
nad it workked. I now understand it. Thanks.

Inspectors see to be seperate windows. Do you know what the built-in
inspector is called? I usually don't double-click on my emails to
bringup a seperate window for each but rather just view them in the
big outlook-inspector.

Thanks again for the clear explination. It really helped !!

-Ed

On Thursday, March 13, 2008 11:18 PM
sandeepthank wrote:

Re: Help writing a macro to move emails
On Feb 7, 10:59=A0pm, "Ken Slovak - [MVP - Outlook]"
<***@mvps.org> wrote:

s
e
no
ech.com/products.htm
...

can anyone please send me the whole code ...i would like to trry and
implement for my wrk ...


Submitted via EggHeadCafe - Software Developer Portal of Choice
Useful Vista Tweaks
http://www.eggheadcafe.com/tutorials/aspnet/f0247f48-7bc0-4981-b515-c80c5d6d00ec/useful-vista-tweaks.aspx
Ken Slovak - [MVP - Outlook]
2008-02-06 14:20:59 UTC
Permalink
If the item is open and being closed then the Inspector it's in is also
closing. Handle the NewInspector event of the Inspectors collection and
handle the Inspector.Close event or the MailItem.Close event for the
MailItem in the Inspector to do what you want.
--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007
Reminder Manager, Extended Reminders, Attachment Options
http://www.slovaktech.com/products.htm
Post by l***@skiz.net
I already have some macros which move emails to predetermined folders
and these macros are run with a keyboard shortcut. It works fine.
I'd like to write a macro which will do the following: When I close
an email (and go to the next one), I'd like to move the email I just
closed to folder xyz. So if i did not flag the current email or move
it to another folder, I want my macro to move it for me :).
Is this possible?
Thanks
-Ed
l***@skiz.net
2008-02-06 20:40:20 UTC
Permalink
Thanks. I've tried that. This is what I have:
In a Class Module call EdClass:
Public WithEvents myEdItem As Outlook.MailItem
Private Sub Class_Initialize()
Set myItem = Application.ActiveInspector.CurrentItem
End Sub
Private Sub myEdItem_Close(Cancel As Boolean)
If Not myItem.Saved Then
MsgBox " The item was closed."
End If
End Sub


Then in a standard Module, I have:
Dim myClass As New EdClass
Sub Register_Event_Handler()
Set myClass.myEdItem = "Outlook.mailitem"

End Sub


But I get an error that an Object is needed when trying to instantiate
the class (set myClass...).

Any thoughts?

Thanks
-Ed
Ken Slovak - [MVP - Outlook]
2008-02-06 21:10:12 UTC
Permalink
You can't set the value of an object to a string value.

You need to do everything I mentioned, handling
Application.Inspectors.NewInspector and testing for
Inspector.CurrentItem.Class to be olMail. That will give you a handle to the
mail item and then you need to instantiate your class so it can handle the
Close event of the mail item.
--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007
Reminder Manager, Extended Reminders, Attachment Options
http://www.slovaktech.com/products.htm
Post by l***@skiz.net
Public WithEvents myEdItem As Outlook.MailItem
Private Sub Class_Initialize()
Set myItem = Application.ActiveInspector.CurrentItem
End Sub
Private Sub myEdItem_Close(Cancel As Boolean)
If Not myItem.Saved Then
MsgBox " The item was closed."
End If
End Sub
Dim myClass As New EdClass
Sub Register_Event_Handler()
Set myClass.myEdItem = "Outlook.mailitem"
End Sub
But I get an error that an Object is needed when trying to instantiate
the class (set myClass...).
Any thoughts?
Thanks
-Ed
l***@skiz.net
2008-02-07 02:10:06 UTC
Permalink
'Hope I'm not pushing here :). So I'm taking small steps. I tried to
write something to test to see if the current inspector was mail (as
you suggested). I believe if I can get this down, the rest will be
easy. I guess I'm not grok;ing the class thing. I took a C++ class a
while back and was good at it. In this case, I'm not sure if the class
gets instantiated with the "Set" command... and if so, what is its
"handle" ?

Class:EdInspectorEventClass
Public WithEvents myOlInspectors As Outlook.Inspectors
Public Sub Initialize_handler()
Set myOlInspectors = Application.Inspectors
End Sub
Private Sub myOlInspectors_NewInspector(ByVal Inspector As
Outlook.Inspector)
If (Inspector.CurrentItem.Class = olMail) Then
MsgBox ("You're in mail.")
End If
End Sub

In Module2:
Sub DoSomething()
Set EdInspectorEventClass = Outlook.Inspectors
EdInspectorEventClass.Initialize_handler ' *****This is where
it fails and says object does not support this property.*****
End Sub
l***@skiz.net
2008-02-07 07:10:02 UTC
Permalink
Sorry.... I know that was wrong... but I'm stumped.

In Class called "EdClass"
Public WithEvents myOlInspectors As Outlook.Inspectors
Public Sub Initialize_handler()
Set myOlInspectors = Application.Inspectors
End Sub
Private Sub myOlInspectors_NewInspector(ByVal Inspector As
Outlook.Inspector)
If (Inspector.CurrentItem.Class = olMail) Then
MsgBox ("You're in mail.")
End If
End Sub


In Module2
Dim myClass As New EdClass
Sub Register_Event2_Handler()
Set myClass.myOlInspectors = Application.Inspectors 'gives me an
error.
End Sub

From the immediate window, I tried calling: myClass.Initialize_handler
and that gave me the same error.


HELP :)


Thanks
-Ed
Ken Slovak - [MVP - Outlook]
2008-02-07 14:38:03 UTC
Permalink
Class:EdInspectorEventClass
Public WithEvents myOlInspectors As Outlook.Inspectors
Public WithEvents myMail As Outlook.MailItem

Public Sub Initialize_handler()
Set myOlInspectors = Application.Inspectors
End Sub

Private Sub myOlInspectors_NewInspector(ByVal Inspector As
Outlook.Inspector)
If (Inspector.CurrentItem.Class = olMail) Then
MsgBox ("You're in mail.")
Set myMail = Inspector.CurrentItem
End If
End Sub

Private Sub myMail_Close(Cancel As Boolean)
' do whatever you want
End Sub

In Module2:

Public myClass As EdInspectorEventClass
Sub DoSomething()
Set EdInspectorEventClass = New EdInspectorEventClass
EdInspectorEventClass.Initialize_handler
End Sub

To start things off the DoSomething procedure must be called to instantiate
an instance of the EdInspectorEventClass class and to call
EdInspectorEventClass.Initialize_handler. From there the code in
EdInspectorEventClass will run and handle any NewInspector event.

Of course you will also need to release your class at some point (set it to
Nothing). And the code as shown will handle one open Inspector at a time,
not multiple open Inspectors. For that you'd need to use a more complex
approach of wrapper classes stored in a collection.

Personally, if I was writing this and I wanted the code to start
automatically I'd put the Module2 code in the ThisOutlookSession class and
call DoSomething from the Application_Startup() event handler. I'd also
probably restructure it like this:

' move all this into ThisOutlookSession:
Public WithEvents myOlInspectors As Outlook.Inspectors
Public myInspectorsCollection As New Collection

Public Sub Initialize_handler()
Set myOlInspectors = Application.Inspectors
End Sub

Private Sub myOlInspectors_NewInspector(ByVal Inspector As
Outlook.Inspector)
If (Inspector.CurrentItem.Class = olMail) Then
MsgBox ("You're in mail.")
Dim oInspClass As New EdInspectorEventClass
Set oInspClass.myMail = Inspector.CurrentItem
myInspectorsCollection.Add oInspClass
End If
End Sub

Class:EdInspectorEventClass
Public WithEvents myMail As Outlook.MailItem

Private Sub myMail_Close(Cancel As Boolean)
' do whatever you want
End Sub

That will let you handle item.Close() on multiple open Inspectors and the
collection will keep each Inspector and Mail item alive.
--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007
Reminder Manager, Extended Reminders, Attachment Options
http://www.slovaktech.com/products.htm
Post by l***@skiz.net
'Hope I'm not pushing here :). So I'm taking small steps. I tried to
write something to test to see if the current inspector was mail (as
you suggested). I believe if I can get this down, the rest will be
easy. I guess I'm not grok;ing the class thing. I took a C++ class a
while back and was good at it. In this case, I'm not sure if the class
gets instantiated with the "Set" command... and if so, what is its
"handle" ?
Class:EdInspectorEventClass
Public WithEvents myOlInspectors As Outlook.Inspectors
Public Sub Initialize_handler()
Set myOlInspectors = Application.Inspectors
End Sub
Private Sub myOlInspectors_NewInspector(ByVal Inspector As
Outlook.Inspector)
If (Inspector.CurrentItem.Class = olMail) Then
MsgBox ("You're in mail.")
End If
End Sub
Sub DoSomething()
Set EdInspectorEventClass = Outlook.Inspectors
EdInspectorEventClass.Initialize_handler ' *****This is where
it fails and says object does not support this property.*****
End Sub
l***@skiz.net
2008-02-07 16:59:44 UTC
Permalink
Thanks Ken. I modified your module2 code to read: myclass = new ...
nad it workked. I now understand it. Thanks.

Inspectors see to be seperate windows. Do you know what the built-in
inspector is called? I usually don't double-click on my emails to
bringup a seperate window for each but rather just view them in the
big outlook-inspector.

Thanks again for the clear explination. It really helped !!

-Ed
Ken Slovak - [MVP - Outlook]
2008-02-07 17:59:24 UTC
Permalink
Each open item is an Inspector. A folder view is an Explorer. You can have
multiple Inspectors and Explorers open at the same time. The active members
are ActiveInspector and ActiveExplorer.

Never assume there will always be at least one Explorer or Inspector. There
are cases where you can have an Inspector with no Explorers and certainly no
Inspectors with at least one Explorer open.
--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007
Reminder Manager, Extended Reminders, Attachment Options
http://www.slovaktech.com/products.htm
Post by l***@skiz.net
Thanks Ken. I modified your module2 code to read: myclass = new ...
nad it workked. I now understand it. Thanks.
Inspectors see to be seperate windows. Do you know what the built-in
inspector is called? I usually don't double-click on my emails to
bringup a seperate window for each but rather just view them in the
big outlook-inspector.
Thanks again for the clear explination. It really helped !!
-Ed
s***@gmail.com
2008-03-13 06:12:10 UTC
Permalink
On Feb 7, 10:59 pm, "Ken Slovak - [MVP - Outlook]"
Post by Ken Slovak - [MVP - Outlook]
Each open item is an Inspector. A folder view is an Explorer. You can have
multiple Inspectors and Explorers open at the same time. The active members
are ActiveInspector and ActiveExplorer.
Never assume there will always be at least one Explorer or Inspector. There
are cases where you can have an Inspector with no Explorers and certainly no
Inspectors with at least one Explorer open.
--
Ken Slovak
[MVP - Outlook]http://www.slovaktech.com
Author: Professional Programming Outlook 2007
Reminder Manager, Extended Reminders, Attachment Optionshttp://www.slovaktech.com/products.htm
Thanks Ken.   I modified your module2 code to read:  myclass = new ...
nad it workked.  I now understand it.  Thanks.
Inspectors see to be seperate windows.  Do you know what the built-in
inspector is called?   I usually don't double-click on my emails to
bringup a seperate window for each but rather just view them in the
big outlook-inspector.
Thanks again for the clear explination.  It really helped !!
-Ed- Hide quoted text -
- Show quoted text -
can anyone please send me the whole code ...i would like to trry and
implement for my wrk ...
Loading...