msoutlook.org Forum Index
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Help writing a macro to move emails

 
Post new topic   Reply to topic    msoutlook.org Forum Index -> MS Office Outlook
Author Message
landau



Joined: 06 Feb 2008
Posts: 12

PostPosted: Wed Feb 06, 2008 4:04 am    Post subject: Help writing a macro to move emails Reply with quote

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 Smile.

Is this possible?

Thanks
-Ed

Archived from group: microsoft>public>office>developer>outlook>vba
Back to top
View user's profile Send private message
Anand.V.V.N



Joined: 07 Nov 2007
Posts: 7

PostPosted: Wed Feb 06, 2008 10:39 am    Post subject: RE: Help writing a macro to move emails Reply with quote

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?"


"landau@skiz.net" wrote:

> 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 Smile.
>
> Is this possible?
>
> Thanks
> -Ed
>
Back to top
View user's profile Send private message
Ken Slovak - [MVP - Outlo



Joined: 12 Aug 2007
Posts: 405

PostPosted: Wed Feb 06, 2008 2:20 pm    Post subject: Re: Help writing a macro to move emails Reply with quote

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


wrote in message @s37g2000prg.googlegroups.com...
> 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 Smile.
>
> Is this possible?
>
> Thanks
> -Ed
Back to top
View user's profile Send private message
landau



Joined: 06 Feb 2008
Posts: 12

PostPosted: Wed Feb 06, 2008 5:40 pm    Post subject: Re: Help writing a macro to move emails Reply with quote

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
Back to top
View user's profile Send private message
Ken Slovak - [MVP - Outlo



Joined: 12 Aug 2007
Posts: 405

PostPosted: Wed Feb 06, 2008 9:10 pm    Post subject: Re: Help writing a macro to move emails Reply with quote

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


wrote in message @e4g2000hsg.googlegroups.com...
> 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
Back to top
View user's profile Send private message
landau



Joined: 06 Feb 2008
Posts: 12

PostPosted: Wed Feb 06, 2008 11:10 pm    Post subject: Re: Help writing a macro to move emails Reply with quote

'Hope I'm not pushing here Smile. 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
Back to top
View user's profile Send private message
landau



Joined: 06 Feb 2008
Posts: 12

PostPosted: Thu Feb 07, 2008 4:10 am    Post subject: Re: Help writing a macro to move emails Reply with quote

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 Smile


Thanks
-Ed
Back to top
View user's profile Send private message
Ken Slovak - [MVP - Outlo



Joined: 12 Aug 2007
Posts: 405

PostPosted: Thu Feb 07, 2008 2:38 pm    Post subject: Re: Help writing a macro to move emails Reply with quote

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


wrote in message @b2g2000hsg.googlegroups.com...
> 'Hope I'm not pushing here Smile. 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
>
>
Back to top
View user's profile Send private message
landau



Joined: 06 Feb 2008
Posts: 12

PostPosted: Thu Feb 07, 2008 1:59 pm    Post subject: Re: Help writing a macro to move emails Reply with quote

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
Back to top
View user's profile Send private message
Ken Slovak - [MVP - Outlo



Joined: 12 Aug 2007
Posts: 405

PostPosted: Thu Feb 07, 2008 5:59 pm    Post subject: Re: Help writing a macro to move emails Reply with quote

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


wrote in message @j78g2000hsd.googlegroups.com...
> 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

Back to top
View user's profile Send private message
Display posts from previous:   
Related Topics:
Move Spam macro... Hi, If anyone can help me out with this VB code in Outlook I would greatly help us out. This macro simply allows you to select multiple emails and add them to your Junk Senders.txt list then it automatically deletes the messages. What we are in search o

Finding/Writing Outlook plugin to launch URL with phone numb Hi, I am looking for information on how to write a plugin for Microsoft Outlook (or Outlook 2002). BRIEF DESCRIPTION: Something that automatically launches a web browser with a phone number embedded at the end of the URL (a special purpose website that di

The macro cannot be found Hello, Help is needed for the following problem: I wrote some VBA code in Outlook 2003. when I tried to run it from within Outlook by clicking a customized macro button on the toolbar, I received an error message that said "The xxx macro cannot be found".

Run a Rule from Macro Is it possible to fire a rule from a macro? Outlook 2003. Bye

Macro Execution Pardon my second post, but my first received no response. I am a newbie to vba, but managed to create a macro that works when run manually, I would like the macro to run automatically on the send_item event, or some other event, but I am having trouble
Post new topic   Reply to topic    msoutlook.org Forum Index -> MS Office Outlook All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group