创建与IBM Notes的Excel邮件合并

时间:2016-06-27 14:33:38

标签: excel vba lotus-notes mailmerge

早上好,

我有一些代码会根据excel中的列在IBM Notes中创建一个电子邮件mailmerge。我遇到的麻烦只是我想为A列中的每个电子邮件地址创建单独的电子邮件。我也无法弄清楚如何为每个收件人附加特定文件,文件的位置将在C列中。谁能帮我吗?

Sub SendQuoteToEmail()

 Dim NSession As Object
 Dim NDatabase As Object
 Dim NUIWorkSpace As Object
 Dim NDoc As Object
 Dim NUIdoc As Object
 Dim WordApp As Object
 Dim subject As String
 Dim EmailAddress As Variant

 subject = Worksheets("Sheet1").Range("B2")
 Debug.Print subject

Set NSession = CreateObject("Notes.NotesSession")
Set NUIWorkSpace = CreateObject("Notes.NotesUIWorkspace")
Set NDatabase = NSession.GetDatabase("", "")
If Not NDatabase.IsOpen Then NDatabase.OPENMAIL

 For all Emai-Addresses
EmailAddress =                        Worksheets("Sheet1").Application.Transpose(Range("A2").Resize(Range("A" &                           Rows.Count).End(xlUp).Row).Value)
Set NDoc = NDatabase.CreateDocument

With NDoc
    .Form = "Memo" ' or .ReplaceItemValue("Form", "Memo")
    .SendTo = EmailAddress
    .subject = subject
End With
Set rtitem = NDoc.CreateRichTextItem("Body")
Call rt.AppendText(Worksheets("sheet1").Range("d2") & vbLf & vbLf)
Call rt.EmbedObject(1454, "", "c:filepath.doc")
Call NDoc.Save(True, False)
Next ' email-address
Set NDoc = NDatabase.CreateDocument

 With NDoc
.SendTo = EmailAddress
'.CopyTo = ""
.subject = subject

.body = Worksheets("sheet1").Range("d2") & vbLf & vbLf


.Save True, False
 End With

Set NUIdoc = NUIWorkSpace.EDITDocument(True, NDoc)
With NUIdoc

Set WordApp = Nothing
End With

Set NSession = Nothing
End Sub

2 个答案:

答案 0 :(得分:0)

要附加文件,请查看NotesRichTextItem类的EmbedObject方法。

您的代码还存在其他一些问题。 为字段赋值时,不应使用扩展表示法,使用NotesDocument类的ReplaceItemValue方法存储值。 Body 字段是一个富文本字段,您应该将NotesRichTextItem类用于与该字段相关的任何内容,因此使用Appendtext和AddNewLine方法将内容写入字段。这也允许你使用我上面提到的EmbeddObject方法来附加文件。

您可以在我的邮件通知类中找到一些有用的提示。我刚发布了一个更新版本 http://blog.texasswede.com/updated-mailnotification-class-now-with-html-email-support-and-web-links/

希望它有所帮助!

答案 1 :(得分:0)

这似乎是在Notes中创建邮件的代码的更相关部分:send email to a specific Lotus Notes contacts using VBA

您希望在发送邮件之前在屏幕上打开邮件,还是可以看不到它们?

更新:建议代码

subject = Worksheets("Sheet1").Range("B2")
Debug.Print subject

Set NSession = CreateObject("Notes.NotesSession")
Set NUIWorkSpace = CreateObject("Notes.NotesUIWorkspace")
Set NDatabase = NSession.GetDatabase("", "")
If Not NDatabase.IsOpen Then NDatabase.OPENMAIL

For all email-addresses
    EmailAddress = Worksheets("Sheet1").Application.Transpose(Range("A2").Resize(Range("A" & Rows.Count).End(xlUp).Row).Value)
    Set NDoc = NDatabase.CreateDocument

    With NDoc
        .Form= "Memo" ' or .ReplaceItemValue("Form", "Memo")
        .SendTo = EmailAddress
        .subject = subject
    End With
    set rtitem= NDoc.CreateRichTextItem("Body")
    Call rt.AppendText(Worksheets("sheet1").Range("d2") & vbLf & vbLf)
    Call rt.EmbedObject(1454, "", "c:filepath.doc")
    Call NDoc.Save(True, False)
Next ' email-address

待处理:循环,文件名,地址......