Sometimes on engagements, I get asked to build things that are, well unnecssary.  Seeing how consulting is about billing hours, sometimes, I'm happy to oblige.  I recently was asked to provide a bit of code that would look to a public address book and update entries contained in a personal address book.  While this can be done, usually the full name field of a person record contains all of the varients that a user has had during their exsistance in Notes.  This agent will use the local address book's current location document to open the directory on the User's home mail server and analyze each of the personal address book entries against the $Users view in the Domino directory.  The database will create copy of the personal address book as "copynames.nsf" before making changes.

It will also document each value skipped or updated accordingly (please see the UpdatePersonalAddressBook.txt file for reference).  At the end of the process it will display a dialog box showing the number of entries processed, how many were successful, how many failed, and how many were skipped.


Thanks.


Sub Initialize
       Dim s As New NotesSession
       Dim nn As NotesName
       Dim pab As NotesDatabase, nab As NotesDatabase, bkupnab As NotesDatabase
       Dim locdc As NotesDocumentCollection
       Dim doc As NotesDocument, curlocdoc As NotesDocument, nabdoc As NotesDocument
       Dim i As Integer, successcount As Integer, failurecount As Integer, skippedcount As Integer
       Dim pview As NotesView, nview As NotesView, locview As NotesView, Nabview As NotesView
       Dim Comma As Long
       Dim currentlocation As String, currentserver As String
       Dim outfileNum As Integer, outfilePath As String
       
       On Error Resume Next
       i = 0
       successcount = 0
       skippedcount = 0
       failurecount = 0
       Set pab = s.GetDatabase("","names.nsf")
       If pab Is Nothing Or pab.IsOpen = False Then
               Msgbox "Cannot open database on local client workstation." & Chr(10) & "Exiting process."
               failurecount failurecount + 1
               Goto Done
       End If
       Set bkupnab = pab.CreateCopy("","copynames.nsf")
       Set pview = pab.GetView("people")
       If pview Is Nothing Then
               Msgbox "Cannot open Contacts view in database on local client workstation." & Chr(10) & "Exiting process."
               failurecount failurecount + 1                
               Goto Done
       End If
       currentlocation = s.GetEnvironmentString("Location",True)
       Comma = Instr(currentlocation,",")        
       If Comma  > 0 Then
               currentlocation = Strleft(currentlocation,",")
       End If
       Set locview = pab.GetView("Locations")
       If locview Is Nothing Then
               Msgbox "Cannot open Locations view in database on local client workstation." & Chr(10) & "Exiting process."
               failurecount failurecount + 1                
               Goto Done
       End If
       Set locdc = locview.GetAllDocumentsByKey(currentlocation,True)
       If locdc.Count > 0 Then
               Set curlocdoc locdc.GetFirstDocument
               currentserver curlocdoc.MailServer(0)
       Else
               Msgbox "Cannot open Curent Location Document in database on local client workstation." & Chr(10) & "Exiting process."
               failurecount failurecount + 1                
               Goto Done
       End If
       Set nab = s.GetDatabase( currentserver, "names.nsf")
       If nab Is Nothing Or nab.IsOpen = False Then
               Msgbox "Cannot open database on Server <" & currentserver & ">." & Chr(10) & "Exiting process."
               failurecount failurecount + 1                
               Goto Done
       End If
       Set Nabview = nab.GetView("($Users)")
       If Nabview Is Nothing Then
               Msgbox "Cannot open ($Users)  view in database on Server <" & currentserver & ">." & Chr(10) & "Exiting process."
               failurecount failurecount + 1                
               Goto Done
       End If
       outfileNum% = Freefile()
       outfilePath = "c:\UpdatePersonalAddressbook.txt"
       Open outfilePath$ For Append As outfileNum%
       Set doc = pview.GetFirstDocument
       Print "Getting Users from Personal Address Book"
       Print #outfileNum%,"Getting Users from Personal Address Book"
       While Not doc Is Nothing
               i = i + 1
               Set nabdoc = Nothing
               Set nn = s.CreateName(doc.Fullname(0))
               If nn.IsHierarchical = True Then
                       Set nabdoc = nabview.GetDocumentByKey(nn.Common)
                       If Not Nabdoc Is Nothing Then
                               Call nabdoc.CopyAllItems(doc, True)
                       End If
               End If
               Set nabdoc = nabview.GetDocumentByKey(doc.MailAddress(0))
               If Not nabdoc Is Nothing Then
                       Call nabdoc.CopyAllItems(doc, True)
               End If
               Set nabdoc = nabview.GetDocumentByKey(doc.FullName(0))
               If Not nabdoc Is Nothing Then
                       Call nabdoc.CopyAllItems(doc, True)
               Else
                       Print "Skipping User " & doc.Firstname(0) & " " & doc.LastName(0)
                       Print #outfileNum%,"Skipping User " & doc.Firstname(0) & " " & doc.LastName(0)
                       skippedcount = skippedcount + 1
                       Goto GetNext
               End If
               If doc.MailDomain (0) <> "" Then
                       doc.maildomain = ""
               End If
               Print "Updating User " & doc.Firstname(0) & " " & doc.LastName(0)
               Print #outfileNum%,"Updating User " & doc.Firstname(0) & " " & doc.LastName(0)
               doc.Save True,False
               successcount successcount + 1
GetNext:
               Set doc = pview.GetNextDocument(doc)
       Wend
       Close outfileNum%
Done:        
       Msgbox "Operation Completed.  Processed a total of " & i & " users." & Chr(10) & Chr(10) & _
       "Success: " & successCount & "  Failure: " & failureCount & "  Skipped: " & skippedCount
       
End Sub

Comments (0)
Perry Hiltz June 12 2008 10:55:08 AM