Reinhaa
Reinhaa

Reputation: 195

Click button to copy document to another view

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!

Upvotes: 0

Views: 156

Answers (1)

D.Bugger
D.Bugger

Reputation: 2359

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

Upvotes: 1

Related Questions