adjusting macro to process all new incoming

  • Thread starter Thread starter bob c.
  • Start date Start date
B

bob c.

I have the following code that works great when I manually start the macro.

What do I have to adjust so I can move it to the "ThisOutlookSession" and
make it work for all new emails received into my inbox ?

Sub ForwardSelectedMessages()
On Error Resume Next

Dim objItem As Object
Dim objForward As Outlook.MailItem

For Each objItem In ActiveExplorer.Selection
If objItem.Class = olMail Then
Set objForward = objItem.Forward
objForward.To = "(e-mail address removed)"
objForward.BCC = "(e-mail address removed)"
objForward.Send
Set objItem = Nothing
Set objForward = Nothing
End If
Next

Set objItem = Nothing
Set objForward = Nothing
End Sub
 
Hi Bob,

you could use the NewMail event. A sample is available in the VBA help.

This event only fires if at least one new item is coming in, it doesn´t
fire for each item. Anyway, this doesn´t matter if you want to loop
through all Inbox items. But your code forwards all items again and
again. Do you know that?

I´d suggest the use of the ItemAdd event. It fires if an item is added
to a folder, e.g. a new mail in your Inbox. You could handle each item
only once and without the expensive (performance) loop. (A sample is
also given in the VBA help.)
 
I've made several adjustments without success and this is my latest:


Dim myOlApp As New Outlook.Application
Public WithEvents myOlItems As Outlook.Items

Public Sub Initialize_handler()
Set myOlItems =
myOlApp.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Items
End Sub


Private Sub myOlItems_ItemAdd(ByVal Item As Object)
Dim myOlMItem As Outlook.MailItem
Set myOlMItem = mail_item.Forward
myOlMItem.To = "(e-mail address removed)"
myOlMItem.BCC = "(e-mail address removed)"
myOlMItem.Display
'uncomment to send automatically (comment out objForward.Display
too)
'objForward.Send
Set objItem = Nothing
Set objForward = Nothing
End Sub
 
Hi Bob,
Public Sub Initialize_handler()

in this point the VBA help should be clearer. OL´s initialize handler is
calling Application_Startup(). You could write your initialize code into
this function or call it from within there.
Set myOlMItem = mail_item.Forward

In your sample this should be:

Set myOlMItem = Item.Forward
 
The: Set myOlMItem = Item.Forward worked great, thanks for all your
help.

Not quite sure what you meant for the: Public Sub Initialize_handler()
could you elaborate ?

How do I tweak the macro to work for other folders in my "Favorite Folders"
?
 
Hi Bob,

please replace:

Public Sub Initialize_handler()
Set myOlItems =
myOlApp.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Items
End Sub

.... with:

Public Sub Application_Startup()
Set myOlItems =
myOlApp.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Items
End Sub
How do I tweak the macro to work for other folders in my "Favorite
Folders"

In GetDefaultFolder´s FolderType argument you can specify a standard
folder.

Set myOlItems =
myOlApp.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Items

Please set the cursor into "olFolderInbox" and select "Showing
Constants" from the context menu. You will get a list of available
contants.

For any other folder you can use Sue´s function for getting a reference
by its path:

http://www.outlookcode.com/d/code/getfolder.htm

or walk through each folder´s Folders collection.
 
Back
Top