Object variable not set

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

Subject: Object variable not set

Have you tried using the debugger or adding error-trapping to your code to determine exactly which line is causing the problem?

Sub Initialize

On error goto errhand

…all your code…

errhand:

Msgbox "error at line " & erl & ": " & error

exit sub

End Sub

Subject: Object variable not set

You have 2 uidoc open at the same time. With Notes 6 and more, the uiws.currentdoc is the first open document in the worspace and not the latest. Even when you have script in the postopen of the newdoc, the currentdoc is not the correct current doc. Use “Source” parameter.

Subject: RE: Object variable not set

The error was being caused by code within the Post Open event and not by this code. Once removed, everything works fine.

Thanks for all your help.