alexmac97
alexmac97

Reputation: 13

Read fields in Lotus Notes Documents Through VBA

I am trying to write VBA code that will read data from fields in a Lotus Notes document. Currently, I am able to read data using the FieldGetText method, however this only works when I have the document open. I need to be able to loop through documents without opening them, as there are several hundred. I need to be able to read these same fields from many documents, but cannot figure out how to loop through them. My code currently is:

    Set LotusNotes = CreateObject("Notes.NotesUiWorkspace")
    Set CurrentDoc = LotusNotes.CurrentDocument
    While Not (CurrentDoc Is Nothing)

    ' Affectation of data
    DueDate = CurrentDoc.FieldGetText("RevDueDate")
    DueTime = CurrentDoc.FieldGetText("RevDueTime")
    DateClosed = CurrentDoc.FieldGetText("DateClosed")

    Wend

I understand that this uses a Front End object. I was able to use a Back End object that could loop through documents (without them being open), however the data (for the dates, specifically) did not match the field text from the documents. That code looked like this:

Set LotusNotes = CreateObject("Notes.NotesSession")
Set db = LotusNotes.GetDatabase("")
Set view = db.GetView(view_name)
view.AutoUpdate = False
Set columnview = view.AllEntries

Set doc = view.GetFirstDocument
While Not (doc Is Nothing)
    revDate = doc.GetItemValueDateTimeArray("RevDueDate")
    revDate = doc.RevDueDate 
Set doc = view.GetNextDocument(doc)
Wend

Basically, I'm just wondering if it's possible to loop through multiple files using the NotesUIWorkspace class that I tried first, or if it's possible to somehow use FieldGetText in the NotesWorkspace class instead.

Any help is appreciated, thanks.

Upvotes: 1

Views: 1787

Answers (2)

Richard Schwartz
Richard Schwartz

Reputation: 14628

You're using the Notes "front-end" classes rooted at Notes.NotesUIWorkspace. These are OLE classes, which means that they need the Notes client to be running and they work on the open document. There are also back-end classes rooted at Notes.NotesSession. These are also OLE classes, so they still need the Notes client to be running but they can access other documents (and other databases, servers, etc.). There is also a set of back-end classes rooted at Lotus.NotesSession. Note the different prefix. These are COM classes, so they do not need the Notes client to be running - though it does have to be installed and configured, and your code will have to provide the user's password since the client won't prompt for it.

You can find documentation and examples for the NotesSession class here. Down near the bottom of the page, you'll find links to information about using them via OLE or COM. You can find a bunch of examples based on using the COM classes here, including one that traverses the documents in a view in database. Apart from the inital setup of the session, it would be the same if you use OLE.

Upvotes: 1

Strahil
Strahil

Reputation: 76

It is possible, and also better to use the 'Back end' object Notes.NotesSession. So try this:

Set doc = view.GetFirstDocument
While Not (doc Is Nothing)
    'Check if you have the field in the document
    if doc.HasItem("RevDueDate") Then
        revDate = doc.getFirstItem("RevDueDate").Text
    End If
Set doc = view.GetNextDocument(doc)
Wend

It is also possible to use Notes.NotesUiWorkspaceobject by opening every document in the client, getting the fields data and closing the document but I would highly recommend NOT TO make it like this as there is high possibility that you will crash the Notes client, if you need to loop on more documents.

Upvotes: 0

Related Questions