I've written some code to make excel check for changes and prompt for a password when a button is clicked. If password is correct it should send an email detailing the changes. Password input comes up but email doesn't send. What have I done wrong?
>>> use code - tags <<<
>>> use code - tags <<<
Code:
Private Sub CommandButton1_Click()
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 i As Integer
Dim password As String
'Prompt for password
password = InputBox("Enter password:")
If password = "mypassword" Then
'Set email properties
strto = "recipient@email.com" 'replace with email address of recipient
strsub = "Changes made in column A" 'replace with email subject
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
'Check for changes in column A
For i = 2 To Range("A" & Rows.Count).End(xlUp).Row
If Cells(i, 1).Value <> Cells(i, 1).Value Then
'Add details of changed row to email body
strbody = strbody & "Row " & i & ": " & Cells(i, 1).Value & " changed to " & Cells(i, 1).Value & vbNewLine
End If
Next i
'Send email with details of changes
If strbody <> "" Then
With OutMail
.To = strto
.Subject = strsub
.Body = strbody
.Send
End With
End If
Set OutMail = Nothing
Set OutApp = Nothing
Else
MsgBox "Incorrect password. Email not sent."
End If
End Sub
Last edited by a moderator: