LotusScript for setting up and configuring Locations & replicating Server NAB

Until Managed Mail Replicas (MMR) I needed a method to change locations, create locations, and change a few other settings within Lotus Notes. This can still be helpful even if you use MMR though. Along comes LotusScript to save the day. I spent several months tweaking this code to get it working the way I wanted. I know there are better ways I could probably have done it but I am still learning. If anyone has feedback please feel free to leave a comment with suggestions.

Sub Click(Source As Button)
 Dim db As New NotesDatabase ("", "names.nsf")
 Dim session As New NotesSession
 Dim view As NotesView
 Dim doc As NotesDocument
 Dim nextdoc As NotesDocument
 Dim destLocation As Variant

 On Error Goto ErrHandler

 'Delete Travel location document
 Set view = db.GetView("Locations")
 Set doc = view.GetFirstDocument
 While Not doc Is Nothing
 LocName = doc.GetItemValue("Name")
 Set nextdoc=view.GetNextDocument(doc)
 If LocName(0) = "Travel" Then
 Call doc.Remove(True)
 Msgbox "Location Travel deleted"
 End If
 Set doc=nextdoc
 Wend

 'Delete Home location document
 Set view = db.GetView("Locations")
 Set doc = view.GetFirstDocument
 While Not doc Is Nothing
 LocName = doc.GetItemValue("Name")
 Set nextdoc=view.GetNextDocument(doc)
 If LocName(0) = "Home" Then
 Call doc.Remove(True)
 Msgbox "Location Home deleted"
 End If
 Set doc=nextdoc
 Wend

 'Selects the Online location and renames it to Office and changes one value
 Set view = db.GetView("Locations")
 Set doc = view.GetFirstDocument
 While Not doc Is Nothing
 LocName = doc.GetItemValue("Name")
 Set nextdoc=view.GetNextDocument(doc)
 If LocName(0) = "Online" Then
 Call doc.ReplaceItemValue("Name", "Office")
 Call doc.ReplaceItemValue("ExhaustiveNameLookup", "1")
 Call doc.Save( True, True )
 Msgbox "Office Location created"
 End If
 Set doc=nextdoc
 Wend

'*****Section to Create New Locations******
 Dim workspace As New NotesUIWorkspace
 Dim docui As NotesUIDocument

 'Create Remote Location location document
 Set view = db.GetView("Locations")
 Set doc = view.GetFirstDocument
 While Not doc Is Nothing
 LocName = doc.GetItemValue("Name")
 Set nextdoc=view.GetNextDocument(doc)
 If LocName(0) = "Remote Location" Then
 Goto RemoteLocationExists
 End If
 Set doc=nextdoc
 Wend

 'If the Location does not exist, create it
 Call workspace.ComposeDocument( "", "names.nsf", "Location" )
 Set docui = workspace.Currentdocument
 Call docui.FieldSetText("LocationType", "0")
 Call docui.FieldSetText("DirectoryServer", "%Enter Directory Server Here ie. Acme/Acme%")
 Call docui.FieldSetText("Name", "Remote Location")
 Call docui.FieldSetText("MailType", "Local")
 Call docui.FieldSetText("ExhaustiveNameLookup", "1")
 Call docui.FieldSetText("MailThreshold", "1")
 Msgbox "Remote Location created"

 Call docui.Save
 Call docui.Close

 Goto VPNConnection

RemoteLocationExists:
 Msgbox "Remote Location already exists"

VPNConnection:
 'Create VPN Connection location document
 Set view = db.GetView("Locations")
 Set doc = view.GetFirstDocument
 While Not doc Is Nothing
 LocName = doc.GetItemValue("Name")
 Set nextdoc=view.GetNextDocument(doc)
 If LocName(0) = "VPN Connection" Then
 Goto VPNLocationExists
 End If
 Set doc=nextdoc

 Wend

 'If the Location does not exist, create it
 Call workspace.ComposeDocument( "", "names.nsf", "Location" )
 Set docui = workspace.Currentdocument
 Call docui.FieldSetText("LocationType", "0")
 Call docui.FieldSetText("DirectoryServer", "%Enter Directory Server Here ie. Acme/Acme%")
 Call docui.FieldSetText("Name", "VPN Connection")
 Call docui.FieldSetText("MailType", "Local")
 Call docui.FieldSetText("ExhaustiveNameLookup", "1")
 Call docui.FieldSetText("MailThreshold", "1")
 Msgbox "VPN Connection Location created"

 Call docui.Save
 Call docui.Close

 Goto InternetConnection

VPNLocationExists:
 Msgbox "VPN Location already exists"

InternetConnection:
 'Create Internet Connection location document
 Set view = db.GetView("Locations")
 Set doc = view.GetFirstDocument
 While Not doc Is Nothing
 LocName = doc.GetItemValue("Name")
 Set nextdoc=view.GetNextDocument(doc)
 If LocName(0) = "Internet Connection" Then
 Goto InternetLocationExists
 End If
 Set doc=nextdoc

 Wend

 'If the Location does not exist, create it
 Call workspace.ComposeDocument( "", "names.nsf", "Location" )
 Set docui = workspace.Currentdocument
 Call docui.FieldSetText("LocationType", "0")
 Call docui.FieldSetText("DirectoryServer", "%Enter Directory Server Here ie. Acme/Acme%")
 Call docui.FieldSetText("Name", "Internet Connection")
 Call docui.FieldSetText("MailType", "Local")
 Call docui.FieldSetText("ExhaustiveNameLookup", "1")
 Call docui.FieldSetText("MailThreshold", "1")
 Msgbox "Internet Connection Location created"

 Call docui.Save
 Call docui.Close

 Goto AddressBook

InternetLocationExists:
 Msgbox "Internet Location already exists"

AddressBook:
 'Declares the variables to create the Directory Address Book Replica
 Dim ddb As notesdatabase
 Dim replica As notesdatabase
 Dim dworkspace As New NotesUIWorkspace

 'Sets the database to the server, points it to the names.nsf, and creates a local replica called %Pick a local replica name%
 Set ddb = New NotesDatabase( "%Enter Server Name%", "names.nsf" )
 Set replica = ddb.createreplica ("", "%Enter Local Replica Name%")

 'Replicates against the server
 Call replica.replicate("%Enter Directory Server Here ie. Acme/Acme%")

 'Adds the local database for the local replica name database to the Replication and Sync view
 Call dworkspace.AddDatabase("","%Enter local replica name%")
 Msgbox "Directory Address Book replica created"

 'Declares variables to create the connection settings for the Internet Connection location
 Dim iworkspace As New NotesUIWorkspace
 Dim wodoc As NotesUIDocument
 'Create Connection document
 Call iworkspace.ComposeDocument( "", "names.nsf", "Connection" )
 Set wodoc = iworkspace.Currentdocument

 'Changes the settings of the connection document for the Internet Connection
 Call wodoc.FieldSetText("ConnectionType", "Local Area Network")
 Call wodoc.FieldSetText("LanPortName", "TCPIP")
 Call wodoc.FieldSetText("Destination", "%Enter Directory Server Here ie. Acme/Acme%")
 Call wodoc.FieldSetText("ConnectionLocation", "Internet Connection")
 Call wodoc.FieldSetText("Source", "*")
 Call wodoc.FieldSetText("OptionalNetworkAddress", "%Enter DNS address Here ie. acme@acme.com%")

 Call wodoc.Save
 Call wodoc.Close
 Msgbox "Internet Connection document created"

ErrHandler:
 Resume Next

End Sub