2014-06-26 59 views
0

我有一个跟踪发票的工作表,我试图生成一个自动电子邮件发送程序,如果第12栏中的单元格包含AUTOEMAIL,它会将所有行与一个类似的电子邮件地址我已经使用TRIM函数生成了。它会将所有类似的行(基于列15的电子邮件地址)拉入LotusNotes电子邮件中。 Ron De Bruin在他的网站上有一些很棒的例子。我试图编写一个循环,尝试循环并复制基于电子邮件地址的所有行。当我开始运行时,代码不会执行任何操作,但不会显示错误。在Outlook中有这种情况在线完成,但它们不适用于LotusNotes,因为问题比较早,与早期绑定相关。我也更新VBA自动化。Excel 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 
+0

你有什么试图缩小这个问题?你可以调试vba,看看它是否达到灵敏线 - 或者尝试更简单的版本 - 即发送电子邮件,但没有合并电子邮件等的代码,只需发送到硬编码的电子邮件或从Excel工作表中的单个单元格。 – phaedra

+0

Ron de Bruins发送未格式化的数据与我的工作表正常工作,但依赖于VBA阵列的电子邮件地址。我意识到上面我没有设置vaRecipient,但我手动将其设置为我的电子邮件地址,我仍然无法看到任何输出:( – Treevar

回答

0

我设定的电子邮件正文范围的提示框,用户可以突出细胞,然后另一个提示对话框,在其中它要求指使用TRIM()函数创建的电子邮件。我意识到代码设置的方式不会允许我想要做的事情。新方法效果很好

树木

相关问题