UI document close

So, I have a script that when a certain document opens, processing happens, and then the document closes itself and the script moves on to the next document.

Call uidoc.close(True)

I have this at the bottom of the postopen script, but when I debug I see that it reads the line, but doesn’t close the document.

Anybody have any idea why?

thanks.

Subject: UI document close

Is it necessary for the document to actually open on the UI. Because if not you can call the method from the QueryOpen event and set continue to false after processing, this way the document never opens

Subject: RE: UI document close

yes, it’s absolutely necessary. the postopen calls a script from the script library to save an OLE document as an attachment instead. it won’t work on anything other than a UIdocument.

I tried it every way imaginable. agents, etc. This is the only way I can get it to work. but, the problem is I have thousands of documents it needs to process and if it keeps all these UIdocuments open, after about 10 of them I get an error message about it not being able to keep the UIdocument open - which is fine because I don’t want it to!!

is there any way to suppress error messages? because that would work too.

Subject: RE: UI document close

Error suppression is usu. done with the following statement.

On error resume next

If you know the statement number, you should use it, because then only the specific error is bypassed, and not all others.

On error 1234 resume next

'replace 1234 with actual error number

Subject: RE: UI document close

I put that errror message in there and I still get this message:

unable to defer document close

after about 20 records.

Subject: RE: UI document close

The problem might be that your trigger, the processing and the close statement are in different places and this causes the close statement not to execute though im not sure about how it is that you’re actually making everything run.Anyway, if i were trying to do something similar i would put everything in an agent that will run thru all the documents, open them in the UI, process, save and close one by one. The agent should be run from the action menu and have a target of “All documents in view” or “All selected documents” if you wanna choose which documents to process. Also you should remove anything you have in the Open events of the form to make sure it doesn’t affect processing.

The agent would be something like this, hope it helps

Sub Initialize

Dim S As New NotesSession

Dim cdb As NotesDatabase

Dim collection As NotesDocumentCollection

Dim curDoc As NotesDocument

Dim UIWS As New NotesUIWorkspace

Dim curUIDoc As NotesUIDocument



Set cdb = S.CurrentDatabase

Set collection = cdb.UnprocessedDocuments



If collection.Count = 0 Then

	Msgbox "No documents to process"

	Exit Sub

End If



Set curDoc = collection.GetFirstDocument

Do While Not(curDoc Is Nothing)

	'Open document in edit mode and assigns it to curUIDoc

	Set curUIDoc = UIWS.EditDocument(True,curDoc)

	'----------------Processing of UI document----------------

	

	

	'-----------------Processing of UI document---------------

	Call curUIDoc.Save

	Call curUIDoc.Close(True)

	

	Set curDoc = collection.GetNextDocument(curDoc)

Loop

End Sub

Note where it says Processing of UI document you have the handle you need on the current UI document being processed so you can do whatever you want with it from there.

Subject: RE: UI document close

okay, I did that as an agent and had 10 documents selected to run on.

everything works great, except it doesn’t close the UIdocument until after ALL of the documents have been processed. I can see 10 UI documents sitting there and once the last one processes, THEN it closes all 10.

???

I put in that EXACT script. This script runs on the PostOpen of the document:

Sub Postopen(Source As Notesuidocument)

Dim workspace As New NotesUIWorkspace

Dim uidoc As NotesUIDocument

Set uidoc = workspace.CurrentDocument

cannum=uidoc.FieldGetText( "fs_candidate_num" )

ln=uidoc.fieldgettext("pelastname")

afilename=cannum+ln

bfilename="c:\resfiles\"+ln+".doc"

Dim sReturn$



sReturn$ = gpProcessOLEObjectUI2("SaveAs")



Dim session As New NotesSession



Dim db As New notesdatabase("","flagstaff\fastaff_resumes.nsf")

Dim doc As NotesDocument

Set doc = uidoc.document

Dim rtitem As NotesRichTextItem

Dim object As NotesEmbeddedObject

Dim doc3 As notesdocument

Set doc3=New notesdocument(db)

Call doc.copyallitems(doc3,True)

doc3.atttype="Converted"

doc.atttype="Object Converted"

Set rtitem = New NotesRichTextItem( doc3, "resBody" )

Set object = rtitem.EmbedObject _

( EMBED_ATTACHMENT, "", "c:\resfiles\doc.doc")

Call uidoc.fieldsettext("atttype","Completed")

Call uidoc.save

Call doc3.Save( True, True )



Call uidoc.close(True)

End Sub

Subject: RE: UI document close

From what i can see there were a few lines that weren’t necessary and might cause you trouble, like for example, you’re setting the backend document of the UIdoc to doc, and then you’re setting the field atttype, first in the doc then in the uidoc. But your not saving the backend document at any time so this field should just be set in the uidoc then saved and the only reason you still need the backend document is to copy the items to doc3 which from i can tell is a document you’re creating on a separate database.

you can try adding that gpProcessOLEObjectUI2() function to the agent and changing the initialize code like below.

You should erase anything in the PostOpen method of the form if you’re going to use the agent.

Sub Initialize

Dim S As New NotesSession

Dim cdb As NotesDatabase

Dim collection As NotesDocumentCollection

Dim curDoc As NotesDocument

Dim UIWS As New NotesUIWorkspace

Dim curUIDoc As NotesUIDocument



Set cdb = S.CurrentDatabase

Set collection = cdb.UnprocessedDocuments



If collection.Count = 0 Then

	Msgbox "No documents to process"

	Exit Sub

End If



Set curDoc = collection.GetFirstDocument

Do While Not(curDoc Is Nothing)

	'Open document in edit mode and assigns it to curUIDoc

	Set curUIDoc = UIWS.EditDocument(True,curDoc)

	'----------------Processing of UI document----------------

	cannum = curUIdoc.FieldGetText( "fs_candidate_num" )

	ln=CurUIdoc.fieldgettext("pelastname")

	afilename=cannum+ln

	bfilename="c:\resfiles\"+ln+".doc"

	Dim sReturn$

	

	sReturn$ = gpProcessOLEObjectUI2("SaveAs")

	

	Dim db As New notesdatabase("","flagstaff\fastaff_resumes.nsf")

	Dim rtitem As NotesRichTextItem

	Dim object As NotesEmbeddedObject

	Dim doc3 As notesdocument

	Set doc3=New notesdocument(db)

	Call curDoc.copyallitems(doc3,True)

	doc3.atttype="Converted"

	'doc.atttype="Object Converted"

	Set rtitem = New NotesRichTextItem( doc3, "resBody" )

	Set object = rtitem.EmbedObject _

	( EMBED_ATTACHMENT, "", "c:\resfiles\doc.doc")

	Call curUIdoc.fieldsettext("atttype","Completed")

	

	Call doc3.Save( True, True )

	

	'-----------------Processing of UI document---------------

	Call curUIDoc.Save

	Call curUIDoc.Close(True)

	

	Set curDoc = collection.GetNextDocument(curDoc)

Loop

End Sub

If you still have the same problem, it might be because whatever you’re calling in gpProcessOLEObjectUI2 is reading the document in the UI and is preventing it from closing.

Subject: RE: UI document close

I tried putting that gpprocessole in an agent and it won’t even let me save the agent when I put that in.

not sure why, but it won’t.

I wrote the thing in a HURRY so it doesn’t surprise me if I have stuff in there I don’t need.

Function gpProcessOLEObjectUI2(psCmd$) As String

 ' This routine is called from the SmartIcons.  It is used to create and edit

 ' OLE obejcts.

Dim bObject As Integer

Dim sCmd$, sReturn$, sTmpFile$

Dim sOleField$, sOleObjProgID$, sOleName$, sOleClass$

 ' LN Objects

Dim rtitem As NotesRichTextItem

Dim object As NotesEmbeddedObject

Dim vOLEObject As Variant

Dim vWord As Variant

Dim vWordBasic As Variant



On Error Goto ipErrorHandler



 ' Initialization 

sOleName$ = ""

sOleClass$ = ""

sReturn$ = ""

bObject% = False



' Get the command to execute

sCmd$ = Trim$(Ucase$(psCmd$))

Print "Doing OLE processing: " & sCmd$



 ' Initialize LN Objects

Set ws = New NotesUIWorkspace

Set uidoc = ws.CurrentDocument

uidoc.AutoReload = False

Set docTemp = uidoc.Document



 ' If in preview mode then exit

If (uidoc.InPreviewPane) Then 

	Exit Function

End If



 ' Check to see if an OLE field and type are specified on the form.  If not, then exit.    

If docTemp.hasitem("$OLEObjProgID") Then

	bObject% = True

	sOleField$ = docTemp.~$OLEObjField(0)

	sOleObjProgID$ = docTemp.~$OLEObjProgID(0)          

Else

	Print "No OLE field is specified on this document."

	Exit Function

End If



Select Case sCmd$          

	

Case "NEW"

	If bObject% Then

		Gosub ipNewObject

	End If

	

Case "EDIT"

	If bObject% Then

		Gosub ipObjectName

		If Len(sOleName$) Then

			Gosub ipEditObject

		Else

			Messagebox "Unable to access embedded object.", 64, "Error"

		End If               

	End If

	

Case "NEWOREDIT"

      ' Determine if there is an embedded object.  If so, do Edit

      ' If not, then do New.

	If bObject% Then

		Gosub ipObjectName

		If Len(sOleName$) Then

			Gosub ipEditObject

		Else

			Gosub ipNewObject

		End If                              

	End If

	

Case "NAME"

	If bObject% Then

		Gosub ipObjectName

	End If

	sReturn$ = sOleName$

	

Case "CLASS"

	If bObject% Then

		Gosub ipObjectClass               

	End If

	sReturn$ = sOleClass$

	

Case "PRINT"          

	If bObject% Then              

		Gosub ipObjectName

		Gosub ipPrintObject

	Else

		Call uidoc.Print

	End If

	

Case "SAVEAS"          

	If bObject% Then              

		Gosub ipObjectName

		Gosub ipSaveObject                       

	End If

	

Case Else

End Select               

ipFinish:

Print ""



 ' Deallocate all objects      

Set rtitem = Nothing

Set object = Nothing

Set vWordBasic = Nothing

Set vWord = Nothing                    



gpProcessOLEObjectUI2 = sReturn$

Exit Function

ipNewObject:

 ' Make sure the document is in edit mode

If uidoc.EditMode = False Then

	uidoc.EditMode = True

End If     

 ' Go to the field that will contain the object

uidoc.gotofield(sOleField$)              

 ' Get the name of the new object from the field on the form

sOleName$ = sOleObjProgID$                    

 ' Create a new object

Print "Creating a new " & sOleName$ & "..."

Call uidoc.CreateObject(sOleName$, sOleObjProgID$, "")

Return

ipEditObject:

' Make sure the document is in edit mode

If uidoc.EditMode = False Then

	uidoc.EditMode = True

End If          

 ' Launch the OLE object

Print "Editting " & sOleName$ & "..."

Set vOLEObject = uidoc.GetObject(sOleName$)       

Return

ipPrintObject:

Print "Printing " & sOleName$ & "..."

Select Case Ucase$(sOleName$)

Case "WORD.DOCUMENT", "WORD.DOCUMENT.8"

      ' Get the handle to the Word object and activate the window

	Set vWord = object.Activate ( True )          

      ' Minimize the application

	vWord.Application.WindowState = 2

      ' Get the handle to Word Basic

	Set vWordBasic = vWord.Application.WordBasic

      ' Print the document

	vWordBasic.FilePrint

      ' Close the document

	vWordBasic.FileClose

Case Else                                                  

	Messagebox "Unable to process embedded object named '" & sOleName$ & "'", 64, "Error"         

End Select     

Return

ipSaveObject:

Print "Saving " & sOleName$ & "..."

Select Case Ucase$(sOleName$)

Case "WORD.DOCUMENT", "WORD.DOCUMENT.8"

      ' Get a file name

	'sTmpFile$ = Inputbox("File Name: ", "Save As", "c:\temp\newdoc.doc")

	sTmpFile$="c:\resfiles\doc.doc"

	If Len(sTmpFile$) = 0 Then

		Return

	End If

	If gpFileExists(sTmpFile$) Then

		Kill sTmpFile$

	End If

      ' Get the handle to the Word object and activate the window

	Set vWord = object.Activate ( True )          

      ' Minimize the application

	vWord.Application.WindowState = 2

      ' Get the handle to Word Basic

	Set vWordBasic = vWord.Application.WordBasic

      ' Save the document

	vWordBasic.FileSaveAs sTmpFile$         

      ' Close the document

	vWordBasic.FileClose

Case Else                                                  

	Messagebox "Unable to process embedded object named '" & sOleName$ & "'", 64, "Error"         

End Select     

Return

ipObjectName:

sOleName$ = ""

Set rtitem = docTemp.GetFirstItem(sOleField$)

If rtitem Is Nothing Then

Else

	Set object = rtitem.EmbeddedObjects(0)

	If object Is Nothing Then

	Else

		sOleName$ = object.name

	End If                        

End If

Print "Object's name is " & sOleName$

Return

ipObjectClass:

sOleClass$ = ""

Set rtitem = docTemp.GetFirstItem(sOleField$)

If rtitem Is Nothing Then

Else

	Set object = rtitem.EmbeddedObjects(0)

	If object Is Nothing Then

	Else

		sOleClass$ = object.class

	End If                        

End If

Print "Object's class is " & sOleClass$

Return

ipErrorHandler:

Call gpError("Error (gpProcessOLEObjectUI2): " & Cstr(Err) & "-" & Error$)

Resume ipFinish       

End Function

I didn’t write this script, and I tried putting a uidocument close method line in it but it would give me an error message when i did.

Subject: RE: UI document close

I tried adding it over here and it wouldn’t save due to script errors, but its because of an unavailable function called gpFileExists that it calls. Make sure you’re adding any other libraries that the method would be using. Check where it was originally. If it was in the form any additional declarations or libraries should be in there in the Globals section or as additional methods in the form. Those need to be added to the agent in the same way. Also make sure you’re pasting the code outside of any function or sub clauses to make sure it creates the new function

Subject: RE: UI document close

thanks for all your help!! I got it going. for some reason I used your script and it started working. then I turned off any error message and it works fine.

seriously, thank you! I really appreciate the help because you DID help!!