|
本帖最后由 sbww8800 于 2015-7-6 16:09 编辑
- 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
- Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
-
- 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
- '使用Alt+S发送邮件,这是本文的关键之处,免安全提示自动发送邮件全靠它了
- Application.SendKeys "%s"
- End Function
-
- '批量发送邮件
- Sub BatchSendMail()
- Application.ScreenUpdating = False
- Sheet7.Activate
- [d1] = "D:\数据备份\入库资料" & Format(Now, "yyyymmddhhmm") & ".xlsx;D:\数据备份\统计表" & Format(Now, "yyyymmddhhmm") & ".xlsx"
- Dim rowCount, endRowNo
- endRowNo = Cells(1, 1).CurrentRegion.Rows.Count
- '逐行发送邮件
- For rowCount = 1 To endRowNo
- SendMail Cells(rowCount, 1), Cells(rowCount, 2), Cells(rowCount, 3), Cells(rowCount, 4)
- Next
- Sheet1.Activate
- Application.ScreenUpdating = True
- End Sub
- ' 发送多个附件的子程序
- Sub SendMail(ByVal to_who As String, ByVal subject As String, ByVal body As String, ByVal attachement As String)
- Dim objOL As Object
- Dim itmNewMail As Object
- Dim attaches
- Dim attach
-
- '引用Microsoft Outlook 对象
- Set objOL = CreateObject("Outlook.Application")
- Set itmNewMail = objOL.CreateItem(olMailItem)
- With itmNewMail
- .subject = subject '主旨
- .HTMLbody = body '正文本文
- .To = to_who '收件者
- .Display '启动Outlook发送窗口
- attaches = Split(attachement, ";")
-
- For Each attach In attaches
- If (Len(attach) > 0) Then
- .Attachments.Add attach
- End If
- Next
- SetTimer 0, 0, 0, AddressOf WinProcA
- End With
-
- Set objOL = Nothing
- Set itmNewMail = Nothing
- End Sub
复制代码
找几天了,一直没解决,要自动发送邮件,上面代码要先打开outlook才能发送,没打开就一直躺在发件箱里,直到打开outlook才发送,这能解决吗?望大侠出手
|
|