Excel VBA电子邮件行到单个收件人

时间:2014-06-26 19:03:43

标签: excel vba email excel-vba automation

我有一个跟踪发票的工作表,我正在尝试生成一个自动电子邮件,如果第12列中的单元格包含AUTOEMAIL,它会将所有行与我使用TRIM函数生成的类似电子邮件地址组合在一起。它会将所有类似的行(基于第15列的电子邮件地址)拉入LotusNotes电子邮件中。 Ron De Bruin在他的网站上有一些很棒的例子。我试图编写一个循环,尝试循环并根据电子邮件地址复制所有行。当我去运行时,代码什么都不做,但没有出现错误。在Outlook中有完成此操作的实例,但它们不适用于LotusNotes,因为问题是迟到与早期绑定。我也是VBA自动化的新手。

    Sub Send_Data()
Dim noSession As Object, noDatabase As Object, noDocument As Object
Dim vaRecipient As Variant
Dim rnBody As Range
Dim Data As DataObject

Const stSubject As String = "TEST"
Const stMsg As String = "TEST"
Const stPrompt As String = "Please select the range:"

lastrow = Range("N" & Rows.Count).End(xlUp).row
For Each Cell In Range("N8:N" & lastrow)
If WorksheetFunction.CountIf(Range("N8:N" & Cell.row), Cell) = 1 Then
If Cells(Cell.row, 11) = "AUTOEMAIL" Then

rnBody = "Hello" & vbNewLine & vbNewLine & _
ActiveCell.EntireRow.Select


On Error Resume Next

'The user canceled the operation.
If rnBody Is Nothing Then Exit Sub

On Error GoTo 0

'Instantiate Lotus Notes COM's objects.
Set noSession = CreateObject("Notes.NotesSession")
Set noDatabase = noSession.GETDATABASE("", "")

'Make sure Lotus Notes is open and available.
If noDatabase.IsOpen = False Then noDatabase.OPENMAIL

'Create the document for the e-mail.
Set noDocument = noDatabase.CreateDocument

 'Copy the selected range into memory.
 rnBody.Copy

 'Retrieve the data from then copied range.
 Set Data = New DataObject
 Data.GetFromClipboard

 'Add data to the mainproperties of the e-mail's document.
 With noDocument
   .Form = "Memo"
   .SendTo = vaRecipient
   .Subject = stSubject
   'Retrieve the data from the clipboard.
   .Body = stMsg & " " & Data.GetText
    .SaveMessageOnSend = True
 End With

 ' SEND EMAIL
 With noDocument
   .PostedDate = Now()
   .Send 0, vaRecipient
End With

' REMOVE FROM MEMORY
Set noDocument = Nothing
Set noDatabase = Nothing
Set noSession = Nothing


'SWITCH BACK TO EXCEL
AppActivate "Microsoft Excel"

'EMPTY COPY-PAST CLIPBOARD
Application.CutCopyMode = False
' DISPLAYS TO USER IF SUCCESSFUL
MsgBox "Complete!", vbInformation
  End If
 End If
Next Cell
End Sub

1 个答案:

答案 0 :(得分:0)

我将电子邮件正文范围设置为提示框,用户可以在其中突出显示单元格,然后将另一个提示框设置为使用TRIM()函数创建的电子邮件。我意识到代码的设置方式不允许我想做的事情。新方法效果很好

Treevar

相关问题