Auto-reply

  • Thread starter Thread starter Office_Novice
  • Start date Start date
O

Office_Novice

Greetings, I have some experience programming in excel and word, but none in
outlook. I have been charged with finding a solution to an outlook problem. I
have a mail box that is no longer monitored and would like to do two things,
if possible. If an email hits the inbox of the "oldmail" box I would like to
forward that email to the "newmail" box and notify the sender of the old mail
box's demise and to use the new mail box. Giving the sender the new address
in a link would be ideal. Any ideas on how to get started would be greatly
appreciated.
 
Well this is what i have come up with so far.
How do i get this to trigger when an email hits the inbox?
Sub AutoReply()
Dim myOlApp As Outlook.Application
Dim myNmspc As Outlook.NameSpace
Dim myFldr As Object
Dim myItem As Object
Dim myReply As Object
Dim myFwd As Object
Dim i As Long

For i = 1 To 100
Set myOlApp = CreateObject("Outlook.Application")
Set myNmspc = myOlApp.GetNamespace("MAPI")
Set myFldr = myNmspc.GetDefaultFolder(olFolderInbox)
Set myItem = myFldr.Items(i)
Set myReply = myItem.Reply
Set myFwd = myItem.Forward

If i > 0 Then
With myReply
.Body = " This email address is no longer monitored. Please use
another."
.Send
With myFwd
.Recipients.Add "(e-mail address removed)"
.Body = " This was sent to non - monitored email. Please reveiw"
.Send
End With
End With
End If
Next i
End Sub
 
First, change the code so that it takes an Outlook.MailItem as an argument and processes it, rather than processing 100 items in a folder:

Sub AutoReply(myItem as Outlook.MailItem)
Dim myReply As Outlook.MailItem
Dim myFwd As Outlook.MailItem
Set myReply = myItem.Reply
Set myFwd = myItem.Forward
With myReply
.Body = " This email address is no longer monitored. Please use another."
.Send
End With
With myFwd
.Recipients.Add "(e-mail address removed)"
.Body = " This was sent to non - monitored email. Please reveiw"
.Send
End With
Set myReply = Nothing
Set myFwd = Nothing
End Sub

Second, adapt the Items.ItemAdd event handler shown at http://www.outlookcode.com/article.aspx?id=62 so that instead of Namespace.GetDefaultFolder, it uses Namespace.GetSharedDefaultFolder to return the other mailbox's Inbox

Dim objRecip as Outlook.Recipient
Set objRecip = Application.CreateRecipient("old mailbox alias")
Set olInboxItems = objNS.GetSharedDefaultFolder(objRecip, olFolderInbox).Items

Finally, in an ItemAdd event handler, call your AutoReply procedure:

Private Sub olInboxItems_ItemAdd(ByVal Item As Object)
Dim mail as Outlook.MailItem
If Item.Class = olMail Then
Set mail = Item
Call AutoReply(mail)
End If
Set mail = Nothing
End Sub
 
ThanksSue but i think i am in over my head, I can't seem to get this quite
right. But thanks anyway.
 
Well, The auto reply macro i understand. Thats pretty silmilar to things i
have seen in the past. The Code on your website worked ok too, this
Dim objRecip as Outlook.Recipient
Set objRecip = Application.CreateRecipient("old mailbox alias")
Set olInboxItems = objNS.GetSharedDefaultFolder(objRecip,
olFolderInbox).Items
Kept giving me an object error. on the Application.CreateRecip... line. also
I could only step through the code from your web site any idea why i could'nt
debug the whole thing? I really appreciate the help.
 
Here is what i have...

Option Explicit
Private WithEvents olInboxItems As Items

Private Sub Application_Startup()
Dim objNS As NameSpace
Dim objRecip As Outlook.Recipient
Set objNS = Application.Session
' instantiate objects declared WithEvents
Set objRecip = Application.CreateRecipient("oldmailbx") '<-- Runtime
err 438 object doesn't support property or method
Set olInboxItems = objNS.GetSharedDefaultFolder(objRecip,
olFolderInbox).Items
Set objNS = Nothing
End Sub

Private Sub olInboxItems_ItemAdd(ByVal Item As Object)
On Error Resume Next
Item.BodyFormat = olFormatPlain
Item.Save
Set Item = Nothing

End Sub


Sub AutoReply(myItem As Outlook.MailItem)
Dim myReply As Outlook.MailItem
Dim myFwd As Outlook.MailItem
Set myReply = myItem.Reply
Set myFwd = myItem.Forward
With myReply
.Body = " This email address is no longer monitored. Please use
another."
.Send
End With
With myFwd
.Recipients.Add "(e-mail address removed)"
.Body = " This was sent to non - monitored email. Please reveiw"
.Send
End With
Set myReply = Nothing
Set myFwd = Nothing
End Sub

Private Sub olkInboxItems_ItemAdd(ByVal Item As Object)

Dim mail As Outlook.MailItem
If Item.Class = olMail Then
Set mail = Item
Call AutoReply(mail)
End If
Set mail = Nothing
End Sub
 
My bad, sorry. CreateRecipient is a method of the Namespace object, not Application. (Remember: The object browser -- F2 -- is your friend.) So, use Application.Session.CreateRecipient.
--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54
 
I have all of this in the ThisOutlookSession Module. Is this correct?


Option Explicit
Private WithEvents olkInboxItems As Items
Private Sub Application_Startup()
Dim objNS As NameSpace
Dim objRecip As Outlook.Recipient
Set objNS = Application.Session
' instantiate objects declared WithEvents
Set objRecip = Application.Session.CreateRecipient("oldmailbx")
Set olkInboxItems = objNS.GetSharedDefaultFolder(objRecip,
olFolderInbox).Items
Set objNS = Nothing

End Sub
Private Sub olInboxItems_ItemAdd(ByVal Item As Object)
On Error Resume Next
Item.BodyFormat = olFormatPlain
Item.Save
Set Item = Nothing
End Sub
Sub AutoReply(myItem As Outlook.MailItem)
Dim myReply As Outlook.MailItem
Dim myFwd As Outlook.MailItem
Set myReply = myItem.Reply
Set myFwd = myItem.Forward
With myReply
.Body = " This email address is no longer monitored. Please use
another."
.Send
End With
With myFwd
.Recipients.Add "(e-mail address removed)"
.Body = " This was sent to non - monitored email. Please reveiw"
.Send
End With
Set myReply = Nothing
Set myFwd = Nothing
End Sub
Private Sub olkInboxItems_ItemAdd(ByVal Item As Object)
Dim mail As Outlook.MailItem
If Item.Class = olMail Then
Set mail = Item
Call AutoReply(mail)
End If
Set mail = Nothing
End Sub

I can only step through this portion of the code

Private Sub Application_Startup()
Dim objNS As NameSpace
Dim objRecip As Outlook.Recipient
Set objNS = Application.Session
' instantiate objects declared WithEvents
Set objRecip = Application.Session.CreateRecipient("oldmailbx")
Set olkInboxItems = objNS.GetSharedDefaultFolder(objRecip,
olFolderInbox).Items
Set objNS = Nothing

End Sub

Nothing else seems to firing. Any ideas?
 
Compare these code statements (look at my original if you need to), and you should see the mistake. (HINT: It's in the event handler procedure name.):

Private WithEvents olkInboxItems As Items

Set olkInboxItems = objNS.GetSharedDefaultFolder(objRecip, olFolderInbox).Items

Private Sub olInboxItems_ItemAdd(ByVal Item As Object)

Once you fix the name of the event handler, to step through its code, you'll need to add a breakpoint by highlighting a statement and pressing F9. Then when the event fires, execution will stop at that breakpoint.

--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54
 
Ambiguous name detected: olInboxItems_ItemAdd

Private Sub olInboxItems_ItemAdd(ByVal Item As Object)
 
You're on the right track. You changed the name of the Items variable, but did not change the name of its associated event handler procedure to match.

--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54
 
Back
Top