jonbutterworth
IS-IT--Management
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:
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
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