Lotusscript - close document

With a lot of help from other Notes developers, I’ve put a button in the inboxes or many users. What it does is defined in the beginning of the code. And it works great. However, we are finding that when users try to use this button with the email still open or in the preview, they get an error message “Cannot remove NotesDocument when instantiated by NotesUIDocument”. We’d like to avoid them getting this error message, by adding script that will save and close the document. I have tried using the Help in Designer, using info on the web, and playing around with it myself. But unfortunately, I am not a developer and VERY new to Lotusscript. Could someone please offer their assistance?Thanks very much in advance. Here is the script minus the walkmime function:

Sub Click(Source As Button)

'Notes Agent: name=ReportAsSpam, trigger=onEvent, Action menu selection, target=All selected docs

'forwards the selected message(s) with full header to a spam reporting email address

'moves message from inbox to trash, refreshes automatically and appends “Spam Submission >” to the subject line

'requires: Function walkmime (mime As notesmimeentity) As String

Const SPAMREPORTADDRESS = "xxx.com" 'the email address used to report spam, uncomment to go live

Const SPAMREPORTADDRESS1 = "yyy.com" 'the email address used to report spam, uncomment to go live



Dim sess As New NotesSession

Dim ws As New NotesUIWorkspace



Dim db As NotesDatabase

Dim memo As NotesDocument, doc As NotesDocument

Dim col As NotesDocumentCollection

Dim mime As NotesMIMEEntity

Dim rtitem As NotesItem

Dim temp As Variant 'to get info from non-ASCII data

Dim i As Integer 'to count messages



Dim uiview As NotesUIView 'to update the inbox after messages have been moved

Dim mail As NotesRichTextItem 'the sent message

Dim item As NotesItem 'to process fields with multiple items (i.e. recieved)

Dim res As Integer





sess.ConvertMime = False 'init mime conversion

Set db = sess.CurrentDatabase

Set col = db.UnprocessedDocuments

Set doc = col.GetFirstDocument()

If doc Is Nothing Then Exit Sub 'sanity check to abort early



Print "Reporting " + Cstr( col.Count ) + " selected documents as Spam to " + SPAMREPORTADDRESS + ". Please wait..."

i = 1



Set col = db.UnprocessedDocuments

If Not col Is Nothing Then

	Set doc = col.GetFirstDocument()

	If Not doc Is Nothing Then

		res = ws.Prompt (PROMPT_YESNO, "Alert", "Are you sure you want to report this email as spam?")

		If res =1 Then 'Executes if user select "Yes" to proceed

		Else

			Exit Sub ' Exists subroutine. Stops execution of code.

		End If

		While Not(doc Is Nothing) 

			Set memo = db.CreateDocument

			Set mail = New NotesRichTextItem( memo, "Body" )

			

			Set mime = doc.GetMIMEEntity

			If Not(mime Is Nothing) Then

				Forall header In mime.HeaderObjects

					Call mail.AppendText( walkmime ( mime ) ) 'calls required function walkmime

				End Forall

				Call doc.CloseMIMEEntities 'allow clean switch to using items

				

				If Instr( doc.Subject(0), "SPAM Submission" ) = 0 Then 

					Call doc.ReplaceItemValue("Subject", "SPAM Submission > " + doc.Subject(0) ) 'remove [spam]

				End If

				

				memo.subject = doc.subject(0)

				Call memo.Send( False, SPAMREPORTADDRESS) 'forward spam report message

				Call memo.Send( False, SPAMREPORTADDRESS1) 'forward spam report message

				

				Print "Successfully reported message " Cstr(i) + " of " + Cstr( col.Count ) +" ("+ doc.Subject(0) + ")" 

			Else

				Print "No MIME content found in message, not sending spam report. (the internet message may have been converted to Notes Format)"

			End If

			

			Call doc.Remove(True)

			Call ws.ViewRefresh

			

			Call sess.UpdateProcessedDoc( doc )

			i = i +1 'increment counter

		Wend

		

		Set uiview = ws.CurrentView

		Call uiview.DeselectAll 'to prevent UI bugs

		sess.ConvertMime = True 'revert mime conversion 

		

		Print "Done reporting " + Cstr( col.Count ) + " document(s) as Spam."

		

	End If

End If

End Sub

Subject: Lotusscript - close document

Thank you Paul for that quick response.Unfortunately, my boss(es) would prefer that it be done by marking the document as closed, as opposed to another message box coming up. I already tried a similar idea.

Using the following from the Help in Designer that saves the current document, refreshes the view (called “Main”), and closes the document, I’ve added this:

Sub Click(Source As Button)

’ Declare all variables

Dim workspace As New NotesUIWorkspace

Dim session As New NotesSession

Dim uidoc As NotesUIDocument

Dim view As NotesView

Dim db As NotesDatabase

'Set all variables

Set uidoc = workspace.CurrentDocument

Set db = session.CurrentDatabase

Set view = db.GetView( “Main” )

'Save current document, refresh “Main” view

'and close document

Call uidoc.Save

Call view.Refresh

Call workspace.ViewRefresh

Call uidoc.Close

End Sub

And tailored to what was already in there. If I am using the debugger correctly it is failing on the line Call uidoc.Save, with an “object variable not set”. What am I missing? Thanks in advance.

Sub Click(Source As Button)

'Notes Agent: name=ReportAsSpam, trigger=onEvent, Action menu selection, target=All selected docs

'forwards the selected message(s) with full header to a spam reporting email address

'moves message from inbox to trash, refreshes automatically and appends “Spam Submission >” to the subject line

'requires: Function walkmime (mime As notesmimeentity) As String

'm = Msgbox ("Is the email you are reporting currently open or in the preview window below? If it is, Cancel this message and close the email. Then try again.", 1, "Confirmation")

'If m=2 Then

'	Exit Sub

'End If

'Uncomment 4 lines above if messagebox is desired to prompt user to close email before clicking spam button



Const SPAMREPORTADDRESS = "xxx.net" 'abuse@global.sprint.com 'the email address used to report spam, uncomment to go live

Const SPAMREPORTADDRESS1 = "yyy.com" 'the email address used to report spam, uncomment to go live



Dim sess As New NotesSession

Dim ws As New NotesUIWorkspace

Dim uidoc As NotesUIDocument

Dim view As NotesView

Dim db As NotesDatabase



Dim memo As NotesDocument, doc As NotesDocument

Dim col As NotesDocumentCollection

Dim mime As NotesMIMEEntity

Dim rtitem As NotesItem

Dim temp As Variant 'to get info from non-ASCII data

Dim i As Integer 'to count messages



Dim uiview As NotesUIView 'to update the inbox after messages have been moved

Dim mail As NotesRichTextItem 'the sent message

Dim item As NotesItem 'to process fields with multiple items (i.e. recieved)

Dim res As Integer



sess.ConvertMime = False 'init mime conversion



Set db = sess.CurrentDatabase

Set uidoc = ws.CurrentDocument

Set db = sess.CurrentDatabase

Set view = db.GetView( "Main" )



Set col = db.UnprocessedDocuments

Set doc = col.GetFirstDocument()

If doc Is Nothing Then Exit Sub 'sanity check to abort early



Print "Reporting " + Cstr( col.Count ) + " selected documents as Spam to " + SPAMREPORTADDRESS + ". Please wait..."

i = 1



Set col = db.UnprocessedDocuments

If Not col Is Nothing Then

	Set doc = col.GetFirstDocument()

	If Not doc Is Nothing Then

		res = ws.Prompt (PROMPT_YESNO, "Alert", "Are you sure you want to report this email as spam?")

		If res =1 Then 'Executes if user select "Yes" to proceed

		Else

			Exit Sub ' Exits subroutine. Stops execution of code.

		End If

		While Not(doc Is Nothing) 

			Set memo = db.CreateDocument

			Set mail = New NotesRichTextItem( memo, "Body" )

			

			Set mime = doc.GetMIMEEntity

			If Not(mime Is Nothing) Then

				Forall header In mime.HeaderObjects

					Call mail.AppendText( walkmime ( mime ) ) 'calls required function walkmime

				End Forall

				Call doc.CloseMIMEEntities 'allow clean switch to using items

				

				If Instr( doc.Subject(0), "SPAM Submission" ) = 0 Then 

					Call doc.ReplaceItemValue("Subject", "SPAM Submission > " + doc.Subject(0) ) 'remove [spam]

				End If

				

				

				memo.subject = doc.subject(0)

				Call memo.Send( False, SPAMREPORTADDRESS) 'forward spam report message

				Call memo.Send( False, SPAMREPORTADDRESS1) 'forward spam report message

				

				Print "Successfully reported message " Cstr(i) + " of " + Cstr( col.Count ) +" ("+ doc.Subject(0) + ")" 

			Else

				Print "No MIME content found in message, not sending spam report. (the internet message may have been converted to Notes Format)"

			End If

			

			Call uidoc.Save

			Call view.Refresh

			Call ws.ViewRefresh

			Call uidoc.Close

			

			'Call ws.ViewRefresh

			Call doc.Remove(True)

			

			Call sess.UpdateProcessedDoc( doc )

			i = i +1 'increment counter

		Wend

		

		Set uiview = ws.CurrentView

		Call uiview.DeselectAll 'to prevent UI bugs

		sess.ConvertMime = True 'revert mime conversion 

		

		Print "Done reporting " + Cstr( col.Count ) + " document(s) as Spam."

		

	End If

End If

End Sub

Subject: Lotusscript - close document

Find the number eqivelant for the text error and use an error handling routine to catch the error, skip the doc(s), increment another counter (skippedDocs) and display it to the end user with a reason (x docs were skipped because they were open)

On Error 4241 Goto errHandler

your stuff here

errHandler:

your error handling routine here