编辑:下面将只接受个别交易并将其通过电子邮件发送给用户。最好使用用户表单将完整的语句组合在一起,这样您就可以控制要包含的数据。我建议初学者 VBA 课程(检查 w3 学校)以熟悉基础知识,然后可以调整以下内容以完成您正在寻找的内容。
在 Excel 中点击Alt + F11,然后双击此工作表,然后粘贴以下代码:
Sub sendemails()
Application.DisplayAlerts = False
Dim i As Long
i = 7
Dim UserNameCol As String
UserNameCol = "U"
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
Do Until Len(Cells(i, UserNameCol).Value) = 0 'will stop when blank cell appears
'Set up all the variables you need for your emails and fill them
Dim UserName As String
Dim FullName As String
Dim PO As String
Dim SortCode As String
Dim AccountNum As String
Dim Euro As String
Dim Pound As String
Dim ExchRate As String
'i is the counter to move through the list and the username col is U
UserName = Cells(i, UserNameCol).Value
FullName = Cells(i, UserNameCol).Offset(, -19).Value
SortCode = Cells(i, UserNameCol).Offset(, -16).Value
AccountNum = Cells(i, UserNameCol).Offset(, -15).Value
PO = Cells(i, UserNameCol).Offset(, -14).Value
Euro = Cells(i, UserNameCol).Offset(, -13).Value
Exchange = Cells(i, UserNameCol).Offset(, -12).Value
Pound = Cells(i, UserNameCol).Offset(, -11).Value
ExchRate = Cells(i, UserNameCol).Offset(, -10).Value
Set OutMail = OutApp.CreateItem(0) 'this goes here because a new email is needed each time
On Error Resume Next
With OutMail
'Customise the below as required
.To = UserName
.Importance = 1
.Subject = "Hello"
.Body = "Full Name: " & FullName & vbCr & _
"PO: " & PO & vbCr & _
"Sort Code: " & SortCode & vbCr & _
"Account Number: " & AccountNum & vbCr & _
"Amount Euro: " & Euro & vbCr & _
"Amount Pound: " & Pound & vbCr & _
"Exchange Rate: " & ExchRate & vbCr & vbCr & _
"Thanks for reading."
'display shows each email before sending
.Display
'send sends email automatically
'.Send
End With
On Error GoTo 0
i = i + 1
Loop
'destroy outlook when finished processing all mails
Set OutMail = Nothing
Set OutApp = Nothing
Application.DisplayAlerts = True
End Sub
我会在一对保持 .Display 属性集的情况下尝试它,一旦你满意它的工作原理,注释掉 .Display 并取消注释掉 .Send(在开头添加或删除撇号)必要时行)。
同时确保您启用了 Microsoft Outlook 对象库(工具 > 参考 > MS Outlook 对象库)。
希望这会有所帮助。