The following code, creates a new document based on a ui document opened in read mode. The code works great if there is more than one template to choose from, however, if there is only one template, I get an error “Object variable not set” when it is trying to close the ui template. I am successful in creating my new document from the template, but I would like to close the template down before the user begins working in the new document. If anyone can see where my problem is, please let me know.
Here’s my code:
Sub Initialize
Dim session As New notessession
Dim uiws As New notesuiworkspace
Dim database As NotesDatabase
Dim vview As NotesView
Dim vdoc As NotesDocument
Dim collection As notesdocumentcollection
Dim noteCursorDoc As NotesDocument
Dim noteUIEditDocument As notesuidocument
Dim uiNewDoc As notesuidocument
Dim count As Integer
count = 0
Set noteCursorDoc = session.currentdatabase.createdocument
Set database = session.currentdatabase
Set vview = database.GetView("MasterFinal")
Set vdoc = vview.GetFirstDocument
While Not (vdoc Is Nothing)
count = count + 1
Set vdoc = vview.GetNextDocument(vdoc)
Wend
If count > 1 Then
Set collection = uiws.PickListCollection( _
PICKLIST_CUSTOM, _
False, _
database.server, _
database.Filename, _
"MasterFinal", _
"Master Survey", _
"Please select a survey to complete.")
If Not(collection Is Nothing) Then '//User may have cancelled
Set noteCursorDoc = collection.getfirstdocument
If Not (noteCursorDoc Is Nothing) Then
Set noteUIEditDocument = uiws.Editdocument(False, noteCursorDoc)
Set uiNewDoc=uiws.ComposeDocument("","","Survey")
Call noteUIEditDocument.Close
Call uiNewDoc.refresh
End If
End If
Elseif count = 1 Then 'this is where it fails
Set noteCursorDoc = vview.getfirstdocument
If Not noteCursorDoc Is Nothing Then
Set noteUIEditDocument = uiws.Editdocument(False, noteCursorDoc)
Set uiNewDoc=uiws.ComposeDocument("","","Survey")
Call noteUIEditDocument.Close
Call uiNewDoc.refresh
End If
Else
Dim boxType As Long, answer As Integer
boxType& = MB_OK + MB_ICONINFORMATION
answer% = Messagebox ("There are no surveys to complete at this time.", boxType&, _
"Survey Request")
Exit Sub
End If
End Sub