Outlook Script

Associate
Joined
24 Feb 2009
Posts
1,720
Location
Teesside
Hi,

I am currently trying to set up a script for outlook but my limited skills are leaving me stumped! I imagine its straight forward if you have any half decent skills though, so thought I would ask here!

Basically, at work we have a helpdesk which will kick out email updates to us, but only sends to internal emails. External ones just come to us which we then pass on to an email address located in the subject line (the subject line is just the email address, no other text).

I have found this guide,which is similar to what I want but I just can't get it working. Just need to take the text from the subject and forward the email to it.

At the moment I have:

Code:
Sub HelpdeskRedirect(Item As Outlook.MailItem)
 Item.To = Item.Subject
 Item.Save
 Item.Subject = "Helpdesk Update"
 Item.Send
 
End Sub

This tried to send a couple of times and I got undeliverable reports due to permissions. I've changed those but I get nothing being sent (nothing in sent folder which it did earlier). Changed things back to how they were originally and I still get nothing in the sent folder.

Thanks for any help :D
 
Soldato
Joined
25 Oct 2002
Posts
2,617
The issue here is that you're trying to treat a received message in your inbox as if it were an unsent item which you are creating - you really need to create a new item from this email (ie. forward it) to make sure it's in a state ready for you to send on.

The following will forward the original item, it then changes the recipient to what was in the subject and it restores the original items body (so that you don't include all the usual automatic forwarding stuff that you get in Outlook) and then sends it - obviously if you want to keep this then just remove those lines. This does assume that the subject will always contain a valid e-mail address...!

Code:
Sub onReceipt(item As MailItem)

    Dim newItem As MailItem
    
    Set newItem = item.Forward
    
    With newItem
        ' Set the recipient and new subject
        .Recipients.Add item.Subject
        .Subject = "Helpdesk Update"
        
        ' Restore the original items body ie. get rid of your signature, and the usual forward details.
        .Body = item.Body
        .HTMLBody = item.HTMLBody
        .RTFBody = item.RTFBody
        
        ' Send the email.
        .send
    End With
    
    ' Optional - delete the original item
    'item.Delete
    
End Sub
 
Associate
OP
Joined
24 Feb 2009
Posts
1,720
Location
Teesside
Thanks for the reply and sorry for waiting so long before I tried it!

That's working perfectly and message send and come through without a problem. Thanks for your help :)
 
Back
Top Bottom