Copy documents from a view to other database and remove it

时间:2015-11-25 10:22:46

标签: lotus-notes lotusscript lotus

I want to transfer the docs from a view in a database to other view in other database, so i have to copy and then to remove the docs, because the only option that notesdocument has is copytodatabase.

So i have this code:

Option Public
Option Declare

Sub Initialize()

Dim session As New NotesSession
Dim db As NotesDatabase
Set db = session.CurrentDatabase
Dim dbB As New NotesDatabase(db.Server,"Desarrollo\Formular_CL02.nsf")
Dim vwA As NotesView
Dim vwB As NotesView
Dim docA As NotesDocument
Dim docB As NotesDocument

'Open the database
If Not (dbB.isopen) Then
    Call dbB.open(db.Server,"Desarrollo\Formular_CL02.nsf")
End If

'Get the views
Set vwA = db.getView( "TestDevelop" )
Set vwB = dbB.getView( "TestDevelop" )

Set docA = vwA.GetFirstDocument 
Do While Not( docA Is Nothing )
    If docB Is Nothing Then
        Call docA.CopyToDatabase(dbB)
        Call docA.Remove(True)
    End If
    Set docA = vwA.GetNextDocument(docA) 
Loop

End Sub

When i execute the agent at the end it shows me an error:

Function requires a valid ADT argument

If i remove the line about Call docA.Remove(True) the agent will copy all documents without error.

Any advice?

Thanks a lot!

2 个答案:

答案 0 :(得分:3)

您删除了docA,然后您无法获取下一个文档。

只需使用另一个" docNext"保持信息:

Dim docNext as NotesDocument

Set docA = vwA.GetFirstDocument 
Do While Not( docA Is Nothing )
    Set docNext = vwA.GetNextDocument(docA) 

    If docB Is Nothing Then
        Call docA.CopyToDatabase(dbB)
        Call docA.Remove(True)
    End If
    Set docA = docNext
Loop

此外,最好在代码中始终使用错误处理程序来获取有关错误的最少信息:

第一行代码(直接在End Sub行之前):

On Error Goto ErrorHandler

代码结束:

EndSub:
  Exit Sub
ErrorHandler: 
  Print Err & ", " & Error & " in line " & erl
  Resume EndSub

您可以替换" print"通过消息框或发送电子邮件/写日志文件,无论如何。但至少你知道错误号,错误文本和错误行......

答案 1 :(得分:2)

错误发生在行Set docA = vwA.GetNextDocument(docA)中,因为您已经删除了docA,并且它不能再用作参数。

将您的代码更改为:

Do While Not( docA Is Nothing )
    Set docTemp = vwA.GetNextDocument(docA) 
    Call docA.CopyToDatabase(dbB)
    Call docA.Remove(True)
    Set docA = docTemp  
Loop