LotusScript gets unread mail

xiaoxiao2021-03-17  171

Option PublicOption Explicit

'** Notes C-API functions used by the UnreadDocList class (these are Windows-specific' ** calls - please adjust as necessary for other operating system platforms) Declare Function OSPathNetConstruct Lib "nnotes.dll" (Byval portName As Integer, _Byval ServerName As String, Byval FileName As String, ByVal Pathname As String) AS INTEGER

Declare function nsfdbopen lib "nnotes.dll" (byval dbname as string) (Byval HDLLL "(Byval HDLL" (Byval HDLLL "(Byval HDLLL) AS Integer

Declare Function NSFDbGetUnreadNoteTable Lib "nnotes.dll" (Byval hDB As Long, _Byval userName As String, Byval userNameLength As Integer, _Byval fCreateIfNotAvailable As Boolean, rethUnreadList As Long) As Integer

Declare function nsfdbgetmodifiednotetable lib "nnotes" (Byval Noteclassmask as ", _byval startdate as double, retenddate as double, rethtable as long) AS Integer

Declare Function Identries LIB "Nnotes" (Byval Htable As Long, Byval TfirstBool As Integer, Retid As Long) AS INTEGER

DECLARE FUNCTION OSMEMFREE LIB "Nnotes" (Byval Handle As Long) AS Integer

'** error code masksconst err_mask = & h3fffconst pkg_mask = & h3f00const errnum_mask = & h00ff

Declare function osloadstring lib "nnotes.dll" (Byval Stringcode As INTEGER, _BYVAL RETBUFFER AS STRING, BYVAL BUFFERLENGTH AS INTEGER AS Integer

Class UnreadDocList% REMThe UnreadDocList class provides a way to programmatically accessthe list of unread docs in a database or a view / folder. As far as I know, there's no good built-in way of doing this as of Notes 6.x, so I had to usesome C-API calls.Because we're calling the C-API, you'll also need to declare several APIfunctions in the Declarations section of the agent or script library thatholds this class. If you got this class without the related API Declarations, please see the Original Version of this code at http://www.nsftools.com

Here's An Example of getting the unread docs in a user's inbox:

Dim session As New NotesSession Dim db As NotesDatabase Dim inbox As NotesView Dim mailDb As Variant Dim udc As New UnreadDocList Dim unreadArray As Variant mailDb = Evaluate ( "@ MailDbName") Set db = session.GetDatabase (mailDb (0), mailDb (1 )) SET INBOX = db.getView ("($ INBOX)") UnreadArray = udc.getunreadInView (Inbox, session.effectiveUserName) if (len (udc.getlasterror ())> 0) Then Print "There Was An error:" & udc.getlasterror () end ififf (unreadarray (0) = "") Then Print "There 0 unread Docs in Your Inbox" Else Print "There" & (Ubound (unreadarray) 1) & "UnRead Docs in Your inbox "endiff

A few string to note about this class:

.

2. The GetUnreadInDB and GetUnreadInView functions return an array of NoteIDs if unread docs are found. If you need to access thedocs themselves, you can step through the array and get them oneat a time with NotesDatabase.GetDocumentByID (noteID)

3. The process is that you first get all of the unread docs in the entiredatabase, then you see which of those docs are in a given view. TheGetUnreadInDB function actually only gets the first 32,767 unread docsin the database, so it's possible that you won 't get an accurate list fordatabases with really large numbers of unread docs.4. you need to supply a valid user name that you're checking the unreaddocs for. This is because different users have different unread counts (which is obvious if you think About it).

5. I'm not sure how well this works when run on a server versus beingrun on a user's workstation. That's because I think the unread marksused to be stored in the user's local desktop.dsk file, and in some versionof Notes (version 6 ?) I Think It Was Stored in The Database Somehow, To Allow Unread Marks To Replicate. Your Best Bet Is To Test IT and See.

You can use this code in Any Way You Want, As Long ANYTHING, AND YOURE PRETEND You Wrote It yourself.

version 1.0April 15, 2005Julian Robichaux (http://www.nsftools.com)% END REM Private lastError As String Public Function getLastError () As String '** if any errors occurred while the getUnreadInView or' ** getUnreadInDb functions ran, getLastError will return '** a string indicating the nature of the error (if there were' ** no errors, a blank string will be returned) getLastError = lastError End Function Public Function getUnreadInView (view As NotesView, userName As String) As Variant '** returns an array of NoteIDs representing the docs in the view' ** that are marked as unread for the given user (or an empty '** array if nothing was found) On Error Goto processError Dim returnArray () As String' ** first try to get all the unread docs in the database (if there are '** none, we can stop processing right now) Dim unreadArray As Variant Redim returnArray (0) As String unreadArray = getUnreadInDB (view.Parent, userName) IF (unreadarray (0) = "") THEN GETUNREADINV iew = returnArray Exit Function End If '** set the view's AutoUpdate flag to False, so we can step through' ** the view a little faster Dim viewFlag As Integer viewFlag = view.AutoUpdate view.AutoUpdate = False '** get the NoteIDs of all the docs in the view Dim doc As NotesDocument Dim viewDocList List As String Dim count As Integer Dim i As Integer Set doc = view.GetFirstDocument Do Until (doc Is Nothing) viewDocList (Right ( "00000000" & doc.NoteID, 8)) = DOC.NOTEID SET DOC = view.getnextdocument (doc) loop view.autoupdate = viewflag '

** compare the NoteIDs in the view with the ones in the unreadArray For i = 0 To Ubound (unreadArray) If Iselement (viewDocList (unreadArray (i))) Then Redim Preserve returnArray (count) As String returnArray (count) = unreadArray ( i) count = count 1 End If Next getUnreadInView = returnArray Exit Function processError: lastError = Error $ getUnreadInView = returnArray Exit Function End Function Public Function getUnreadInDB (db As NotesDatabase, userName As String) As Variant '** return an array of NoteIDs for all the "unread" docs in the given '** database for the given user (or at least the first 32,767 unread' ** docs, since that's the maximum upper-bound of an array - you '** could actually double this number by starting the array at -32,786 '** instead of 0, but it's more "natural" to return a 0-based array, and' ** frankly if there are more than 32,000 unread docs in the database '** you Should Probably Narrow your Query Anyway) DIM HDB As Long Dim hIDTable As Long Dim notesUserName As NotesName Dim longUserName As String Dim pathName As String * 256 Dim noteID As Long Dim firstFlag As Integer Dim result As Integer Dim count As Long Dim returnArray () As String '** initialize some variables Redim returnArray (0) As String lastError = "" '** create a proper network path name with OSPathNetConstruct Call OSPathNetConstruct (0, db.Server, db.FilePath, pathName)' ** open the database and get a handle with NSFDbOpen result = NSFDbOpen (pathName, HDB) IF Result <> 0 Then Lasterror = "Cannot Open Database" & Db.FilePath & "

ON Server "& DB.Server & _" Error WAS "& CSTR (Result) &": "& getApierror (Result) got the ID Table of All The Unread Docs in The Database (UnRead Marks) are '** kept on a per-user basis, so you need to provide a user name as well) Set notesUserName = New notesName (userName) longUserName = notesUserName.Canonical result = NSFDbGetUnreadNoteTable (hDB, userName, Len (username), 0 , hidtable) if result <> 0 THEN LASTERROR = "Cannot Open ID Table On" & DB.Server & ". Error WAS" & CSTR (Result) & ":" & GetApierror Result) goto closedb end11 ** make Sure We got some ids return to us (if not, just exit) count = identer (hidtable) if (count = 0) Then goto freeidtable else '** redim the return array To Proper size, but don't let it get '** Too Big IF (count> 32767) THEN Redim ReturnaRray (32767) AS STRING ELSE Redim Returnarray (Count) AS STRING End if Count = 0 end if '** get the NoteIDs in the table and put them in the array firstFlag = True Do While IDScan (hIDTable, firstFlag, noteID)> 0 returnArray (count) = ConvertNoteID (noteID) firstFlag = False count = count 1 If (count > Ubound (returnArray)) Then Exit Do End If Loop freeIDTable: '** free the memory used when we grabbed the ID table Call OsMemFree (hIDTable)' should possibly use IDDestroyTable instead closeDb:? '** close the database with NSFDbClose Call Nsfdbclose (HDB) endoffunction: getunreadindb =

returnArray Exit Function End Function Private Function GetAPIError (errorCode As Integer) As String '** this function translates Notes API error codes into their' ** corresponding error strings Dim errorString As String * 256 Dim returnErrorString As String Dim resultStringLength As Long Dim errorCodeTranslated As Integer '** mask off the top 2 bits of the errorCode that was returned; this is' ** what the ERR macro in the API does errorCodeTranslated = (errorCode And ERR_MASK)' ** get the error code translation using the OSLoadString API function resultStringLength = OSLoadString (0, errorCodeTranslated, errorString, Len (errorString) - 1) '** strip off the null-termination on the string before you return it If (Instr (errorString, Chr (0))> 0) Then returnErrorString = Left $ (Errorstring, INSTR (Errorstring, Chr (0)) - 1) Else ReturnerRORSTRING = Errorstring End if getapierror = ReturnerRorstring End Function Private Function ConvertNoteId (Noteid As Long) As String '** convert the noteID to a Hex value, and left-pad it with zeros Dim noteIDString As String noteIDString = Hex $ (noteID) noteIDString = String (8 - Len (noteIDString), "0") & noteIDString ConvertNoteID = NoteidString End Function End Class

转载请注明原文地址:https://www.9cbs.com/read-129721.html

New Post(0)