Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel VBA to Email Selected Worksheet depending on value

Status
Not open for further replies.

jonbutterworth

IS-IT--Management
Sep 21, 2010
17
GB
Hey Guys,

I'm new to VBA! :)

I am creating an interesting workbook containing 2 sheets. One called Expenses and one called Overtime.

Basically I have created a form with 3 check boxes:

Manager1
Manager2
Other: (Textbox here)

Send Cancel

So I want the user to be able to select which manager they have or have the option of typing an address in also and sending to that address.

So I understand that the code would potentially need to be under the button click feature to send the mail, and add the values of the checkboxes to the TO: field of the mail send. My questions are:

Can I hardcode the email addresses to the 2 managers to the check boxes and how to I tell the send button to check the text box for an additional email address?

My second question is, the workbook has 2 sheets, I want to always send the 1st sheet (expenses) but only send the second sheet if D8 has a value in. If D8 has no value then that user has recorded no overtime that month and there is no need for sending that sheet.

I have some rough ideas as to how to piece this together but as I add more ideas the code evades me.

I have found the following code for sending an email, and I understand most of it:

Code:
Sub Mail_ActiveSheet()
'Working in 97-2010
    Dim FileExtStr As String
    Dim FileFormatNum As Long
    Dim Sourcewb As Workbook
    Dim Destwb As Workbook
    Dim TempFilePath As String
    Dim TempFileName As String
    Dim I As Long

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    Set Sourcewb = ActiveWorkbook

    'Copy the sheet to a new workbook
    ActiveSheet.Copy
    Set Destwb = ActiveWorkbook

    'Determine the Excel version and file extension/format
    With Destwb
        If Val(Application.Version) < 12 Then
            'You use Excel 97-2003
            FileExtStr = ".xls": FileFormatNum = -4143
        Else
            'You use Excel 2007-2010, we exit the sub when your answer is
            'NO in the security dialog that you only see  when you copy
            'an sheet from a xlsm file with macro's disabled.
            If Sourcewb.Name = .Name Then
                With Application
                    .ScreenUpdating = True
                    .EnableEvents = True
                End With
                MsgBox "Your answer is NO in the security dialog"
                Exit Sub
            Else
                Select Case Sourcewb.FileFormat
                Case 51: FileExtStr = ".xlsx": FileFormatNum = 51
                Case 52:
                    If .HasVBProject Then
                        FileExtStr = ".xlsm": FileFormatNum = 52
                    Else
                        FileExtStr = ".xlsx": FileFormatNum = 51
                    End If
                Case 56: FileExtStr = ".xls": FileFormatNum = 56
                Case Else: FileExtStr = ".xlsb": FileFormatNum = 50
                End Select
            End If
        End If
    End With

    '    'Change all cells in the worksheet to values if you want
    '    With Destwb.Sheets(1).UsedRange
    '        .Cells.Copy
    '        .Cells.PasteSpecial xlPasteValues
    '        .Cells(1).Select
    '    End With
    '    Application.CutCopyMode = False

    'Save the new workbook/Mail it/Delete it
    TempFilePath = Environ$("temp") & "\"
    TempFileName = "Part of " & Sourcewb.Name & " " _
                 & Format(Now, "dd-mmm-yy h-mm-ss")

    With Destwb
        .SaveAs TempFilePath & TempFileName & FileExtStr, _
                FileFormat:=FileFormatNum
        On Error Resume Next
        For I = 1 To 3
            .SendMail "ron@debruin.nl", _
                      "This is the Subject line"
            If Err.Number = 0 Then Exit For
        Next I
        On Error GoTo 0
        .Close SaveChanges:=False
    End With

    'Delete the file you have send
    Kill TempFilePath & TempFileName & FileExtStr

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
End Sub

But I am confused as to how I can acheive what I want out of it.

I wonder if you could help, I'd be forever gratefull! :)

Thanks,

Jon
 
Hey Guys,

I know that the sound of my post, sounds like I'm asking someone to basically complete everything for me, but thats not what I'm asking at all.

I'm just looking for some guidlines as to where I start.

I've got the following:

The form that has the txt box, the two check boxed (each manager) a cancel button and an ok button:

Code:
Option Explicit



Private Sub CheckBox1_Click()

    

End Sub

Private Sub cmdCancel_Click()
    Unload Me
End Sub

Then I have a Module that is attached to the button on the spreadsheet that shows the email window:

Code:
Option Explicit

Sub OpenMail()
    
    frmEmail.Show

End Sub

I also have a calendar pop up module and form that are working and enter dates into the relevant cells.

So I'm just looking for help on the code that sends the emails and decides which addresses the email is sent to.

Please can someone help?

Thanks,

Jon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top