|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
本帖最后由 johnwalk 于 2015-10-31 12:07 编辑
邮件批量发送
http://club.excelhome.net/thread-781973-1-1.html
(出处: ExcelHome技术论坛)
非常棒的一个东西,还无私的分享出源码
里面东西很有用 自己也正好在学习vba
想就学习notre的东西,分享一点自己的学习体会
- Dim myolapp As Object
- Sub Send_Mails()
- 'On Error Resume Next
- Dim subject As String
- Dim i As Integer
- Dim l As Integer
- Dim wst As Worksheet
- Dim myitem
- Dim y As Long
- Dim z As String
- Set wst = ThisWorkbook.ActiveSheet
- subject = wst.Range("Subject").Value
- Set myolapp = CreateObject("outlook.application")
- i = wst.Range("Receiver").End(xlDown).Row - wst.Range("Receiver").Row
- z = Len(wst.Range("Att").Offset(l, 0).Value)
-
- For l = 1 To i
- Set myitem = myolapp.CreateItem(olMailItem)
- With myitem
- .subject = subject
- .To = wst.Range("Receiver").Offset(l, 0)
- .cc = wst.Range("CCer").Offset(l, 0)
- .BodyFormat = 3
- .Body = MailBody(l, wst)
- '.display
-
- y = 1
- Do
- x = InStr(y, wst.Range("Att").Offset(l, 0).Value, ";")
- If x = 0 And y <> 1 Then
- z = Mid(wst.Range("Att").Offset(l, 0).Value, y, Len(wst.Range("Att").Offset(l, 0).Value) - y + 1)
- ElseIf x = 0 And y = 1 Then
- z = wst.Range("Att").Offset(l, 0).Value
- Else
- z = Mid(wst.Range("Att").Offset(l, 0).Value, y, x - y)
- End If
- y = x + 1
- .Attachments.Add z
- Loop Until x = 0
-
- .Send
- End With
- Next
-
- 'myolapp.Quit
- Set myolapp = Nothing
- Set myitem = Nothing
-
- End Sub
- Function MailBody(l As Integer, wst As Worksheet)
- 'On Error Resume Next
-
- Dim wapp As Object
- Dim wb As Object
- Dim k As Integer
- Dim j As Integer
-
- Set wapp = CreateObject("word.application")
- Set wb = wapp.Documents.Open(wst.Range("Content").Value)
- k = wst.Range("replace").End(xlToRight).Column - wst.Range("replace").Column
-
- For j = 0 To k
- wb.content.Find.Execute findText:=wst.Range("replace").Offset(0, j), Replacewith:=wst.Range("replace").Offset(l, j), Replace:=2
- Next
- MailBody = wb.content.Text
-
- wb.Close SaveChanges:=False
- wapp.Quit
-
- Set wb = Nothing
- Set wapp = Nothing
- End Function
复制代码
|
|