+ Reply to Thread
Results 1 to 6 of 6

send automated email with comment based on checked box

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-09-2013
    Location
    Chicago
    MS-Off Ver
    Excel 2016
    Posts
    543

    send automated email with comment based on checked box

    In the the code below I have changed the "x" to a checkbox and can not seem to get the syntax correct. Basically on an excel 2010 spreadsheet I have two comments that I am trying to use VB to send an email with the comment selected (checked). Thank you .

    VB

    Private Sub CommandButton1_Click()
        Dim WS As Worksheet, Rng As Range, c As Range
        Dim OutApp As Object, OutMail As Object
        Dim Msg As String, Addr As String, FName As String, i As Long
        
        Set OutApp = CreateObject("Outlook.Application")
        Set WS = ThisWorkbook.Sheets("Sheet1")
        Set Rng = WS.Range("A2", WS.Range("A" & Rows.Count).End(xlUp))
        For Each c In Rng
            Msg = Msg & "For " & c.Offset(, 1) & Chr(14) & Chr(14)
            For i = 3 To 14
                If WS.Cells(c.Row, i) = "xlOn" Then
                    Msg = Msg & "   -" & WS.Cells(1, i) & Chr(14)
                End If
            Next
            Msg = Msg & Chr(14) & "Thank you,"
            Msg = Msg & Chr(14) & "Christopher McCabe"
            Set OutMail = OutApp.CreateItem(0)
            On Error Resume Next
            With OutMail
            .To = c.Offset(, 0)
            .CC = ""
            .BCC = ""
            .Subject = "Daily Operational Safety Briefing"
            .Attachment = "C:\Users\cmccabe\Desktop\DOSE reporting form 10-14-15.xlsx"
            .Body = Msg
            .Attachments.Add ("C:\Users\cmccabe\Desktop\DOSE reporting form 10-14-15.xlsx")
            .Send
            End With
            
            MsgBox "The data has been emailed sucessfully.", vbInformation
         Next c
        
        Set OutMail = Nothing
        Set OutApp = Nothing
    
    End Sub

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: send automated email with comment based on checked box

    Hello cmccabe,

    It would be easier to spot the problem with a copy of the workbook. Can you post a copy?
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Forum Contributor
    Join Date
    05-09-2013
    Location
    Chicago
    MS-Off Ver
    Excel 2016
    Posts
    543

    Re: send automated email with comment based on checked box

    I have attached the spreadsheet and am getting a Type Mismatch error on the line in bold. Thank you .

    Private Sub CommandButton1_Click()
        Dim WS As Worksheet, Rng As Range, c As Range
        Dim OutApp As Object, OutMail As Object
        Dim Msg As String, Addr As String, FName As String, i As Long
        Dim obj As Object
        
        On Error Resume Next
        Set obj = GetObject(, "Outlook.Application")
        On Error GoTo 0
        If obj Is Nothing Then
            Set obj = CreateObject("Outlook.Application")
        End If
       
        Set OutApp = CreateObject("Outlook.Application")
        Set WS = ThisWorkbook.Sheets("Sheet1")
        Set Rng = WS.Range("A2", WS.Range("A" & Rows.Count).End(xlUp))
        For Each c In Rng
            Msg = Msg & "For " & c.Offset(, 1) & Chr(14) & Chr(14)
            For i = 3 To 14
                If WS.Cells(c.Row, "Checkbox" & i - 1).Object.Value = True Then
                Msg = Msg & "   -" & WS.Cells(1, i) & Chr(14)
                End If
            Next
            Msg = Msg & Chr(14) & "Thank you,"
            Msg = Msg & Chr(14) & "Christopher McCabe"
            Set OutMail = OutApp.CreateItem(0)
            On Error Resume Next
            With OutMail
            .To = c.Offset(, 0)
            .CC = ""
            .BCC = ""
            .Subject = "Daily Operational Safety Briefing"
            .Attachment = "C:\Users\cmccabe\Desktop\DOSE reporting form 10-14-15.xlsx"
            .Body = Msg
            .Attachments.Add ("C:\Users\cmccabe\Desktop\DOSE reporting form 10-14-15.xlsx")
            .Send
            End With
            
            MsgBox "The data has been emailed sucessfully.", vbInformation
         Next c
        
        Set OutMail = Nothing
        Set OutApp = Nothing
    
    End Sub
    Attached Files Attached Files

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: send automated email with comment based on checked box

    Hello Christopher,

    I have been busy but have a solution to your problem. I created two macros for the ActiveX combo boxes.

    Since you are locating these by cell address, it is very important the check box fits inside the cell. The macro AddCheckBoxes will add an ActiveX check box and center it in each cell you have selected. This prevents errors in locating the check box by cell address.

    The second macro MapCheckBoxes creates a dictionary object to store the check boxes in. The check boxes are returned from the dictionary by the cell address. The cell address is in absolute A1 style, i.e. $A$1. If you enter a cell address that does not have a check box, an error will occur.

    I am not sure why you are using Chr(14) in your code, but I left it intact. I was going to change to Chr(13), carriage return, but decided perhaps the recipient's email client may require it.

    Here is the code that has been added...

    Module - Checkbox_Macros
    Global ChkBoxes As Object
    
    Sub MapCheckBoxes(ByRef Wks As Worksheet)
    
        Dim obj As Object
        Dim Wks As Worksheet
        
            If ChkBoxes Is Nothing Then
                Set ChkBoxes = CreateObject("Scripting.Dictionary")
                ChkBoxes.CompareMode = vbTextCompare
            End If
            
            For Each obj In Wks.OLEObjects
                If TypeName(obj.Object) = "CheckBox" Then
                    If Not ChkBoxes.Exists(obj.TopLeftCell.Address) Then
                        ChkBoxes.Add obj.TopLeftCell.Address, obj
                    End If
                End If
            Next obj
            
    End Sub
    
    Sub AddCheckBoxes()
    
        ' Add an ActiveX CheckBox to the center of each selected cell.
        
        Dim Cell    As Range
        Dim ChkBox  As Object
        Dim Rng     As Range
        Dim Wks     As Worksheet
        
            Set Wks = ActiveSheet
            
            Set Rng = Application.Selection
            If TypeName(Rng) <> "Range" Then Exit Sub
            
                For Each Cell In Rng
                    Set ChkBox = Wks.OLEObjects.Add("Forms.CheckBox.1")
                    ChkBox.Height = 13.5
                    ChkBox.Width = 16.5
                    ChkBox.Left = Cell.Left + ((Cell.Width - ChkBox.Width) / 2)
                    ChkBox.Top = Cell.Top + ((Cell.Height - ChkBox.Height) / 2)
                Next Cell
                
    End Sub
    CommandButton1_Click()
    Private Sub CommandButton1_Click()
    
        Dim WS As Worksheet, Rng As Range, c As Range
        Dim OutApp As Object, OutMail As Object
        Dim Msg As String, Addr As String, FName As String, i As Long
        Dim MyFile As String
        Dim obj As Object, InBox As Object
        
            MyFile = "C:\Users\cmccabe\Desktop\DOSE reporting form 10-14-15.xlsx"
            
            If Dir(MyFile) = "" Then
                MsgBox "The file to attach was not found." & vbLf & vbLf & MyFile, vbExclamation, "Aborting Email"
                GoTo Cleanup
            End If
            
            On Error Resume Next
                Set OutApp = GetObject(, "Outlook.Application")
                If OutApp Is Nothing Then
                    Set OutApp = CreateObject("Outlook.Application")
                End If
                Set InBox = OutApp.Session.GetDefaultFolder(4)
            On Error GoTo 0
            
            Set WS = ThisWorkbook.Sheets("Sheet1")
            Set Rng = WS.Range("A2", WS.Range("A" & Rows.Count).End(xlUp))
            
            Call MapCheckBoxes(WS)
        
            For Each c In Rng
                Msg = Msg & "For " & c.Offset(, 1) & Chr(14) & Chr(14)
                    For i = 3 To 14
                        If CheckBoxes(WS.Cells(c.Row, "Checkbox" & i - 1).Object).Value = True Then
                            Msg = Msg & "   -" & WS.Cells(1, i) & Chr(14)
                        End If
                    Next i
                Msg = Msg & Chr(14) & "Thank you,"
                Msg = Msg & Chr(14) & "Christopher McCabe"
                
                    With OutApp.CreateItem(0)
                        .To = c.Offset(, 0)
                        .CC = ""
                        .BCC = ""
                        .Subject = "Daily Operational Safety Briefing"
                        .Body = Msg
                        .Attachments.Add MyFile, 1
                        .Send
                    End With
            
                MsgBox "The data has been emailed sucessfully.", vbInformation
            Next c
        
    Cleanup:
            Set OutApp = Nothing
    
    End Sub
    Attached Files Attached Files

  5. #5
    Forum Contributor
    Join Date
    05-09-2013
    Location
    Chicago
    MS-Off Ver
    Excel 2016
    Posts
    543

    Re: send automated email with comment based on checked box

    Thank you very much, works great .

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: send automated email with comment based on checked box

    Hello Christopher,

    You're welcome. If you have any questions about what the code does, please ask. I will be glad to explain it.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] How can I send a sheet in an Automated email with VBA?
    By gmr4evr1 in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 07-28-2015, 10:10 AM
  2. [SOLVED] Automated Email VBA from Excel - Freezes before send
    By ELeGault in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 12-16-2014, 12:37 AM
  3. Send automated email if value in cell is value
    By DelKolio in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 07-16-2014, 08:53 AM
  4. Workflow with signatures (if cell=x then send email, if signed then send email)
    By Kate2811 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-25-2014, 05:37 AM
  5. Macro to send automated email reminders
    By Amber12 in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 02-13-2014, 11:22 AM
  6. command button VBA to send automated email not workbook or worksheet
    By lpratt in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-08-2012, 11:13 PM
  7. Automated email to send worksheet as report
    By wellseytd in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-10-2011, 10:28 AM

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