[英]VBA help for auto email based on column values with confirmation
我想知道是否有人可以幫助我進行 VBA 編碼,我相對較新。 我之前已經設法設置了 VBA 自動電子郵件和自動過濾器,但這比我以前做過的任何事情都要復雜得多!
我想向第 3 行 B:D 列(其電子郵件地址在第 2 行)中的每個人發送一封電子郵件:當行 = 今天的日期(在 B2 中)和該行中每列的值時(B:D) >0。 如果 =0 或空白則忽略移至下一列。 電子郵件正文中也應使用數字,例如,如果有 2,則:
“嗨,倫敦,
您的表現非常好,將獲得 2 份禮物!
問候,
聖誕老人”
一旦遍歷所有列 (B:D) 和發送的電子郵件,然后在 E 列中鍵入“Y”以向其他文檔查看者說明已發送確認電子郵件。 請參閱布局示例:示例布局。 A 列中不會有重復的日期。
非常感謝任何人可以提供的任何幫助或指導!
歡迎來到堆棧溢出! 這不是一個完整的代碼解決方案,但有一些提示:
For Each
循環或類似的循環,谷歌'vba loops'來看看我在說什么。 循環是一段運行多次的代碼,每次只更改一個變量(在您的情況下,變量將是正在查看的列,循環應該運行 3 次)。 循環可防止不必要的代碼重復,使以后更容易閱讀和編輯代碼。Date
。Dim strBody As String
,然后在每個循環中創建或構建此字符串,例如:strBody = "Hello" & vbcrlf & "You've performed really well and will get " & LNumberVariable & " presents"
Sub SendEmail(Optional ToAddresses As String, Optional CcAddresses As String, _
Optional BccAddresses As String, Optional Subject As String, _
Optional Body As String, Optional AttachFiles As Variant = False, Optional AutoSend As Boolean = False, _
Optional SendFromAddress As String)
'Adapted from https://www.rondebruin.nl/win/s1/outlook/bmail4.htm
Dim OutApp As Object
Dim OutMail As Object
'Current application, where applicable;
On Error GoTo Err
Set OutApp = GetObject(, "Outlook.Application")
NoErr:
On Error GoTo 0
Set OutMail = OutApp.CreateItem(0)
With OutMail
'Basic Text properties
.To = ToAddresses
.CC = CcAddresses
.Bcc = BccAddresses
.Subject = Subject
'---------------------
'Body; HTML or plain text
If Body Like "*<br>*" Then
.HtmlBody = Body
Else
.Body = Body
End If
'---------------------
'Attachments:
If Not AttachFiles = False Then
If IsArray(AttachFiles) Then
For x = LBound(AttachFiles) To UBound(AttachFiles)
.Attachments.Add (AttachFiles(x))
Next
Else
.Attachments.Add (AttachFiles)
End If
End If
'---------------------
'Sender Address
If Len(SendFromAddress) > 0 Then
For a = 1 To OutApp.Session.accounts.Count
If LCase(OutApp.Session.accounts.Item(a)) Like LCase(SendFromAddress) Then
.sendusingaccount = OutApp.Session.accounts.Item(a)
SendFromAddress = ""
Exit For
End If
Next
If Len(SendFromAddress) > 0 Then .SentOnBehalfOfName = SendFromAddress
End If
'---------------------
'Send or display:
If AutoSend = True Then
.Send
Else
.Display
End If
'---------------------
End With
Set OutMail = Nothing
Set OutApp = Nothing
Exit Sub
Err:
If Err.Number = 429 Then
Set OutApp = CreateObject("Outlook.application")
GoTo NoErr
End If
On Error GoTo 0
Err.Raise (Err.Number)
End Sub
如果以上內容在您的項目中,您可以這樣稱呼它
Call SendEmail(ToAddresses:="me@example.com;you@example.com", Subject:="my Test email", Body:=myStringBodyVariable, AutoSend:=True)
或者
Call SendEmail("me@example.com", , , "Subject line here", "Body text would appear here", , False)
所有參數都是可選的,因此您可以只包含您需要/喜歡的參數。 從 Excel 運行,它需要您設置對 Outlook 庫的引用,以便它可以在 Excel 應用程序中運行 Outlook 代碼。
我知道要閱讀的內容很多,但我希望這些提示對您有所幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.