From: http://blog.csdn.net/chenxiangping/article/details/70821116
1, . set up Outlook email account (not the focus of this article)
2, . set up Outlook Trust Center as follows
If the following operation settings are not made, the prompt is as follows when Excel VBA calls Outlook to automatically send mail
2.1 Outlook - > tools - > Trust Center
2.2 programming access - > select "never send me suspicious activity warning (not recommended)", note: it is recommended to use Excel VBA to automatically send mail to enable this function
3, Enable Excel Macro
3.1 enable macro operations as follows:
Open Excel and click Office - > Excel, as shown below
Select "Trust Center" - > trust center setting on the left side of the "Excel option" form, as shown below:
In the "Trust Center" form - > macro settings, select the following two options
Then close Excel and open again to enable macro and VBA programming development.
4, Excel VBA development
4.1 create module class: clsModel, write the following code:
- Public Declare Function SetTimer Lib "user32" _
- (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerfunc As Long) As Long
- Public Declare Function KillTimer Lib "user32" _
- (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
- Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
- Sub AutoMail()
- GB_EMPSALARY.Show
- End Sub
- 'Function WinProcA(ByVal hwnd As Long, ByVal uMsg As Long, ByVal idEvent As Long, ByVal SysTime As Long) As Long
- ' KillTimer 0, idEvent
- ' DoEvents
- ' Sleep 100
- ' 'Use Alt+S Sending email is the key point of this article. It is the only way to automatically send email without security prompt
- ' Application.SendKeys "%s"
- 'End Function
- ' Subprogram to send a single message
- Sub SendMail(ByVal to_who As String, ByVal SubJect As String, ByVal body As String, ByVal cell As String)
- Dim objOL As Object
- Dim itmNewMail As Object
- 'Quote Microsoft Outlook object
- Set objOL = CreateObject("Outlook.Application")
- Set itmNewMail = objOL.CreateItem(olMailItem)
- On Error GoTo Err_Handle
- With itmNewMail
- .SubJect = SubJect 'A purpose
- .htmlBody = body 'Text text
- '.body = body 'Text text
- .To = to_who 'Recipient
- '.Attachments.Add attachement 'Attachments. If you don't need to send attachments, you can delete this sentence, Excel The fourth column in cannot be deleted
- .Display 'start-up Outlook Send window
- 'SetTimer 0, 0, 0, AddressOf WinProcA
- .Send
- 'Application.Wait (Now + TimeValue("0:00:03"))
- 'Application.SendKeys "%s"
- End With
- Worksheets("Sheet1").Range(cell).Value = "Y"
- Set objOL = Nothing
- Set itmNewMail = Nothing
- Err_Handle:
- Set objOL = Nothing
- Set itmNewMail = Nothing
- On Error Resume Next
- End Sub
4.2 create an automatic email sending interface, so that users can see which line of Excel table to operate.
The main codes are as follows:
- Private Sub butSend_Click()
- On Error Resume Next
- Dim i As Integer
- Dim EmpName, eMail, mailSubJect, mailBody, cell, sendFlag As String
- i = CInt(txtStartRow.Text)
- If (i < 3) Then
- i = 3
- End If
- 'Mailbox subject
- mailSubJect = "A company" & Worksheets("Sheet1").Range("C1").Value & "Payslip"
- 'Employee name
- EmpName = Worksheets("Sheet1").Range("E" & i).Value
- 'Employee name is empty exit stop sending mail
- Do While EmpName <> ""
- 'Send mail flag
- sendFlag = Worksheets("Sheet1").Range("A" & i).Value
- 'e-mail address
- eMail = Worksheets("Sheet1").Range("AH" & i).Value
- 'Whether the email is sent and closed, and whether the email address is empty
- If (sendFlag <> "Y" And eMail <> "") Then
- 'Mailbox content
- mailBody = SalaryContext(EmpName, i)
- 'Send flag cell or not
- cell = "A" & i
- SendMail eMail, mailSubJect, mailBody, cell
- End If
- i = i + 1
- 'Get employee name on next line
- EmpName = Worksheets("Sheet1").Range("E" & i).Value
- DoEvents
- Sleep 300
- txtSend.Text = i
- Loop
- End Sub
- 'Payslip table details
- Function SalaryContext(ByVal EmpName As String, ByVal Row As Integer) As String
- Dim htmlBody, tableHeader, tableBody As String
- htmlBody = "<html>" & _
- "<head>" & _
- "<meta http-equiv=""Content-Type"" contentType=""application/vnd.ms-excel;charset=gb2312"">" & _
- " <STYLE type=text/css>" & _
- " .sub_title{" & _
- " FONT-WEIGHT: bold;" & _
- " FONT-SIZE: 4mm;" & _
- " VERTICAL-ALIGN: middle;" & _
- " TEXT-ALIGN: center" & _
- " background-color: #ffff66//" & _
- " }"
- htmlBody = htmlBody & " .context {" & _
- " font-size: 12px;" & _
- " BORDER-TOP-WIDTH: 0.6mm;" & _
- " PADDING-RIGHT: 1mm;" & _
- " PADDING-LEFT: 1mm;" & _
- " BORDER-LEFT-WIDTH: 0.6mm;" & _
- " BORDER-BOTTOM-WIDTH: 0.6mm;" & _
- " PADDING-BOTTOM: 0mm;" & _
- " PADDING-TOP: 0mm;" & _
- " BORDER-COLLAPSE: collapse;" & _
- " BORDER-RIGHT-WIDTH: 0.6mm" & _
- " }"
- htmlBody = htmlBody & " .context td{" & _
- " border:1px solid #009900;" & _
- " }" & _
- " .page {" & _
- " page-break-after: always;" & _
- " }" & _
- " </STYLE>" & _
- "</head><body>Dear " & EmpName & Chr(13)
- htmlBody = htmlBody & "<table class=""context"" borderColor=""#669933"" border=1>"
- 'MsgBox htmlBody
- 'Header
- tableHeader = "<tr bgcolor=""#FFE66F""><td align=""center"">Fixer<br>Benchmark</td>" & _
- "<td align=""center"">Floating performance<br>Benchmark</td><td align=""center"">Should be diligent<br>Hours</td>" & _
- "<td align=""right"">Actual<br>Attendance</td><td align=""center"">section<br>holiday</td><td align=""center"">Assessment<br>coefficient</td>" & _
- "<td align=""center"">fixed<br>wages</td><td align=""center"">Float<br>Achievements</td><td align=""center"">stay outside over-night<br>subsidy</td>" & _
- "<td align=""right"">food&subsidy</td><td align=""center"">bonus</td><td align=""center"">Royalty</td>" & _
- "<td align=""right"">subsidy</td><td align=""center"">Replacement</td><td align=""center"">Other<br>subsidy</td>" & _
- "<td align=""right"">Should be issued<br>Total</td><td align=""center"">Late</td><td align=""center"">food</td>" & _
- "<td align=""right"">social security</td><td align=""center"">common<br>MPF</td><td align=""center"">rent</td>" & _
- "<td align=""right"">hydropower</td><td align=""center"">personal income tax</td><td align=""center"">Telephone bill</td>" & _
- "<td align=""right"">Withholding tuition</td><td align=""center"">Other</td><td align=""center"">Withhold<br>Total</td>" & _
- "<td align=""right"">Paid wages</td></tr>"
- 'MsgBox Worksheets("Sheet1").Range("F" & i).Value
- 'Table content
- tableBody = "<tr>" & _
- "<td>" & Worksheets("Sheet1").Range("F" & Row).Value & "</td>" & _
- "<td>" & Worksheets("Sheet1").Range("G" & Row).Value & "</td>" & _
- "<td>" & Worksheets("Sheet1").Range("H" & Row).Value & "</td>" & _
- "<td>" & Worksheets("Sheet1").Range("I" & Row).Value & "</td>" & _
- "<td>" & Worksheets("Sheet1").Range("J" & Row).Value & "</td>" & _
- "<td>" & Worksheets("Sheet1").Range("K" & Row).Value & "</td>" & _
- "<td>" & Worksheets("Sheet1").Range("L" & Row).Value & "</td>" & _
- "<td>" & Worksheets("Sheet1").Range("M" & Row).Value & "</td>" & _
- "<td>" & Worksheets("Sheet1").Range("N" & Row).Value & "</td>" & _
- "<td>" & Worksheets("Sheet1").Range("O" & Row).Value & "</td>" & _
- "<td>" & Worksheets("Sheet1").Range("P" & Row).Value & "</td>" & _
- "<td>" & Worksheets("Sheet1").Range("Q" & Row).Value & "</td>" & _
- "<td>" & Worksheets("Sheet1").Range("R" & Row).Value & "</td>" & _
- "<td>" & Worksheets("Sheet1").Range("S" & Row).Value & "</td>" & _
- "<td>" & Worksheets("Sheet1").Range("T" & Row).Value & "</td>" & _
- "<td>" & Worksheets("Sheet1").Range("U" & Row).Value & "</td>" & _
- "<td>" & Worksheets("Sheet1").Range("V" & Row).Value & "</td>" & _
- "<td>" & Worksheets("Sheet1").Range("W" & Row).Value & "</td>" & _
- "<td>" & Worksheets("Sheet1").Range("X" & Row).Value & "</td>" & _
- "<td>" & Worksheets("Sheet1").Range("Y" & Row).Value & "</td>" & _
- "<td>" & Worksheets("Sheet1").Range("Z" & Row).Value & "</td>" & _
- "<td>" & Worksheets("Sheet1").Range("AA" & Row).Value & "</td>" & _
- "<td>" & Worksheets("Sheet1").Range("AB" & Row).Value & "</td>" & _
- "<td>" & Worksheets("Sheet1").Range("AC" & Row).Value & "</td>"
- tableBody = tableBody & "<td>" & Worksheets("Sheet1").Range("AD" & Row).Value & "</td>" & _
- "<td>" & Worksheets("Sheet1").Range("AE" & Row).Value & "</td>" & _
- "<td>" & Worksheets("Sheet1").Range("AF" & Row).Value & "</td>" & _
- "<td>" & Worksheets("Sheet1").Range("AG" & Row).Value & "</td>" & _
- "</tr>"
- 'MsgBox tableBody
- htmlBody = htmlBody & tableHeader & tableBody & "</table></body></html>"
- SalaryContext = htmlBody
- End Function
The contents of the Excel table are as follows