Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Outlook / Programming VBA / May 2008

Tip: Looking for answers? Try searching our database.

Auto-reply

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Office_Novice - 05 May 2008 14:17 GMT
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.
Office_Novice - 05 May 2008 18:11 GMT
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 "MyEamil@mydomain.com"
           .Body = " This was sent to non - monitored email. Please reveiw"
           .Send
         End With
       End With
     End If
   Next i
End Sub

> 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
[quoted text clipped - 4 lines]
> in a link would be ideal. Any ideas on how to get started would be greatly
> appreciated.
Sue Mosher [MVP-Outlook] - 05 May 2008 22:57 GMT
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 "MyEamil@mydomain.com"
       .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

Signature

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

> Well this is what i have come up with so far.
> How do i get this to trigger when an email hits the inbox?
[quoted text clipped - 38 lines]
>> in a link would be ideal. Any ideas on how to get started would be greatly
>> appreciated.
Office_Novice - 06 May 2008 15:44 GMT
ThanksSue but i think i am  in over my head, I can't seem to get this quite
right. But thanks anyway.

> 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:
>
[quoted text clipped - 75 lines]
> >> in a link would be ideal. Any ideas on how to get started would be greatly
> >> appreciated.
Sue Mosher [MVP-Outlook] - 06 May 2008 16:17 GMT
If you give us a clue as to where you're stumped, maybe we can help?

Signature

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

> ThanksSue but i think i am  in over my head, I can't seem to get this quite
> right. But thanks anyway.
[quoted text clipped - 35 lines]
>>     Set mail = Nothing
>> End Sub


>> > Well this is what i have come up with so far.
>> > How do i get this to trigger when an email hits the inbox?
[quoted text clipped - 38 lines]
>> >> in a link would be ideal. Any ideas on how to get started would be greatly
>> >> appreciated.
Office_Novice - 06 May 2008 20:08 GMT
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.

> If you give us a clue as to where you're stumped, maybe we can help?
>
[quoted text clipped - 81 lines]
> >> >> in a link would be ideal. Any ideas on how to get started would be greatly
> >> >> appreciated.
Sue Mosher [MVP-Outlook] - 06 May 2008 20:58 GMT
What error? Remember, we can't look over your shoulder. You have to give us somethingto go on.

Signature

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

> 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
[quoted text clipped - 91 lines]
>> >> >> in a link would be ideal. Any ideas on how to get started would be greatly
>> >> >> appreciated.
Office_Novice - 07 May 2008 14:38 GMT
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 "MyEamil@mydomain.com"
       .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

> What error? Remember, we can't look over your shoulder. You have to give us somethingto go on.
>
[quoted text clipped - 93 lines]
> >> >> >> in a link would be ideal. Any ideas on how to get started would be greatly
> >> >> >> appreciated.
Sue Mosher [MVP-Outlook] - 07 May 2008 15:21 GMT
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.
Signature

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

> Here is what i have...
>
[quoted text clipped - 147 lines]
>> >> >> >> in a link would be ideal. Any ideas on how to get started would be greatly
>> >> >> >> appreciated.
Office_Novice - 07 May 2008 16:46 GMT
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 "EmailAdd@Domainname.com"
       .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?

> 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.
> > Here is what i have...
[quoted text clipped - 148 lines]
> >> >> >> >> in a link would be ideal. Any ideas on how to get started would be greatly
> >> >> >> >> appreciated.
Sue Mosher [MVP-Outlook] - 07 May 2008 17:14 GMT
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.

Signature

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?
>
[quoted text clipped - 213 lines]
>> >> >> >> >> in a link would be ideal. Any ideas on how to get started would be greatly
>> >> >> >> >> appreciated.
Office_Novice - 07 May 2008 17:45 GMT
Ambiguous name detected: olInboxItems_ItemAdd

Private Sub olInboxItems_ItemAdd(ByVal Item As Object)

> 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.):
>
[quoted text clipped - 223 lines]
> >> >> >> >> >> in a link would be ideal. Any ideas on how to get started would be greatly
> >> >> >> >> >> appreciated.
Sue Mosher [MVP-Outlook] - 07 May 2008 17:53 GMT
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.

Signature

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
>
[quoted text clipped - 227 lines]
>> >> >> >> >> >> in a link would be ideal. Any ideas on how to get started would be greatly
>> >> >> >> >> >> appreciated.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.