• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Email not sending, code does not error

WjVBA

New Member
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 <<<
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:
I thought the way it's wrote, strbody <>"" would be looking for not equal to empty, store it and send the details
 
WjVBA
I see.
You've written that Password input comes
and it seems that You've given correct password too.
But You've skipped to check/verify - what is strbody's real value?
Of course, Your code do that checking ... but is it?
Have You checked - do Your code give any error codes?

When next IF-sentence should be TRUE?
If Cells(i, 1).Value <> Cells(i, 1).Value Then
 
Ahh that may be it, 'If Cells(i, 1)' will be returning a FALSE. I will check tomorrow. Thanks for spotting that. Maybe I should use the prev value to check against instead
 
Back
Top