Consulting

Results 1 to 9 of 9

Thread: Fwd Mail sent to outlook group

  1. #1
    VBAX Regular
    Joined
    Jul 2015
    Location
    london
    Posts
    6
    Location

    Exclamation Fwd Mail sent to outlook group

    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

  2. #2
    VBAX Mentor skatonni's Avatar
    Joined
    Jun 2006
    Posts
    347
    Location
    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
    To debug, mouse-click anywhere in the code. Press F8 repeatedly to step through the code. http://www.cpearson.com/excel/DebuggingVBA.aspx

    If your problem has been solved in your thread, mark the thread "Solved" by going to the "Thread Tools" dropdown at the top of the thread. You might also consider rating the thread by going to the "Rate Thread" dropdown.

  3. #3
    VBAX Regular
    Joined
    Jul 2015
    Location
    london
    Posts
    6
    Location
    thank you, will try that ASAP

  4. #4
    VBAX Regular
    Joined
    Jul 2015
    Location
    london
    Posts
    6
    Location
    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

  5. #5
    VBAX Mentor skatonni's Avatar
    Joined
    Jun 2006
    Posts
    347
    Location
    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/lib.../ff869330.aspx

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

    https://msdn.microsoft.com/en-us/lib.../ff863406.aspx
    To debug, mouse-click anywhere in the code. Press F8 repeatedly to step through the code. http://www.cpearson.com/excel/DebuggingVBA.aspx

    If your problem has been solved in your thread, mark the thread "Solved" by going to the "Thread Tools" dropdown at the top of the thread. You might also consider rating the thread by going to the "Rate Thread" dropdown.

  6. #6
    VBAX Regular
    Joined
    Jul 2015
    Location
    london
    Posts
    6
    Location
    Quote Originally Posted by skatonni View Post
    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

  7. #7
    VBAX Mentor skatonni's Avatar
    Joined
    Jun 2006
    Posts
    347
    Location
    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
    To debug, mouse-click anywhere in the code. Press F8 repeatedly to step through the code. http://www.cpearson.com/excel/DebuggingVBA.aspx

    If your problem has been solved in your thread, mark the thread "Solved" by going to the "Thread Tools" dropdown at the top of the thread. You might also consider rating the thread by going to the "Rate Thread" dropdown.

  8. #8
    VBAX Regular
    Joined
    Jul 2015
    Location
    london
    Posts
    6
    Location
    thank you for the pointers, thats logical and very helpful

  9. #9
    VBAX Regular
    Joined
    Jul 2015
    Location
    london
    Posts
    6
    Location
    Quote Originally Posted by skatonni View Post
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •