You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VBA操作Lotus Notes如何指定非默认的TEST收件箱查询转发邮件?

问题根因

代码存在变量名传参错误,导致没有读取TEST数据库的收件箱:

  • 你已经将TEST.nsf的收件箱视图赋值给了NViewObj变量,但调用查询函数时错误传入了未初始化的NInboxView变量,空对象触发了默认读取个人收件箱的逻辑
  • 缺少TEST数据库加载成功的校验逻辑,若nsf路径错误也会导致查询失败
修复后完整代码
Public Sub Forward_Email(findSubjectLike As String, forwardToEmailAddresses As String)

    Dim NSession As Object
    Dim NMailDb As Object
    Dim NInboxView As Object
    Dim NDocument As Object
    Dim NUIWorkspace As Object
    Dim NUIDocument As Object
    Dim NFwdUIDocument As Object
   
    
    Set NSession = CreateObject("Lotus.NotesSession")
    Call NSession.Initialize("password") '此处替换为你的Notes登录密码
    
    Set NUIWorkspace = CreateObject("Notes.NotesUIWorkspace")
    ' 如果TEST.nsf不在Notes默认数据目录,第二个参数写完整文件路径,若在其他服务器第一个参数填服务器地址
    Set NMailDb = NSession.GetDatabase("", "TEST.nsf")
    ' 新增数据库打开校验
    If Not NMailDb.IsOpen Then
        MsgBox "TEST数据库打开失败,请检查nsf路径配置"
        Exit Sub
    End If
    Set NInboxView = NMailDb.GetView("Inbox")
    ' 修正传参,传入TEST库的收件箱视图
    Set NDocument = Find_Document(NInboxView, findSubjectLike)
   
    If Not NDocument Is Nothing Then
        Set NUIDocument = NUIWorkspace.EditDocument(False, NDocument)
       
        NUIDocument.Forward
       
        Set NFwdUIDocument = NUIWorkspace.CurrentDocument
        Sleep 100
       
        NFwdUIDocument.GoToField "To"
        Sleep 100
        NFwdUIDocument.InsertText forwardToEmailAddresses
       
        NFwdUIDocument.GoToField "Body"
        NFwdUIDocument.InsertText "This email was forwarded at " & Now
        NFwdUIDocument.InsertText vbLf
        
        NFwdUIDocument.Send
        NFwdUIDocument.Close

        Do
            Set NUIDocument = NUIWorkspace.CurrentDocument
            Sleep 100
            DoEvents
        Loop While NUIDocument Is Nothing
        NUIDocument.Close
       
    Else
        MsgBox vbCrLf & findSubjectLike & vbCrLf & "not found in TEST Inbox"
    End If
   
    Set NUIDocument = Nothing
    Set NFwdUIDocument = Nothing
    Set NDocument = Nothing
    Set NMailDb = Nothing
    Set NUIWorkspace = Nothing
    Set NSession = Nothing
   
End Sub

Private Function Find_Document(NView As Object, findSubjectLike As String) As Object

    Dim NThisDoc As Object
    Dim thisSubject As String
   
    Set Find_Document = Nothing
   
    Set NThisDoc = NView.GetFirstDocument
    While Not NThisDoc Is Nothing And Find_Document Is Nothing
        thisSubject = NThisDoc.GetItemValue("Subject")(0)
        ' 当前为完全匹配,需要模糊匹配可替换为下一行注释内容
        If LCase(thisSubject) = LCase(findSubjectLike) Then Set Find_Document = NThisDoc
        ' If LCase(thisSubject) Like "*" & LCase(findSubjectLike) & "*" Then Set Find_Document = NThisDoc
        Set NThisDoc = NView.GetNextDocument(NThisDoc)
    Wend

End Function
额外配置说明
  • 若TEST.nsf存储在非Notes默认数据目录,需将GetDatabase方法的第二个参数替换为nsf文件的完整绝对路径
  • 若TEST邮箱存储在Domino服务器而非本地,需将GetDatabase方法的第一个空字符串参数替换为对应的服务器地址

内容的提问来源于stack exchange,提问作者Drawleeh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 19:45:01