Click button to copy document to another view - lotus-notes

I have two views which are Computer and Draft. I create a button in computer view and it is to create a copy of computer document to draft view.
Below is my button code. When I click the button, it said "Object variable not set"
Sub Click(Source As Button)
Dim session As New NotesSession
Dim db As NotesDatabase
Dim dc As NotesDocumentCollection
Dim doc As NotesDocument
Dim view As NotesView
Set db = session.CurrentDatabase
Set view = db.GetView( "Draft" )
Set doc = dc.GetFirstDocument()
Set dc = db.AllDocuments
While Not (doc Is Nothing)
Call doc.CopyToDatabase(db)
Set doc = dc.GetNextDocument(doc)
Wend
End Sub
Can anyone help me? And can I ask, do I need to insert any formula inside Draft view? Thanks for any help!
Update Question
I have found the problem and fixed my code. But when I click the button, it copies all document and shows on both views. How can I want a copy of the document in Draft only selected document? For example, the "Active" document only? Thanks!

Views do not "contain" documents. Documents are in a database, and views show selected documents, using a SELECT formula. If your SELECT matches all documents, all documents will show up. That formula decides which documents are part of the view.
If view A contains your document and view B doesn't, you have to adapt the SELECT formula in view B so that it will match the document.
E.g. if you want your current document to show up in view B, you could add a value to the current document, like DocumentViews, and set it to "B", and set the SELECT formula of view B to SELECT DocumentViews="B".
Now, if you want to do something with the document that's currently selected in a view, you can use the NotesDatabase.UnprocessedDocuments property. It contains a list of all selected documents.
Dim ns As New NotesSession
Dim db As NotesDatabase
Set db= ns.CurrentDatabase
Dim dc As NotesDocumentCollection
Set dc= db.UnprocessedDocuments
Dim doc As NotesDocument
Set doc= dc.GetFirstDocument
Dim newdoc As NotesDocument
Do Until doc Is Nothing
' you might have to check the status of the current document before copying...
Set newdoc= doc.CopyToDatabase(db)
Call newdoc.ReplaceItemValue("Status", "Draft")
Call newdoc.Save(True, False)
Set doc= dc.GetNextDocument(doc)
Loop

Related

Save copy document and change status field for copy document and original document using lotusscript button

I have a document, and copy of document. I use TagNo as unique ID for both documents.
I also have Status field to differentiate each document which is Active, Inactive, Draft, and Lock. I will explain below my document situation.
Below here are my document with two field; Tag No = PTagNo; Status = PStatus. The situation as below.
For Original document, the status is set Active. When copy is created, Original document will change to Lock, And Copy document status change to Draft. (For this I already achieve.)
After done editing, I will change status for Draft document and Original document. This happen when I save Draft document as "Complete". My Draft document will be Original document while my Original document will be Archived document. So for my Draft document, status will change to Active while Original Document, status will change to Inactive.(Not achieve yet).
I paste my save code as below.
Save and Complete
Sub Click(Source As Button)
Dim session As New NotesSession
Dim db As NotesDatabase
Dim workspace As New NotesUIWorkspace
Dim uidoc As NotesUIDocument
Dim doc As NotesDocument
Dim activeDoc As NotesDocument
Dim view As NotesView
Dim keys(1) As String
'// Set database and doc
Set db = session.CurrentDatabase
Set uidoc = workspace.CurrentDocument
Set doc = uidoc.Document
keys(0) = doc.PTagNo(0)
keys(1) = "Lock"
Set view = db.GetView("Computer")
vpswd = Inputbox$("Pls input code to save :")
If vpswd = "o" Then
Set activeDoc= view.GetDocumentByKey(keys, True)
If Not activeDoc Is Nothing Then
If activeDoc.PStatus(0) = "Lock" Then
activeDoc.DocumetId = doc.UniversalID
Call activeDoc.ReplaceItemValue("PStatus", "Inactive")
Call activeDoc.Save(True, False)
End If
End If
Call uidoc.FieldSetText("PStatus" , "Active")
Call uidoc.FieldSetText("SaveOptions" , "1")
Call uidoc.Save
Call uidoc.Close
Else
Msgbox "Wrong Code"
Exit Sub
End If
End Sub
So I use GetDocumentByKey for field ptagno but it show error "Object variable not set". Did I use wrong function?. Any help will be appreciated. Thanks!
The variable ptagno has not been set - therefore the "Object variable not set" error. You need to read the value from the field PTagNo and assign it to the ptagno variable - or use it directly. For instance like this:
Set activeDoc= view.GetDocumentByKey(uidoc.FieldGetText("PTagNo"))

Lotus Notes UnprocessedDocuments on embedded view

i have a doc with embedded view... in this embedded view i have a button that compile some code.
Inside this code i have Set Coll=db.unprocesseddocuments instructioin so when i select document inside this embedded view i think the collecion is composed by the document that i have select.
But the collecion contains the current uiwork documents instead the document that i have select inside the embedded view. Why ? How can i populate the collection with the selected document of the embedded view ?
With #command([toolsrunmancro];"MyAgent") instead the simply code inside the button the result is the same...
Someone can help me ?
This example works for me:
Action button "Test" in view (LotusScript)
Sub Click(Source As Button)
Dim session As New NotesSession
Dim db As NotesDatabase
Dim col As NotesDocumentCollection
Dim doc As NotesDocument
Set db = session.CurrentDatabase
Set col = db.UnprocessedDocuments
Set doc = col.GetFirstDocument
While Not doc Is Nothing
Print doc.UniversalID
Set doc = col.GetNextDocument(doc)
Wend
End Sub
The Embedded View properties are
After selecting documents in embedded view a click on action button "Test" prints all selected documents Universal ids.
I think the key to Knut's answer is that the button in question must be an action button within the embedded view, and not a button on the form.

Lotus Notes: Is it possible to create a view that excludes documents in all folders other than Inbox?

I am aware that you can exclude certain folders - but you have to name them. I need a view to show only the documents in the inbox, that doesn't need updating everytime we create a new folder.
Any help would be greatly appreciated!
:)
It is not possible directly. There are no formulas that would help you build a select statement to get documents that are only in the Inbox. However, you could have an agent run on a scheduled basis (maybe every 5-10 minutes) that would update documents and flag them if they are in the inbox. Your view would then just need to select documents that have that flag set.
Updated As umeli pointed out, the flag needs to be unset when documents are moved out of the Inbox. Here's a modified script:
For example:
Dim s as New NotesSession
Dim db as NotesDatabase
Dim view as NotesView
Dim doc as NotesDocument
Dim allEntriesInbox as NotesViewEntryCollection
Dim allEntriesFlagged as NotesViewEntryCollection
Set s = New NotesSession
Set db = s.CurrentDatabase
Set view = db.GetView("($Inbox)")
Set viewFlagged = db.GetView("IsInInboxView")
Set allEntriesInbox = view.AllEntries
Set allEntriesFlagged = viewFlagged.AllEntries
allEntriesFlagged.StampAll("IsInInbox", "")
allEntriesInbox.StampAll("IsInInbox, "1")
Your view (named "IsInInboxView" in this example) should have a selection formula of IsInInbox = "1"

Lotus notes - refering to a doc. from a NotesDocumentCollection

I have a view which on top there is a button called Delete
I want to delete all the selected documents from the view; for this I used:
Dim session As New NotesSession
Dim workspace As New NotesUIWorkspace
Dim database As NotesDatabase
Dim documentCollection As NotesDocumentCollection
Set database=session.CurrentDatabase
Set documentCollection=database.UnprocessedDocuments
If documentCollection.Count=0 Then
Msgbox "No documents selected ",,"warning"
Else
userChoice=Msgbox ("Delete" & Cstr(documentCollection.Count) & " documents?",64+100, _
"Confirm...")
If userChoice=6 Then
Call documentCollection.RemoveAll(True)
Call workspace.ViewRefresh
End if
But, what if I want to delete only some docs ( from all selected docs. from view ) which have let say Value = YES where Value is a text field inside the document?
I tried declaring:
Dim ui As NotesUIDocument
Dim doc As NotesDocument
Set doc=ui.document
But I get the message: Object variable not set. So I guess I have to refer to a document using the NotesDocumentCollection? How?
Thanks for your time!
Your Error Message has nothing to do with your question... The Error Message comes from setting doc form an unitialized uidoc. You need to have a Set ui = ws.CurrentDocumentsomewhere in your code, and of course declare ws: Dim ws as New NotesUIWorkspace
But for your question you don't need an ui- document at all. To delete just some of the selected documents, you cycle through the collection and delete just the documents that match your criteria:
Dim doc as NotesDocument
Dim nextDoc as NotesDocument
Set doc = documentCollection.GetFirstDocument()
While not doc is Nothing
Set nextDoc = documentCollection.GetNextDocument(doc)
if doc.GetItemValue( "Value" )(0) = "Yes" then
call doc.Remove(True)
end if
Set doc = nextDoc
Wend
Or you reduce the collection to just contain the documents that match your criteria and then delete the whole collection:
Call documentCollection.FTSearch("[Value] = Yes",0)
Call documentCollection.RemoveAll()
But take care: The collection is reduced with a FTSearch, that might also get "Yes of course" or "Ye" depending on the setting of the FT Index of the database -> Not very reliable.
You need to loop through the documents in the document collection and then process them individually. Here's an example loop that uses your documentCollection variable:
Dim doc As NotesDocument
Set doc = documentCollection.GetFirstDocument
While Not(doc Is Nothing)
' Do stuff
' Get next document
Set doc = documentCollection.GetNextDocument(doc)
Wend

How can I identify the document created last using Lotusscript from a collection of documents that share the same document number?

I have a database that contains about 20,000 documents. One of the views is categorized by Document Number and sorted such that the document created last is the first document in each category. Some categories (document numbers) only have on document associated with it, but others have multiply documents associated with it. I would like to identify the document created last in each category and write to a field indentifying it as the latest revision. I thought this would be easy, but, I'm having difficulties. Any help would be appreciated.
MJ
It may be simple assuming you do have a view, as you say, that is sorted such that the document created last is the first document within each category. In that case if you were to traverse that view you would simply need to retrieve the first document after each category, and set a value on one of the document's items.
For example,
Dim s as New NotesSession
Dim db as NotesDatabase
Dim view as NotesView
Dim nav As NotesViewNavigator
Dim viewEntry as NotesViewEntry
Dim docEntry as NotesViewEntry
Dim doc as NotesDocument
Set db = s.CurrentDatabase
Set view = db.GetView("My Categorized and Sorted View")
Set nav = view.CreateViewNav
Set viewEntry = nav.GetFirst ' Should be your first category
While Not (viewEntry Is Nothing)
Set docEntry = nav.GetNextDocument(viewEntry) 'The first document entry after the category
Set doc = docEntry.Document
doc.ReplaceItemValue("Some item", "This is the latest doc")
doc.Save(false, false)
Set viewEntry = nav.GetNextCategory(viewEntry) 'Jump to the next category
Wend

Resources