Consulting

Results 1 to 3 of 3

Thread: Write to log on itemsend and BCC email to another mailbox

  1. #1
    VBAX Newbie
    Joined
    Nov 2017
    Posts
    1
    Location

    Question Write to log on itemsend and BCC email to another mailbox

    Hello everyone i'm really new here in this forum and i hope i can share knowledge

    I'm having problems with a script that write to a file when a user SEND an email, because sometimes it recognizes sender email and sometimes not. This is the code.

    Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
    
    Dim objRecip As Recipient
    Dim strMsg As String
    Dim strBcc As String
    Dim sHostName As String
    Dim sUserName As String
    Dim filename As String
    Dim wn As Integer
    Dim FSO As FileSystemObject
    Set FSO = New FileSystemObject
    Dim ts As TextStream
    On Error GoTo GetOut
    
    
    wn = Format(Date, "ww")
    cyear = Format(Date, "yyyy")
    sHostName = Environ$("computername")
    sUserName = Environ$("username")
    filename = wn & "-" & cyear & "-" & sHostName & ".txt"
    
    
    SavePath = "routetofile" & filename
    
    
    
    
    SenderName = Item.SentOnBehalfOfName
    senderit = Item.sender
    
    'Modified strBcc value i'm new and the post don't allow me to post addresses
    strBcc = "emailaddrtoBCC"
    Set objRecip = Item.Recipients.Add(strBcc)
        objRecip.Type = olBCC
           If Not objRecip.Resolve Then
            If FSO.FileExists(SavePath) = False Then
                Set ts = FSO.CreateTextFile(SavePath, True)
                ts.Close
                Set ts = FSO.OpenTextFile(SavePath, IOMode:=ForAppending)
                ts.WriteLine (Now() & ";" & SenderName & ";" & senderit & ";" & GetSmtpAddress(Item) & ";" & sUserName & ";" & sHostName & "; ERROR")
                ts.Close
            Else
                Set ts = FSO.OpenTextFile(SavePath, IOMode:=ForAppending)
                ts.WriteLine (Now() & ";" & SenderName & ";" & senderit & ";" & GetSmtpAddress(Item) & ";" & sUserName & ";" & sHostName & "; ERROR")
                ts.Close
             End If
         End If
    Set objRecip = Nothing
    
    
    
    
    
    
        If FSO.FileExists(SavePath) = False Then
            Set ts = FSO.CreateTextFile(SavePath, True)
            ts.Close
            Set ts = FSO.OpenTextFile(SavePath, IOMode:=ForAppending)
            ts.WriteLine ("Date" & ";" & "Sent on Behalf" & ";" & "Sender" & ";" & "Smtp Address" & ";" & "User Name" & ";" & "Host Name" & ";" & "Status")
            ts.WriteLine (Now() & ";" & SenderName & ";" & senderit & ";" & GetSmtpAddress(Item) & ";" & sUserName & ";" & sHostName & "; OK")
            ts.Close
        Else
            Set ts = FSO.OpenTextFile(SavePath, IOMode:=ForAppending)
            ts.WriteLine (Now() & ";" & SenderName & ";" & senderit & ";" & GetSmtpAddress(Item) & ";" & sUserName & ";" & sHostName & "; OK")
            ts.Close
        End If
    Set FSO = Nothing
       
    GetOut:
    End Sub
    
    
    
    
    
    
    Function GetSmtpAddress(mail As MailItem)
        On Error GoTo On_Error
        GetSmtpAddress = ""
        
        Dim Report As String
        Dim Session As Outlook.NameSpace
        Set Session = Application.Session
        
    
    
            Dim senderEntryID As String
            Dim sender As AddressEntry
            Dim PR_SENT_REPRESENTING_ENTRYID As String
            'Modified value URL TO SCHEMAS i'm new and forum don't allow full urls
            PR_SENT_REPRESENTING_ENTRYID = "URL TO SCHEMAS microsoft.com /mapi/proptag /0x00410102"
            
            senderEntryID = mail.PropertyAccessor.BinaryToString( _
                mail.PropertyAccessor.GetProperty( _
                    PR_SENT_REPRESENTING_ENTRYID))
            
            Set sender = Session.GetAddressEntryFromID(senderEntryID)
            If sender Is Nothing Then
                Exit Function
            End If
            
            If sender.AddressEntryUserType = olExchangeUserAddressEntry Or _
                sender.AddressEntryUserType = olExchangeRemoteUserAddressEntry Then
                    
                Dim exchangeUser As exchangeUser
                Set exchangeUser = sender.GetExchangeUser()
                
                If exchangeUser Is Nothing Then
                    Exit Function
                End If
                
                GetSmtpAddress = exchangeUser.PrimarySmtpAddress
                Exit Function
            Else
                Dim PR_SMTP_ADDRESS
                 'Modified value URL TO SCHEMAS i'm new and forum don't allow full urls
                PR_SMTP_ADDRESS = "URL TO SCHEMAS /mapi/ proptag/ 0x39FE001E"
                GetSmtpAddress = sender.PropertyAccessor.GetProperty(PR_SMTP_ADDRESS)
            End If
                
            
       
        
    Exiting:
            Exit Function
    On_Error:
        MsgBox "error=" & Err.Number & " " & Err.Description
        Resume Exiting
        
    End Function
    What do you think about this code? How can i optimize it, i need to deploy this configuration to 30 computers.

  2. #2
    VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Moderator bump
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    VBAX Mentor skatonni's Avatar
    Joined
    Jun 2006
    Posts
    347
    Location
    You could if you wish set the SentOnBehalfOfName otherwise sender information is not available until the item is sent.

    This may be sufficient for your purposes

    senderit = Session.GetDefaultFolder(olFolderInbox).Parent
    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.

Posting Permissions

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