Animalised
New Member
- Joined
- Apr 24, 2016
- Messages
- 31
Hello Everyone,
I'm very new to VBA but i have written some code to send an email when someone clicks a button.
The code works fine if you only want to click one button, but if you want to click other buttons while in the same session, it will show the following error message:
'-2147467259 (80004005)': Method 'MailEnvelope' of object '_Worksheet' failed
The code is as follows:
(As demonstrated in the code, i repeat the exact same code across all members of staff).
I want to be able to fix this, but i cant find a solution to this online.
Any help would be very appreciated!
Thank you,
Harley.
I'm very new to VBA but i have written some code to send an email when someone clicks a button.
The code works fine if you only want to click one button, but if you want to click other buttons while in the same session, it will show the following error message:
'-2147467259 (80004005)': Method 'MailEnvelope' of object '_Worksheet' failed
The code is as follows:
Code:
Private Sub RoyTattersfield_Click() Sheets("Roy Tattersfield").Select
ActiveSheet.Unprotect "TIRCarnet"
' Select the range of cells on the active worksheet.
Sheets("Roy Tattersfield").Range("A1:H30").Select
' Show the envelope on the ActiveWorkbook.
ActiveWorkbook.EnvelopeVisible = False
' Set the optional introduction field thats adds
' some header text to the email body. It also sets
' the To and Subject lines. Finally the message
' is sent.
With ActiveSheet.MailEnvelope
.Introduction = "Hi Tom," & vbNewLine & vbNewLine & _
"Below is an detailed extract of Roy Tattersfield's spending."
.Item.To = "Example@Email.com"
.Item.CC = ""
.Item.Subject = "Roy Tattersfield - Overspending"
.Item.Send
End With
Sheets("Roy Tattersfield").Select
ActiveSheet.Protect "TIRCarnet"
Sheets("Summary").Select
Cells(11, "D").Value = "Notified"
Range("D11").Interior.Color = RGB(0, 0, 255)
Range("D11").Font.Color = RGB(255, 255, 0)
Range("D11").Font.Bold = True
End Sub
Private Sub SteveAuty_Click()
Sheets("Steve Auty").Select
ActiveSheet.Unprotect "TIRCarnet"
' Select the range of cells on the active worksheet.
Sheets("Steve Auty").Range("A1:H30").Select
' Show the envelope on the ActiveWorkbook.
ActiveWorkbook.EnvelopeVisible = False
' Set the optional introduction field thats adds
' some header text to the email body. It also sets
' the To and Subject lines. Finally the message
' is sent.
With ActiveSheet.MailEnvelope
.Introduction = "Hi Tom," & vbNewLine & vbNewLine & _
"Below is an detailed extract of Steve Auty's spending."
.Item.To = "Example@Email.com"
.Item.CC = ""
.Item.Subject = "Steve Auty - Overspending"
.Item.Send
End With
Sheets("Steve Auty").Select
ActiveSheet.Protect "TIRCarnet"
Sheets("Summary").Select
Cells(6, "H").Value = "Notified"
Range("H6").Interior.Color = RGB(0, 0, 255)
Range("H6").Font.Color = RGB(255, 255, 0)
Range("H6").Font.Bold = True
End Sub
(As demonstrated in the code, i repeat the exact same code across all members of staff).
I want to be able to fix this, but i cant find a solution to this online.
Any help would be very appreciated!
Thank you,
Harley.
Last edited: