Log in

View Full Version : Fwd Mail sent to outlook group



mole999
07-22-2015, 11:59 PM
I know this must be reasonably simple, I'm new to outlook VBA and obviously not understanding the syntax, so I'm not find results elsewhere.

Outlook exchange is disabled for forwarding to an external address, the rule I had got switched off hence going the VBA route

I want to forward any message that is sent to ACCIDENT (either TO or COPY or BCC) and I can't find the words to write that

I have the below code which will forward everything and it works, just want to limit it a lot


Private Sub Application_NewMailEx(ByVal EntryIDCollection As String)
Dim varEntryIDs
Dim objItem
Dim i As Integer
Dim bSend As Boolean
Dim fwdItem As Outlook.MailItem
On Error Resume Next
bSend = False
If Hour(Now) > 15 Or Hour(Now) < 7 Then 'After hours
bSend = True
' ElseIf Hour(Now) = 12 Then 'Lunch
' bSend = True
' ElseIf checkBusy Then 'In meeting
' bSend = True
End If

bSend = True ' non rule test
If bSend Then
varEntryIDs = Split(EntryIDCollection, ",")
For i = 0 To UBound(varEntryIDs)
Set objItem = Application.Session.GetItemFromID(varEntryIDs(i))
Set fwdItem = objItem.Forward
fwdItem.Recipients.Add "[EMAIL
fwdItem.SentOnBehalfOfName = Item.SentOnBehalfOfName
fwdItem.Send
Next
End If
End Sub


please can someone point or correct for this

skatonni
07-23-2015, 12:32 PM
Something like this untested code


Private Sub Application_NewMailEx(ByVal EntryIDCollection As String)
Dim recip As recipient

Dim varEntryIDs
Dim objitem
Dim i As Integer
Dim bSend As Boolean
Dim fwdItem As Outlook.MailItem

'On Error Resume Next ' Should use only when there is a specific reason

bSend = False

If Hour(Now) > 15 Or Hour(Now) < 7 Then 'After hours
bSend = True
End If

If bSend Then

varEntryIDs = Split(EntryIDCollection, ",")

For i = 0 To UBound(varEntryIDs)

Set objitem = Application.Session.GetItemFromID(varEntryIDs(i))

For Each recip In objitem.Recipients

If recip.AddressEntry.DisplayType <> olUser Then

If LCase(recip) = "accident" Then

Set fwdItem = objitem.Forward
fwdItem.Recipients.Add "[EMAIL "
fwdItem.SentOnBehalfOfName = objitem.SentOnBehalfOfName
fwdItem.Display
'fwdItem.Send

Exit For

End If

End If

Next

Next
End If

End Sub

mole999
07-23-2015, 08:57 PM
thank you, will try that ASAP

mole999
07-23-2015, 10:34 PM
I took this out
If recip.AddressEntry.DisplayType <> olUser Then

can you tell me what it should do I understand the Then bit, couldn't work out what it checks as in testing it was stepping over the code from here

skatonni
07-24-2015, 04:44 AM
From the title of the post, I assumed ACCIDENT was a distribution list. If so that line would have sped up the processing.

https://msdn.microsoft.com/en-us/library/office/ff869330.aspx

"You can use the DisplayType property to filter address entries."

https://msdn.microsoft.com/en-us/library/office/ff863406.aspx

mole999
07-24-2015, 07:24 AM
From the title of the post, I assumed ACCIDENT was a distribution list. If so that line would have sped up the processing.


"You can use the DisplayType property to filter address entries."



the group would be "accident.help at email address or shown as accident help", but i'm a member of the group via exchange server and i'm just trying to ensure those very few messages can reach me shortly after they are sent (with the work machine logged on)
my limited testing on my own email kicked an error (though i'm not a group so I could be in error) 30 odd people in the group but might get a couple of email a month, so any check that finds it is not required and curtails the mail redirect early would be good. I hope to learn more about what can be done in outlook over time

skatonni
07-24-2015, 11:50 AM
Open up a relevant mail then find the name you are interested in with this.


Private Sub MailRecipients()
Dim currItem As MailItem
Dim recip As recipient
Dim recips As Recipients
Set currItem = ActiveInspector.currentItem
Set recips = currItem.Recipients
For Each recip In recips
Debug.Print recip.Name
Next recip
End Sub



Without this filter


If recip.AddressEntry.DisplayType <> olUser Then

This line will be called for each recipient


If LCase(recip) = LCase("name to look for here") Then

mole999
07-24-2015, 11:26 PM
thank you for the pointers, thats logical and very helpful

mole999
07-26-2015, 10:51 PM
Open up a relevant mail then find the name you are interested in with this.


Private Sub MailRecipients()
Dim currItem As MailItem
Dim recip As recipient
Dim recips As Recipients
Set currItem = ActiveInspector.currentItem
Set recips = currItem.Recipients
For Each recip In recips
Debug.Print recip.Name
Next recip
End Sub



Without this filter


If recip.AddressEntry.DisplayType <> olUser Then

This line will be called for each recipient


If LCase(recip) = LCase("name to look for here") Then

One additional thing if I may, the bit of code to expose the users Private Sub MailRecipients() the mail I checked has a olUser and a olDistList, will the code ignore one and check use the other?

I don't want to spam the group whilst testing, I've put it back as you first described