Results 1 to 3 of 3

Sending an email if Current User matches cell values

Threaded View

  1. #1
    Forum Contributor
    Join Date
    08-14-2016
    Location
    Warrington, England
    MS-Off Ver
    MS 365
    Posts
    174

    Sending an email if Current User matches cell values

    Hi everyone,

    I am trying to design a macro which will detect the current user (based on their login) and if that user matches the user name in a cell it will email the contents of other cells to that users email address. It's purpose is to be a forgotten your password style event on a login screen.

    For example I have a list of usernames in Cell F8 with that users email address in E8 and their password in H8 with a user below that one in F9 and so on.

    Can anyone shed some light on how i would get this to work? See my code so far:

    Private Sub CommandButton2_Click()
    
    Application.ScreenUpdating = False
    
    Dim RowH As Integer
    Dim Counter As Integer
    Dim WinUser As Variant
    Dim mailAddress As String
    Dim Ash As Worksheet
    Dim rng As Range
    Dim OutApp As Object
    Dim OutMail As Object
    Dim cell As Range
    
    RowH = 1
    Counter = 0
    
    ThisWorkbook.Application.Iteration = True
    ThisWorkbook.Application.MaxIterations = 1
    ThisWorkbook.Application.MaxChange = 0.001
    
    'Capture the BRID of the person logged into the machine
    WinUser = Environ("UserName")
    
    'Check if user on list and increase Counter by 1 if True
    Do Until Sheets("Login").Cells(RowH, 6) = "NO USER"
    
    RowH = RowH + 1
    
        If Sheets("Login").Cells(RowH, 6).Value = WinUser Then
        Counter = Counter + 1
        End If
    Loop
    
    Sheets("Interface").Activate
    Application.ScreenUpdating = False
    Set OutApp = CreateObject("Outlook.Application")
    
    On Error GoTo cleanup
    mailAddress = ""
    On Error Resume Next
    
              On Error GoTo 0
    
    If mailAddress <> "" Then
    
    With Ash.AutoFilter.Range
    On Error Resume Next
    
    Set rng = .SpecialCells(xlCellTypeVisible)
    
    On Error GoTo 0
    End With
    
    Set OutMail = OutApp.CreateItem(0)
    
    On Error Resume Next
    With OutMail
    
    .to = mailAddress
    .Subject = "Test mail"
    .Display  'Or use Send
    
    End With
    On Error GoTo 0
    
    Set OutMail = Nothing
    
    End If
    
    cleanup:
    
    Set OutApp = Nothing
    Application.ScreenUpdating = True
    
    Sheets("Interface").Activate
    If Counter = 0 Then
    
    MsgBox "You do not have a profile.", , "Notification"
    
    End If
    
    Application.ScreenUpdating = False
    
    End Sub
    At present when the counter = 1 line is removed then the macro runs and returns the message you have no profile. when it is added back in the macro does nothing as I cant get the counter = to to trigger sending the email and how to get it to refer to the relevant cells as mentioned above.

    Many thanks in advance!
    Last edited by Catsonheat; 02-23-2017 at 05:13 PM.
    Excel Beginner

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. losing focus of current email when sending
    By johncassell in forum Outlook Programming / VBA / Macros
    Replies: 0
    Last Post: 05-15-2015, 09:02 AM
  2. [SOLVED] VBA to email sheet to current user
    By AndyMan81 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-24-2014, 10:34 AM
  3. Sending Email from Excel VBA through Outlook from different user
    By jeff_kaufman in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-05-2013, 04:19 PM
  4. Checking user provided email with current user in document
    By AJRight in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-17-2012, 12:29 PM
  5. Add signature to Outlook based on the user sending the email/ Multiple users
    By hanott1 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-21-2011, 03:40 PM
  6. Email current user
    By Tony Vargo in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-26-2010, 08:29 AM
  7. Sending email using excel with current time/date
    By undergraduate in forum Excel General
    Replies: 1
    Last Post: 03-24-2010, 02:11 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1