【发布时间】:2017-11-01 07:02:56
【问题描述】:
我有一个 Excel VBA (Send_Mail) 通过 Lotus Notes 发送电子邮件。它工作正常,但是我需要帮助才能一次性将个人电子邮件发送给多个人。
在我的 Excel 表格中。向下的单元格 A7 将是最多可以超过 200 行的电子邮件地址,B7 具有主题行,单元格 C7 具有电子邮件正文。 (所有这些都使用不同的宏自动填充)。但是,我的代码(Send_Mail)只是向单元格 A7 中的地址发送一封电子邮件。我需要您的帮助,以便将邮件发送到 Col A7 及以后的所有电子邮件地址,并附上其各自的主题 (Col B) 和邮件正文 (col C)
下面是我的代码。
Public TOID As String
Public CCID As String
Public SECT As String
Public ACCO As String
Public SUBJ As String
Sub Send_Mail()
Dim answer As Integer
answer = MsgBox("DO YOU HAVE LOTUS NOTES OPEN ?? Not WebLotus notes", vbYesNo + vbQuestion, "LOTUS NOTES")
If answer = vbNo Then
MsgBox "Please Open Notes and Try the Macro Again"
Exit Sub
Else
End If
Application.DisplayAlerts = False
Call Send
MsgBox "Mail Sent to " & (Range("L2").Value) & " " & "Recipents"
Application.DisplayAlerts = True
End Sub
Public Function Send()
SendEMail = True
Sheets("Main").Select
TOID = Range("A7").Value
CCID = ""
SUBJ = Range("B7").Value
'On Error GoTo ErrorMsg
Dim EmailList As Variant
Dim ws, uidoc, Session, db, uidb, NotesAttach, NotesDoc, objShell As Object
Dim RichTextBody, RichTextAttachment As Object
Dim server, mailfile, user, usersig As String
Dim SubjectTxt, MsgTxt As String
Set Session = CreateObject("Notes.NotesSession")
user = Session.UserName
usersig = Session.COMMONUSERNAME
mailfile = Session.GETENVIRONMENTSTRING("MailFile", True)
server = Session.GETENVIRONMENTSTRING("MailServer", True)
Set db = Session.GETDATABASE(server, mailfile)
If Not db.IsOpen Then
Call db.Open("", "")
Exit Function
End If
Set NotesDoc = db.CREATEDOCUMENT
With NotesDoc
.Form = "Memo"
.Subject = SUBJ 'The subject line in the email
.Principal = user
.sendto = TOID 'e-mail ID variable to identify whom email need to be sent
.CopyTo = CCID
End With
Set RichTextBody = NotesDoc.CREATERICHTEXTITEM("Body")
With NotesDoc
.COMPUTEWITHFORM False, False
End With
'==Now set the front end stuff
Set ws = CreateObject("Notes.NotesUIWorkspace")
If Not ws Is Nothing Then
Set uidoc = ws.EDITDOCUMENT(True, NotesDoc)
If Not uidoc Is Nothing Then
If uidoc.EDITMODE Then
'Mail Body
Sheets("Main").Select
Range("C7").Select
Dim rnBody1 As Range
Set rnBody1 = Selection
rnBody1.CopyPicture
'rnBody1.Copy
Call uidoc.GOTOFIELD("Body")
Call uidoc.Paste
End If
End If
End If
Call uidoc.Send
Call uidoc.Close
'close connection to free memory
Set Session = Nothing
Set db = Nothing
Set NotesAttach = Nothing
Set NotesDoc = Nothing
Set uidoc = Nothing
Set ws = Nothing
Sheets("Main").Select
End Function
【问题讨论】:
-
我认为您的发送函数可能是您使用参数调用的过程。这些参数可以是您在 A7 向下范围的循环中分配的变量,例如一个参数是将 Range("A"& currentRow) 传递给子程序,并在子程序内将其分配给 TOID、相同的 B 列和 C 列值(您将为当前行传递这些值。)我认为这将是一个缓慢的过程。
-
去掉你正在使用的 .Select。见Avoiding .Select。使用选项显式并检查所有变量是否已声明并正确键入,例如将 SendEmail 调暗为布尔值。您有很多变体,例如Dim ws,.... NotesAttach, NotesDoc, objShell 作为对象,只有最后一个是对象。您还有一个空的 Else 子句。我不认为在 VBA 中你需要在函数结束时进行垃圾收集,尽管其他人可能不同意。
-
嗨 QHarr,我对这些代码很陌生,不太了解。你能帮我清理这段代码以及循环选项吗?