我正在创建一个 Excel 用户表单,它可以为多达 500 个收件人创建单独的电子邮件。
工作表上有 4 列:名称(A 列)、电子邮件(B 列)、值 1(C 列)。
该代码使用 For 和 Next 循环样式,其中 r 被声明为整数 (r = 2 To 500) 和 MailItem 对象,.To =Cells(r,2)。
我遇到的问题是尝试合并范围(C 列)中的值,以替换用于创建电子邮件正文的文本框中的特殊字符。
因此,如果我输入Hello, There were ^&^ transactions that failed yesterday.
并点击用于“发送”的命令按钮,它将向 B 列中列出的每个电子邮件地址发送一封电子邮件,并用^&^
C 列中的值替换每个单独的电子邮件地址(每一行)。
下面以 VBA 代码为例。当然,还有很多其他声明的变量我没有提到,以使这个查询尽可能简短。
Dim Signature As String, EmailSensitivity As String, EmailImportance As String
Dim Greeting As String, Punctuation As String, Security As String
Sub SendButton1_Click()
If SubjectText = vbNullString Then
If EmailBody1 = vbNullString Then
MsgBox "Form Incomplete:" & vbCrLf & vbCrLf & _
"No Subject or Email Text.", vbOKOnly
Exit Sub
End If
End If
If SubjectText = vbNullString Then
MsgBox "Form Incomplete:" & vbCrLf & vbCrLf & _
"Please enter Subject.", vbOKOnly
Exit Sub
End If
If EmailBody1 = vbNullString Then
MsgBox "Form Incomplete:" & vbCrLf & vbCrLf & _
"Please enter Email Text."
Exit Sub
End If
If SignatureText1 = vbNullString Then
Continue = MsgBox("Your email contains no signature." & vbCrLf & vbCrLf & _
"Are you sure you wish to proceed?", vbYesNo)
If Continue = vbNo Then
Exit Sub
End If
End If
Dim OutApp As Object, OutMail As Object
Dim r As Integer
Application.ScreenUpdating = False
For r = 2 To 501
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(olMailItem)
On Error Resume Next
With OutMail
.Subject = Security & SubjectText.Value
.Body = EmailBody1.Value & vbCrLf & vbCrLf & _
SignatureText1.Value
.To = Cells(r, 2)
.Attachements.Add AttachmentText1.Value
.Importance = EmailImportance
.Sensitivity = EmailSensitivity
.Send
End With
Next r
Set OutApp = Nothing
Application.ScreenUpdating = True
Sheet1.Range("A2:B501").Clear
Continue = MsgBox("You have successfully generated a mass email!" & vbCrLf & vbCrLf & _
"Would you like to generate another email?", vbYesNo)
If Continue = vbNo Then
Application.Quit
End If
End Sub
我还是一个业余爱好者,所以我正在清理很多不必要的代码,但是这个查询主要是用每一行列出的值替换特殊字符。
这是我第一次在论坛上实际发布查询,所以如果我没有遵循正确的程序,请告诉我,非常感谢您的帮助。