Find two cells values in hidden sheet and store as string - excel

My idea is to make a (Forget Password) form that will restore the (Username) & (Password) and send it via Email Address provided by the user. In the following picture the customer will enter his Email address so the procedure code will find an exact match of the Email Address in hidden sheet table, if there is a match the next two cells will be stored as a string.
As you can see down below this is the hidden sheet with a table that contains information about registered customers, so when we get Email matching the (Username) and (Password) will be stored as string ((!!without activating or seeing this sheet!!))
This is my current code:
Public Function send_email()
Dim NUser As String
Dim NPass As String
Dim info As Variant
Set cdomsg = CreateObject("CDO.message")
With cdomsg.Configuration.Fields
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
.Item("http://schemas.microsoft.com/cdo/configuration/smptserverport") = "587"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "dash32762#gmail.com"
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "*******"
.Update
End With
' ========(( below is the code i want to find and store user and pass ))
Set info = Worksheets("AdminPanel2").Range("I11:I80").Find( _
What:=Me.txt_pass.Value, LookIn:=xlFormulas)
If Not info Is Nothing Then
info.Parent.Activate
info.Offset(0, 1).Select
NUser = ActiveCell.Text
MsgBox "That data was sent"
Else
MsgBox "That data was not found."
End If
'===========(( below code i want to recall it in body of the email ))
With cdomsg
.To = info
.From = "dash32762#gmail.com"
.Subject = "Restore information"
.TextBody = Hello youur username is NUser your password is NPass
.send
End With
Set cdomsg = Nothing
End Function
This is the code I want to modify:
' ========(( below is the code i want to find and store user and pass ))
Set info = Worksheets("AdminPanel2").Range("I11:I80").Find( _
What:=Me.txt_pass.Value, LookIn:=xlFormulas)
If Not info Is Nothing Then
info.Parent.Activate
info.Offset(0, 1).Select
NUser = ActiveCell.Text
MsgBox "That data was sent"
Else
MsgBox "That data was not found."
End If

Sending email and password in one email is very bad. Keeping email and password in a hidden Excel sheet is even worse. This is really not how it is done and if this is not some school project, you may have a lot of problems at work. The best practice is not to keep a password, but to keep its hash. And not to send the old password, but make a new one.
Having said all of the above, the .TextBody should be a string with & between the variables like this:
With cdomsg
.To = info
.From = "dash32762#gmail.com"
.Subject = "Restore information"
.TextBody = "Hello your username is" & NUser & " your password is" & NPass
.Send
End With
And concerning the part in your question:
With Worksheets("AdminPanel2").Range("I11:I80")
Set info = .Find(What:=Me.txt_pass.Value, LookIn:=xlValues, LookAt:=xlWhole)
End With
If Not info Is Nothing Then
NUser = info.Offset(0, 1)
MsgBox "That data was sent for user " & info
Else
MsgBox "That data was not found."
End If
Range.Find Method()
How to avoid using Select in Excel VBA

Related

Find command - How to match names on multiple tabs and input data?

I am working on a spreadsheet that has to be completed quarterly so am looking to automate a lot of the process. I have a master tab - "#" and 16 Team tabs. On the # is a table which includes all 16 team names, I would like this table to update depending on what Teams have signed off.
Quick runthrough:
Spreadsheet is emailed out to multiple teams, each log in and review Products. After all products have been reviewed, they press the "Sign Off" button. This button does 3 things;
Inputs username and date to right of button
Sends email to spreadsheet owner
Updates table on "#" tab.
Number 3 is where I am having the issue. I have tried find, if, functions - lots of different options but just can't get it to work. Functions didn't work as the spreadsheet is reset every quarter so the cell values are cleared, so it needs to be VBA.
Some previous options I tried:
Sub If_Team 1()
'Set variables
Set sht1 = Sheets("#")
Set sht2 = Sheets("Team 1")
'Team1
If sht2.Range("M2:N2") <> "" Then
sht1.Range("C4:D4") = sht2.Range("M2:N2")
sht1.Range("B4") = "P"
Else
sht1.Range("C4:D4") = ""
sht1.Range("B4") = "O"
End If
Unfortunately this worked until I put in more If functions, where it then pasted the data in the whole table rather than just Team 1. The below also worked, until again adding more values where it pasted the data in every field where the criteria was met (which was them all).
If pfID = "Team 1" Then GoTo 1 Else
If pfID = "Team 2" Then GoTo 2 Else
1 sht2.Cells(3, 2).Value = "P"
sht2.Cells(3, 3).Value = Date
sht2.Cells(3, 4).Value = Environ("username")
On each team tab is the team name, so lets say "Team 1". Team 1 is found in Cell "F1" on the Active Team Sheet. On the # tab in the table, Team 1 is Cell "A3".
What I would like to happen is ActiveSheet.Range("F1") to find the same name on the # (sht2) tab, and then do the following if the names match (so as we know Team 1 on the # tab is "A3"):
sht2.Range("A4").Value = "P"
sht2.Cells("A5").Value = Date
sht2.Cells("A6").Value = Environ("username")
This way the spreadsheet owner will only need to review the # tab to see who has signed off, rather than go through each tab. However I don't want it to point to an invdividual cell like above as I would like it to find and match the names.
Here is the full code so far:
Sub Button2_Click() 'SIGN OFF BUTTON
Dim cellAddr As String
Dim aCol As Long
' Declare variables
Dim c As Integer ' Column
Dim emBody As String ' Body text of email
Dim emCnt As Integer ' Count of email addressees
Dim emTitl As String ' Subject line of email
Dim emTxt As String ' List of email addressees
Dim myOutlook As Object ' MS Outlook application
Dim mySendmail As Object ' The email to be sent
Dim pfID As String ' Platform ID
Dim r As Integer ' Row
'Set variables
Set sht1 = ActiveSheet
Set sht2 = Sheets("#")
'Cell Address
cellAddr = ActiveSheet.Shapes(Application.Caller).TopLeftCell.Address
'Column Number
aCol = ActiveSheet.Shapes(Application.Caller).TopLeftCell.Column
'Input Date and Username
If aCol <> 1 Then _
sht1.Range(cellAddr).Offset(, 2).Value = Date
sht1.Range(cellAddr).Offset(, 1).Value = Environ("username")
' Obtain Platform details
pfID = ActiveSheet.Range("F1").Value
'Version ID
vID = sht2.Range("D1").Value
**'Input Sign Off on "#" Tab**
' Email subject line
emTitl = pfID & " - Out of Support Software Review " & vID & " Completed"
' Email body text
emBody = "<BODY style=font-size:12pt;font-family:Calibri>" & "Hi," & "<br>" & "<br>" & "Out of Support Software Review " & "<b>" & vID & "</b>" & " Completed for " & "<b>" & pfID & "</b>" & "."
Set myOutlook = CreateObject("Outlook.Application")
Set mySendmail = myOutlook.CreateItem(olMailItem)
With mySendmail
.to = ""
.Subject = emTitl
.HTMLBody = emBody
.Display
End With
' CLEAR.
Set objEmail = Nothing: Set objOutlook = Nothing
ErrHandler:
'
End Sub
Any help is appreciated, any questions let me know! Sorry if this is slightly confusing.

MS Access VBA is creating multiple report emails and parameters are not storing reference from form

I have am working on a report to pull employees with end dates between two unbound text fields on a form. The report is grouped on Supervisor. My goal is to save each report grouping as unique PDFs with the PDF name as the [Supervisor] - John Smith.PDF, Jill Smith.pdf etc. This report is attached to an email, which is opened and waiting for adjustment prior to sending.
Currently, this works except for a couple issues that I am trying to solve:
I get a Parameter popup asking for StartDate and EndDate (my parameters set to the form text fields) before each report opening. I have to manually enter this date for each parameter before each report, it is not populating from the form.
I get multiple emails opened for each supervisor. Some have blank reports. It seems like I am getting one for each record of each report, with the parameter popup for each.
After I hit the button, I input the dates into the parameter popups. My reports opens, saves, closes, and an email is generated correctly. Instead of moving to the next report/email automatically, I have to manually enter the date into the parameter popups again.
My Form has two fields Text0 and Text2 and a Button that will print a report based on the query below using VBA.
Query SQL:
SELECT DISTINCT [Active and Expired Badges].Supervisor_Clean, [Active and Expired Badges].FIRSTNAME, [Active and Expired Badges].LASTNAME, [Active and Expired Badges].[Premera ID], [Active and Expired Badges].Company, [Active and Expired Badges].Title, [Active and Expired Badges].[End Date], [FIRSTNAME] & " " & [LASTNAME] AS Name
FROM [Active and Expired Badges]
WHERE ((([Active and Expired Badges].Title) Like "*" & "outsource" & "*" Or ([Active and Expired Badges].Title) Like "*" & "Contingent" & "*") AND (([Active and Expired Badges].[End Date]) Between [StartDate:] And [StopDate:]));
Button VBA:
Private Sub Command5_Click()
'split report into PDFs named after supervisor and open a separate email with each report attached
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim MyFileName As String
Dim mypath As String
Dim temp As String
Dim qry As QueryDef
Dim StartDate As DAO.Parameter
Dim StopDate As DAO.Parameter
Set db = CurrentDb()
Set qry = db.QueryDefs("30-Day query")
mypath = "C:\Users\cw52450\Desktop\Test Exports\"
qry.Parameters("StartDate").Value = [Forms]![EndDate]![Text0]
qry.Parameters("StopDate").Value = [Forms]![EndDate]![Text2]
Set rs = qry.OpenRecordset(dbOpenSnapshot)
If Not (rs.EOF And rs.BOF) Then
'populate rs
rs.MoveLast
rs.MoveFirst
'start report generation loop
Do While Not rs.EOF
temp = rs("Supervisor_Clean")
MyFileName = rs("Supervisor_Clean") & Format(Date, ", mmm yyyy") & ".PDF"
DoCmd.OpenReport "End Date Report", acViewReport, , "[Supervisor_Clean]='" & temp & "'"
DoCmd.OutputTo acOutputReport, "", acFormatPDF, mypath & MyFileName
DoCmd.Close acReport, "End Date Report"
DoEvents
'start mail section
open Outlook, attach zip folder or file, send e-mail
Dim appOutLook As Outlook.Application
Dim MailOutLook As Outlook.MailItem
Set appOutLook = CreateObject("Outlook.Application")
Set MailOutLook = appOutLook.CreateItem(olMailItem)
With MailOutLook
.SendUsingAccount = appOutLook.Session.Accounts.Item(2)
.BodyFormat = olFormatRichText
'.To = ""
''.cc = ""
''.bcc = ""
.Subject = "Non-Employees with Expiring Building Access"
.HTMLBody = "<html><body><p>Hello,</p><p>The attached report... </p></body></html>"
.Attachments.Add (mypath & MyFileName)
'.DeleteAfterSubmit = True 'This would let Outlook send the note without storing it in your sent bin
.Display
End With
'end mail section
rs.MoveNext
Loop
Else
MsgBox "There are no records in the recordset."
End If
MsgBox "Report generation complete."
Set rs = Nothing
Set db = Nothing
Set qry = Nothing
End Sub
The popup for parameters could be because of incorrect syntax here:
qry.Parameters("StartDate").Value = [Forms]![EndDate]![Text0]
qry.Parameters("StopDate").Value = [Forms]![EndDate]![Text2]
Try to change it to:
qry.Parameters("StartDate").Value = Me.Text0
qry.Parameters("StopDate").Value = Me.Text2

Excel freezes after using macro that sends email using Collaboration Data Objects

I use a macro to send out emails, I am using this approach:
https://qdatalab.com/vba-macro-send-emails-using-gmail/
It works, except that after the email is sent Excel will freeze. It is only the things that are visible in the document that are "frozen". If I scroll down or switch sheet, it will update the screen.
For example if I delete a row, it will delete the row but not show it on the screen. If I switch to another sheet and back again, it will show the row has been deleted.
Is this some kind of memory problem or what might the cause be?
I am using the latest version of Windows 10 (v1903 i believe) and latest version of MS Excel.
When I close and re-open Excel everything is back to normal.
This is the code from the link:
Sub SendEmailUsingGmail()
On Error GoTo Err
Dim NewMail As Object
Dim mailConfig As Object
Dim fields As Variant
Dim msConfigURL As String
Set NewMail = CreateObject("CDO.Message")
Set mailConfig = CreateObject("CDO.Configuration")
' load all default configurations
mailConfig.Load -1
Set fields = mailConfig.fields
'Set All Email Properties
With NewMail
.Subject = "Test Mail"
.From = "email#gmail.com"
.To = "email2#gmail.com;email3#gmail.com"
.CC = "email4#gmail.com"
.BCC = ""
.textbody = ""
End With
msConfigURL = "http://schemas.microsoft.com/cdo/configuration"
With fields
'Enable SSL Authentication
.Item(msConfigURL & "/smtpusessl") = True
'Make SMTP authentication Enabled=true (1)
.Item(msConfigURL & "/smtpauthenticate") = 1
'Set the SMTP server and port Details
'To get these details you can get on Settings Page of your Gmail Account
.Item(msConfigURL & "/smtpserver") = "smtp.gmail.com"
.Item(msConfigURL & "/smtpserverport") = 465
.Item(msConfigURL & "/sendusing") = 2
'Set your credentials of your Gmail Account
.Item(msConfigURL & "/sendusername") = "email#gmail.com"
.Item(msConfigURL & "/sendpassword") = "********"
'Update the configuration fields
.Update
End With
NewMail.Configuration = mailConfig
NewMail.Send
MsgBox ("Mail has been Sent")
Exit_Err:
Set NewMail = Nothing
Set mailConfig = Nothing
End
Err:
Select Case Err.Number
Case -2147220973 'Could be because of Internet Connection
MsgBox " Could be no Internet Connection !! -- " & Err.Description
Case -2147220975 'Incorrect credentials User ID or password
MsgBox "Incorrect Credentials !! -- " & Err.Description
Case Else 'Rest other errors
MsgBox "Error occured while sending the email !! -- " & Err.Description
End Select
Resume Exit_Err
End Sub
Turns out the problem was the MsgBox. After deleting MsgBox ("Mail has been Sent") it works perfectly.
Found the solution here: Using CDO.sys dll for sending messages in VBA freezes Excel
Seems like a bug.

Populate an email with data from an Excel spreadsheet

I have an Excel spreadsheet of contacts. I want to set a drop-down list that sends an email to the specific person I choose and returns the contact info in the body of the email.
I don't know how to get the email to auto-populate and right now, the email that pops up has "true" in the body for the contact info rather than returning the text value in the cell.
Sub DropDown7_Change()
Dim answer As String
answer = MsgBox("Are you sure you want to assign this lead?", _
vbYesNo, "Send Email")
' Above code informs the user that an automated email will be sent
'Code uses the users answer to either carryout the generated email process or to not save the changes.
If answer = vbNo Then Cancel = True
If Cancel = True Then Exit Sub
If answer = vbYes Then
'Connects to outlook and retrieves information needed to create and send the email.
Set OutlookApp = CreateObject("Outlook.Application")
Set OlObjects = OutlookApp.GetNamespace("MAPI")
Set newmsg = OutlookApp.CreateItem(olMailItem)
'Contains the email address of the person receiving the email.
newmsg.Subject = "Lead Assigned to You" 'Sets the automated subject line to the email
newmsg.Body = "Hello," & vbNewLine & _
"You have been assigned a lead. Please follow up with the contact" & vbNewLine & _
ActiveCell.Offset(0, 3).Range("K5").Select
ActiveCell.Offset(0, 6).Range("K5").Select
ActiveCell.Offset(0, 7).Range("K5").Select
'Above code has the body of the automated email
newmsg.Display
End If
End Sub ' End of function
If you are trying to get the values that are Offset to Range("K5") , then you need to use the Offset with .Value , like this Range("K5").Offset(0, 3).Value , this will get the value 3 columns to the right of Cell "K5".
The code below, will add the values from 3 cells with Columns offset to cell "K5" to you email body:
Sub DropDown7_Change()
Dim answer As String
answer = MsgBox("Are you sure you want to assign this lead?", _
vbYesNo, "Send Email")
' Above code informs the user that an automated email will be sent
'Code uses the users answer to either carryout the generated email process or to not save the changes.
If answer = vbNo Then
Exit Sub
Else
If answer = vbYes Then
'Connects to outlook and retrieves information needed to create and send the email.
Set OutlookApp = CreateObject("Outlook.Application")
Set OlObjects = OutlookApp.GetNamespace("MAPI")
Set newmsg = OutlookApp.CreateItem(olMailItem)
'Contains the email address of the person receiving the email.
newmsg.Subject = "Lead Assigned to You" 'Sets the automated subject line to the email
newmsg.body = "Hello," & vbNewLine & _
"You have been assigned a lead. Please follow up with the contact" & vbNewLine & _
Range("K5").Offset(0, 3).Value & vbNewLine & _
Range("K5").Offset(0, 6).Value & vbNewLine & _
Range("K5").Offset(0, 7).Value & vbNewLine
'Above code has the body of the automated email
newmsg.Display
End If
End If
End Sub

Excel VBA code to read a username from a cell then send an email to that user

I require some help in creating Excel VBA code which will read a row of usernames from cells in Excel and then send an email to all those users by searching for the users email address in the Outlook contacts list.
I have managed to write the code that will bring up outlook's compose email dialog box from the spreadsheet.
You can use for in range with mails and call this proc to send email
Public Sub SendMail(MailTO As String, MailSubject As String, MailBody As String)
'http://officevb.com
Dim appOL As Object
Dim myEmail As Object
Dim TxtHello As String
Set appOL = CreateObject("Outlook.Application")
Set myEmail = appOL.CreateItem(olMailItem)
'Use hour to create a text
Select Case Hour(Time)
Case Is <= 12
TxtHello = "Good Morning," & vbNewLine
Case Is >= 12
TxtHello = "Good Afternoom," & vbNewLine
Case Is >= 18
TxtHello = "Good Night," & vbNewLine
End Select
With myEmail
.display
.Recipients.Add MailTO
.Subject = MailSubject
.Body = TxtHello & MailBody
.Send
End With
Set myEmail = Nothing
Set appOL = Nothing
End Sub
call this sub passing these parameters
sendMail "Mail#yourContact.com","Test","This is a automatic mail"
[]´s

Resources