'VBA 使用 CDO 寄送郵件
'使用CDO寄送郵件
'不必透過Outlook寄送
'必須設定引用項目「Microsoft CDO for Windows 2000 Library」
Option Explicit
'宣告變數及參數
Dim objCDO As CDO.Message
Public theRng As Range
Const myUserId = "yourID" '請修改成你的帳號
Const myPassword = "yourPSW" '請修改成你的帳號密碼
Const mySMTPServer = "smtp.gmail.com" '請修改成你的寄送郵件伺服器
Const mySMTPport = 465 '須修改成你的送件伺服器使用的PORT, Gmail使用的是465
'流程控制
Sub Main()
Application.StatusBar = "Sending..." '狀態訊息顯示
Set objCDO = New CDO.Message '創建新的物件
ServerSetting '伺服器設置
Set theRng = ActiveSheet.UsedRange '取的作用工作表使用範圍
fillNsend '填充郵件各欄位資訊並寄送
Set objCDO = Nothing '釋放物件
Application.StatusBar = False '恢復狀態訊息
End Sub
'設置郵件伺服器參數
'這是用Gmail測試成功的參數, 請參閱 Email Server 的設置說明
Sub ServerSetting()
With objCDO
With .Configuration.Fields
.Item(cdoSendUsingMethod) = cdoSendUsingPort
.Item(cdoSendUserName) = myUserId
.Item(cdoSendPassword) = myPassword
.Item(cdoSMTPServer) = mySMTPServer
.Item(cdoSMTPAuthenticate) = True
.Item(cdoSMTPServerPort) = mySMTPport
.Item(cdoSMTPUseSSL) = True
.Update
End With
End With
End Sub
'填充欄位資訊後寄送
Sub fillNsend()
With objCDO
.From = """crdotlin""<" & myUserId & "@" & VBA.Mid(mySMTPServer, 6) & ">"
.To = ActiveSheet.Range("a2").Text
.Subject = "Test Test"
.HTMLBody = RangetoHTML(theRng)
'設定重要性
'.Fields("urn:schemas:mailheader:importance") = 2
'.Fields("urn:schemas:httpmail:importance") = 2
.Fields(cdoImportance) = cdoHigh
.Fields.Update
.Send
End With
End Sub
'測試
Sub TestSendSheet()
Dim Sht As Worksheet
'處理所有工作表
For Each Sht In ThisWorkbook.Worksheets
Sht.Activate '啟動為作用工作表
'如果A2儲存格式郵件位址, 則將此工作表寄送到該信箱
If ActiveSheet.Range("a2").Value Like "*@*" Then Main
Next
End Sub
'引用 Ron de Bruin 的 RangetoHTML 程序
'Reference: http://www.rondebruin.nl/cdo.htm
Function RangetoHTML(rng As Range)
' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-2007
Dim fso As Object
Dim ts As Object
Dim TempFile As String
Dim TempWB As Workbook
'Copy the range and create a new workbook to past the data in
rng.Copy
Set TempWB = Workbooks.Add(1)
With TempWB.Sheets(1)
.Cells(1).PasteSpecial Paste:=8
.Cells(1).PasteSpecial xlPasteValues, , False, False
.Cells(1).PasteSpecial xlPasteFormats, , False, False
.Cells(1).Select
Application.CutCopyMode = False
On Error Resume Next
.DrawingObjects.Visible = True
.DrawingObjects.Delete
On Error GoTo 0
End With
'Publish the sheet to a htm file
With TempWB.PublishObjects.Add( _
SourceType:=xlSourceRange, _
Filename:=TempFile, _
Sheet:=TempWB.Sheets(1).Name, _
Source:=TempWB.Sheets(1).UsedRange.Address, _
HtmlType:=xlHtmlStatic)
.Publish (True)
End With
'Read all data from the htm file into RangetoHTML
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
RangetoHTML = ts.readall
ts.Close
RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
"align=left x:publishsource=")
'Close TempWB
TempWB.Close savechanges:=False
'Delete the htm file we used in this function
Kill TempFile
Set ts = Nothing
Set fso = Nothing
Set TempWB = Nothing
End Function作者: takeshilin88 時間: 2016-4-1 08:35