+ Reply to Thread
Results 1 to 13 of 13

Several persons initials in one cell, sent to their respective e-mail addresses.

Hybrid View

  1. #1
    Registered User
    Join Date
    09-13-2012
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    11

    Several persons initials in one cell, sent to their respective e-mail addresses.

    I have been using the code below to send e-mails based on dates (props to paul) when the workbook is opened. What I can't figure out to do it to enter several initials into cell i,12 and have their respective e-mail addresses added to the "Strto" Value.

    Example: J.D and D.J are held within an i,12 cell. I would like these to correspond to John.doe@gmail.com and doe.john@gmail.com.

    Private Sub Workbook_Open()
    Dim i As Long
    Dim OutApp As Object
    Dim OutMail As Object
    Dim strto As String, strcc As String, strbcc As String
    Dim strsub As String, strbody As String
        
        For i = 6 To Sheets("FMEA").Range("B65536").End(xlUp).Row
            If Sheets("FMEA").Cells(i, 13).Value = (Date) Then
                Set OutApp = CreateObject("Outlook.Application")
                OutApp.Session.Logon
                Set OutMail = OutApp.CreateItem(0)
            
                strto = Sheets("FMEA").Cells(i, 12).Value
                'strcc = Sheets("FMEA").Cells(i, 13).Value & _
                            "; " & Sheets("FMEA").Cells(i, 5).Value
                strbcc = ""
                strsub = "Contract Expiry Notice"
                strbody = "Hi there" & vbNewLine & vbNewLine & _
                    "Your contract, " & Sheets("FMEA").Cells(i, 11).Value & _
                    ", is due to expire on " & Sheets("FMEA").Cells(i, 13).Value & _
                    ". Please contact us at your earliest convenience." & _
                    vbCrLf & vbCrLf & "Thank you."
        
                With OutMail
                    .To = strto
                    .CC = strcc
                    .BCC = strbcc
                    .Subject = strsub
                    .Body = strbody
                    .Send
                End With
        
                Set OutMail = Nothing
                Set OutApp = Nothing
            End If
        Next i
    End Sub
    Thanks a bunch,

    Lacessit
    Last edited by JosephP; 09-26-2012 at 07:10 AM. Reason: corrected code tags

  2. #2
    Forum Contributor wallyeye's Avatar
    Join Date
    05-06-2011
    Location
    Arizona
    MS-Off Ver
    Office 2010, 2007
    Posts
    308

    Re: Several persons initials in one cell, sent to their respective e-mail addresses.

    You can put the names into an array, then loop through that:

    Private Sub Workbook_Open()
    Dim i As Long
    Dim OutApp As Object
    Dim OutMail As Object
    Dim strto As String, strcc As String, strbcc As String
    Dim strsub As String, strbody As String
    dim arrInits       as variant
    dim intAddress   as integer
        
        For i = 6 To Sheets("FMEA").Range("B65536").End(xlUp).Row
            If Sheets("FMEA").Cells(i, 13).Value = (Date) Then
                Set OutApp = CreateObject("Outlook.Application")
                OutApp.Session.Logon
                Set OutMail = OutApp.CreateItem(0)
    
                if instr(1,Sheets("FMEA").Cells(i, 12).Value,",") > 0 then
                     arrinits = split(Sheets("FMEA").Cells(i, 12).Value, ",")
                else
                     redim arrinits(1 to 1)
                     arrinits = Sheets("FMEA").Cells(i, 12).Value
                endif
    
                for intaddress = lbound(arrinits) to ubound(arrinits)
                     on error resume next
                     strto = application.worksheetfunction.vlookup(arrinits(intaddress), _
                        thisworkbook.names("NameMap").referstorange,2,false)
                     if err <> 0 then
                        strto = ""
                     endif
                     on error goto 0
    
                     if strto > "" _ 
                     and strto <> "#N/A" then
                'strto = Sheets("FMEA").Cells(i, 12).Value
                'strcc = Sheets("FMEA").Cells(i, 13).Value & _
                            "; " & Sheets("FMEA").Cells(i, 5).Value
                strbcc = ""
                strsub = "Contract Expiry Notice"
                strbody = "Hi there" & vbNewLine & vbNewLine & _
                    "Your contract, " & Sheets("FMEA").Cells(i, 11).Value & _
                    ", is due to expire on " & Sheets("FMEA").Cells(i, 13).Value & _
                    ". Please contact us at your earliest convenience." & _
                    vbCrLf & vbCrLf & "Thank you."
        
                With OutMail
                    .To = strto
                    .CC = strcc
                    .BCC = strbcc
                    .Subject = strsub
                    .Body = strbody
                    .Send
                End With
                      endif
                next intaddress
        
                Set OutMail = Nothing
                Set OutApp = Nothing
            End If
        Next i
    End Sub
    You will need to define a Named Range "NameMap" that contains a list of your initials and e-mail addresses.

  3. #3
    Registered User
    Join Date
    09-13-2012
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Several persons initials in one cell, sent to their respective e-mail addresses.

    Hi Walleye,

    Thanks for helping It won't let me upload a copy of my sheet here at work but I can do it while at home. It seems the Named range I'm creating isn't working? Gives me a 'Type mismatch' error.

  4. #4
    Registered User
    Join Date
    09-13-2012
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Several persons initials in one cell, sent to their respective e-mail addresses.

    Hi Walleye,

    Thanks for helping It won't let me upload a copy of my sheet here at work but I can do it while at home. It seems the Named range I'm creating isn't working? Gives me a 'Type mismatch' error.
    Managed to upload it using my phone
    Attached Files Attached Files
    Last edited by Lacessit; 09-14-2012 at 03:56 AM.

  5. #5
    Forum Contributor wallyeye's Avatar
    Join Date
    05-06-2011
    Location
    Arizona
    MS-Off Ver
    Office 2010, 2007
    Posts
    308

    Re: Several persons initials in one cell, sent to their respective e-mail addresses.

    I would recommend putting NameMap on a separate worksheet, it needs to be two columns with the first column containing the initials and the second containing the e-mail address.

  6. #6
    Registered User
    Join Date
    09-13-2012
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Several persons initials in one cell, sent to their respective e-mail addresses.

    Hi Walleye,

    I seem to be getting 'Runtime error '-2147221238 (8004010a)' automation error with this line of code?
    I've tried creating a named range on a different sheet like you suggested but to no avail.

    I have it working with one set of initials now, but when I seperate the initials with a "," and try to add another I get Runtime error '-2147221238 (8004010a)' automation error? I can get the error to not come up, but then the second set of initials in the cell still doesn't get an e-mail?

     With OutMail
                    .To = strto
    Last edited by JosephP; 09-26-2012 at 07:11 AM.

  7. #7
    Forum Contributor wallyeye's Avatar
    Join Date
    05-06-2011
    Location
    Arizona
    MS-Off Ver
    Office 2010, 2007
    Posts
    308

    Re: Several persons initials in one cell, sent to their respective e-mail addresses.

    I did find a bit of a logic error, it was creating one e-mail and updating the To line with each new entry. So, I moved the:

                        Set OutMail = OutApp.CreateItem(0)
    inside the loop.

    Try this attached spreadsheet EmailWithSplit.xls, I've put initials in column L and created a NamedRange on a new EMail worksheet. Note there are no spaces between the initials in column L.

  8. #8
    Registered User
    Join Date
    09-13-2012
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Several persons initials in one cell, sent to their respective e-mail addresses.

    I'm still getting the same error when the initials are grouped as such DD,CJ. When I add a space it doesn't give me the error but doesn't send the e-mail. Any ideas? I ran the attached sheet unedited and still no luck .

    AHA! I've made it work but putting the line of code you specified within the intAddress field like so

    Private Sub Workbook_Open()
    Dim i As Long
    Dim OutApp As Object
    Dim OutMail As Object
    Dim strto As String, strcc As String, strbcc As String
    Dim strsub As String, strbody As String
    Dim arrInits       As Variant
    Dim intAddress   As Integer
        
        For i = 6 To Sheets("FMEA").Range("B65536").End(xlUp).Row
            If Sheets("FMEA").Cells(i, 13).Value = (Date) Then
                Set OutApp = CreateObject("Outlook.Application")
                OutApp.Session.Logon
                'Set OutMail = OutApp.CreateItem(0)
               
    
                If InStr(1, Sheets("FMEA").Cells(i, 12).Value, ",") > 0 Then
                     arrInits = Split(Sheets("FMEA").Cells(i, 12).Value, ",")
                Else
                     ReDim arrInits(1 To 1)
                     arrInits = Sheets("FMEA").Cells(i, 12).Value
                End If
    
                For intAddress = LBound(arrInits) To UBound(arrInits)
                    Set OutMail = OutApp.CreateItem(0)                 On Error Resume Next
                     strto = Application.WorksheetFunction.VLookup(arrInits(intAddress), _
                        ThisWorkbook.Names("NameMap5").RefersToRange, 2, False)
                     If Err <> 0 Then
                        strto = ""
                     End If
                     On Error GoTo 0
    
                     If strto > "" _
                     And strto <> "#N/A" Then
                'strto = Sheets("FMEA").Cells(i, 12).Value
                'strcc = Sheets("FMEA").Cells(i, 13).Value & _
                            "; " & Sheets("FMEA").Cells(i, 5).Value
                strbcc = ""
                strsub = "Contract Expiry Notice"
                strbody = "Hi there" & vbNewLine & vbNewLine & _
                    "Your contract, " & Sheets("FMEA").Cells(i, 11).Value & _
                    ", is due to expire on " & Sheets("FMEA").Cells(i, 13).Value & _
                    ". Please contact us at your earliest convenience." & _
                    vbCrLf & vbCrLf & "Thank you."
        
                With OutMail
                    .To = strto
                    .CC = strcc
                    .BCC = strbcc
                    .Subject = strsub
                    .Body = strbody
                    .Send
                    
                End With
                      End If
        
                'Set OutMail = Nothing
                'Set OutApp = Nothing
                
                Next intAddress
                
            End If
        Next i
    End Sub
    Thanks Walleye you've been a great help. I think this may be solved
    Last edited by JosephP; 09-26-2012 at 07:11 AM.

  9. #9
    Registered User
    Join Date
    09-13-2012
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Several persons initials in one cell, sent to their respective e-mail addresses.

    Hi Walleye,

    I seem to be having a problem with what I believe is this piece of code.

    For i = 6 To Sheets("FMEA").Range("B65536").End(xlUp).Row
            If Sheets("FMEA").Cells(i, 13).Value <= (Date) Then
                Set OutApp = CreateObject("Outlook.Application")
                OutApp.Session.Logon
    
                If InStr(1, Sheets("FMEA").Cells(i, 12).Value, ",") > 0 Then
                     arrInits = Split(Sheets("FMEA").Cells(i, 12).Value, ",")
                'Else
                     'ReDim arrInits(1 To 1)
                     'arrInits = Sheets("FMEA").Cells(i, 12).Value
                End If
    My problem is that the code works fine for the sample sheet I sent you but with other sheets it will miss initials and not send e-mails at the end (these sheets have more rows and more dates)

    Example: judging by the initials it should be sending 11 e-mails but is only sending 9 - ending the sub after the 9th and ignoring the last 2.

    sample sheet.xls

    If you need anything let me know, tearing my hair out here >.<

    Thanks a lot in advance
    Last edited by JosephP; 09-26-2012 at 07:10 AM. Reason: corrected code tags

  10. #10
    Forum Contributor wallyeye's Avatar
    Join Date
    05-06-2011
    Location
    Arizona
    MS-Off Ver
    Office 2010, 2007
    Posts
    308

    Re: Several persons initials in one cell, sent to their respective e-mail addresses.

    The last row was checking column B, but the initials in column L went further down. And, I messed up the alternate array assignment. I've cleaned it up considerably here, and made it more robust: sample sheet.xls

  11. #11
    Registered User
    Join Date
    09-13-2012
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Several persons initials in one cell, sent to their respective e-mail addresses.

    Seems to work fine, I'll test it live tomorrow morning.

    Thanks again, you've been amazing

    Lacessit

    Edit: worked perfectly, thanks again for all your help! Now I'm just going to work in a counter with the initials and make a graph with it

    Edit: Just in the process of adding an extra column to the namemap and introducting a counter which will track e-mails sent to each initial and having a bit of trouble fitting it in >.<
    Last edited by Lacessit; 09-27-2012 at 05:55 AM.

  12. #12
    Forum Contributor wallyeye's Avatar
    Join Date
    05-06-2011
    Location
    Arizona
    MS-Off Ver
    Office 2010, 2007
    Posts
    308

    Re: Several persons initials in one cell, sent to their respective e-mail addresses.

    It is difficult to get your head around the scp objects and arrays! Here is an updated sample sheet that builds a separate row index for the initials, and an array to hold the counters:

    sample sheet.xls

  13. #13
    Registered User
    Join Date
    09-13-2012
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Several persons initials in one cell, sent to their respective e-mail addresses.

    Haha you're telling me, I see what you have done though so I'm learning Thanks again for all your help!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1