VB宏发送邮件无需打开Lotus Notes

问题描述

我想在不打开 Lotus Notes UI 的情况下发送带有 vbscript 的邮件。我设法编写了一个脚本,允许发送带有附件的电子邮件,并且一切正常。但是,当我运行宏时会启动应用程序,我希望它安静地工作(不启动应用程序)。

这是我的代码

findAllSeq2n <- function(n){
  f <- function(d,out,start,end){
    if (abs(d) > (end - start + 1) / 2) return(NULL)
    if (start > end) { 
      if (d == 0) {
        #cat(out,"\n")
        return(out)
      }
    }

    out[start] <- 0L
    out[end] <- 1L
    s01 <- f(d + 1L,start + 1L,end - 1L)
    
    out[start] <- 1L
    out[end] <- 1L
    s11 <- f(d,end - 1L)
    
    out[start] <- 0L
    out[end] <- 0L
    s00 <- f(d,end - 1L)
    
    out[start] <- 1L
    out[end] <- 0L
    s10 <- f(d - 1L,end - 1L)

    do.call(cbind,list(s01,s11,s00,s10))
  }
  out <- integer(2*n)
  f(0,1,2*n)
}

findAllSeq2n(2)
#     [,1] [,2] [,3] [,4] [,5] [,6]
#[1,]    0    1    1    0    0    1
#[2,]    1    1    0    1    0    0
#[3,]    0    1    0    1    0    1
#[4,]    1    1    1    0    0    0

findAllSeq2n(3)
findAllSeq2n(4)

问题是每次运行宏时都会启动Lotus notes UI,因为这一行:

Dim noSession As Object Dim noDatabase As Object Dim nodocument As Object Dim paths() As String 'Instantiate the Lotus Notes COM's Objects. Set MailSession = CreateObject("Notes.NotesSession") Set MailDatabase = MailSession.GetDatabase("","") 'If Lotus Notes is not open then open the mail-part of it. If MailDatabase.IsOpen = False Then MailDatabase.OPENMAIL 'Create the list of recipients. vaRecipients = VBA.Array(EmailTo) vaCCRecipients = VBA.Array(EmailCC) vaBCCRecipients = VBA.Array(EmailBCC) Set MailDoc = MailDatabase.CreateDocument MailDoc.Form = "Memo" MailDoc.sendTo = vaRecipients MailDoc.copyTo = vaCCRecipients MailDoc.BlindcopyTo = vaBCCRecipients MailDoc.Subject = Subject MailDoc.Body = Body MailDoc.SAVEMESSAGEONSEND = True MailDoc.ReplyTo = Sender MailDoc.SMTPOriginator = Sender MailDoc.Sender = Sender MailDoc.principal = Sender MailDoc.inetprincipal = Sender MailDoc.from = Sender MailDoc.inetfrom = Sender MailDoc.displayfrom = Sender paths = Split(AttachmentPaths,";") Dim richTextItem As Object Dim AttachmentPath As String For i = LBound(paths()) To UBound(paths()) AttachmentPath = paths(i) Set richTextItem = MailDoc.CreateRichTextItem("Attachment" & i) Call richTextItem.Embedobject(1454,"",AttachmentPath) Next i MailDoc.Send 0,vaRecipients 'Release objects from memory. Set nodocument = nothing Set noDatabase = nothing Set noSession = nothing MsgBox "The e-mail has successfully been created and distributed",vbinformation

解决方法

您认为 OPENMAIL 是客户端打开的原因是错误的。

真正的问题是您对基于 OLE 的对象类“Notes.NotesSession”的使用。 如果您真的想在不打开笔记的情况下离开 - 客户端,您需要使用基于 COM 的对象类“Lotus.NotesSession”或“Domino.NotesSession”。

问题是:使用 Lotus.NotesSession 时,您需要自己用密码初始化会话(密码必须在代码中),否则黑色 DOS-Prompt 将询问您的 Notes-密码。使用“Notes.NotesSession”时,客户端本身用于身份验证。这就是它在运行代码时打开的原因。如果它已经打开,则不会发生任何事情。

你可以查看this old post了解更多细节,它的原理仍然是正确的。

代码与其他类几乎相同,您还需要一个正确安装的 Notes 客户端。你只需要初始化会话而不是仅仅使用它:

Set MailSession = CreateObject("Lotus.NotesSession")
MailSession.Initialize( "yourpassword" )

如果您不输入密码,系统会提示您输入。