首页 > 解决方案 > 使用 Excel VBA 脚本遍历 Lotus Notes 中的所有视图

问题描述

我有一个脚本(在网上找到并根据我的需要进行了调整),它可以访问 Lotus Notes 的特定视图,从每封电子邮件中获取一些信息并将任何附件保存到给定文件夹中。如果我想为更多视图执行此操作,我目前在我的工作表中有这些视图的名称并循环它们。我想做的是遍历所有视图并做同样的事情。

我不是专业的开发人员,也不完全熟悉使用对象。然而,这个问题对我来说太复杂了。Lotus Notes 对象也不是最容易理解的。

我正在使用以下代码,这当然是更大(工作)脚本的一部分。对于初学者来说,只需能够遍历所有视图并打印视图的名称就足够了。从那以后,我想我可以自己做剩下的事情。任何人都可以调整代码以使其工作。

Public Sub Get_Notes_Email_Text()

Dim NSession As Object      'NotesSession
Dim NMailDB As Object       'NotesDatabase
Dim NDocs As Object         'NotesDocumentCollection
Dim NDoc As Object          'NotesDocument
Dim NNextDoc As Object      'NotesDocument
Dim view As String

'Start a Lotus Notes session
Set NSession = CreateObject("Notes.NotesSession")
'Connect to the Lotus Notes database
 Set NMailDB = NSession.GetDatabase("", "C:\Users\" & Environ("Username") & "\AppData\Local\IBM\Notes\Data\mail\" & Environ("Username") & ".nsf") 'Default server en database
If Not NMailDB.IsOpen Then
    NMailDB.OpenMail
End If

'Loop through all views and print .Name tot Immediate Window

'    Dim Map As Variant
'    Dim Mappen As Object
'    Set Mappen = NMailDB.Views
'
'    For Each Map In Mappen
'        Debug.Print Map.Name
'    Next Map
'
End Sub

标签: vbaloopsviewlotus-notes

解决方案


比浏览所有视图更容易的是只转到一个显示所有文档的视图。如果是邮件数据库,则视图称为“($All)”

Set allDocsView = NMailDB.getView("($All)")
allDocsView.autoUpdate = false

然后,您可以使用遍历视图中的文档

Set docToProcess = allDocsView.GetFirstDocument
While Not ( docToProcess Is Nothing )
    'Do what you plan to do here
Set docToProcess = allDocsView.GetNextDocument( docToProcess )
Wend

更新:当您想遍历文件夹而不是视图时,您需要执行以下操作

Dim sess As New notessession
Dim db As NotesDatabase
Dim views As Variant

Set db = sess.CurrentDatabase

views = db.Views
Forall v In views
    If v.isFolder Then ' Needed so that we are only processing Folder objects and not views - you will need to check that you are not processing system folders e.g. $Inbox etc
        'Do your processing of documents here
    End If
End Forall

推荐阅读