如何检索Lotus Notes附件?

时间:2013-09-24 20:11:28

标签: types lotus-notes attachment lotusscript type-mismatch

我正在尝试从Lotus Notes数据库(使用Designer 7.0)导出所有文档及其附件。我可以获取文档数据并获得附件,但前提是我对该名称进行了硬编码。在LotusScript中,我发现以编程方式获取文件名的两种方法都不起作用,如下面两个代码块所示。在第一个中,doc.GetFirstItem(“Body”)返回Nothing,而在第二个中,在Forall行上执行期间存在类型不匹配。 任何有关如何提取附件的帮助将不胜感激!我不确定附件是否存储为“附件”或OLE,但我怀疑是附件,因为它们主要是PDF。

Sub Click(Source As Button)  
Dim session As New NotesSession
Dim db As NotesDatabase
Dim query As String
Dim collection As NotesDocumentCollection
Dim doc As NotesDocument
Dim fileCount As Integer
Dim attachment As NotesEmbeddedObject 
Dim fileName As String

Set db = session.CurrentDatabase
' get a document that has an attachment
Set collection = db.FTSearch( "06/25/2013", 10 )

fileNum% = Freefile()
fileName$ = "c:\kcw\lotusexport.txt"
Open fileName$ For Output As fileNum%
Write #fileNum%, "docs found", collection.Count

Set doc = collection.GetFirstDocument
' write out document properties
Forall x In doc.Items
    Write #fileNum%, x.Name, " = ",  x.Text
End Forall
'extract document (using hardcoded name)
Set attachment = doc.GetAttachment("OCSE-FRONT_SCANTODESKTOP_06262013-104822.pdf")
Call attachment.ExtractFile _
( "c:\kcw\attachment" )

'Try to get attachment through "Body", but rtitem is Nothing
Set rtitem = doc.GetFirstItem( "Body" )
Write #fileNum%, "rtitem is Nothing", rtitem Is Nothing
fileCount = 0
If Not rtitem Is Nothing Then
    If ( rtitem.Type = RICHTEXT ) Then
        Write #fileNum%, "rtitem is RICHTEXT"
        Forall o In rtitem.EmbeddedObjects
            Write #fileNum%, "has Embedded Objects"
            fileCount = fileCount + 1
            Write #fileNum%,"rtitem num", fileCount
            Call o.ExtractFile _
            ( "c:\kcw\newfile" & Cstr(fileCount) )
        End Forall
    End If
End If

'Fails with "Type mismatch" at Forall loop
If doc.HasEmbedded Then
    Write #fileNum%, "doc has embedded"     
    Forall objects In doc.EmbeddedObjects
        Write #fileNum%, "in for loop"
        Write #fileNum%, "filename= ", object.Source
    End Forall
End If

Close fileNum%
End Sub

4 个答案:

答案 0 :(得分:4)

将为您提供文档

中所有附件的列表
objects =  Evaluate("@AttachmentNames", doc)

答案 1 :(得分:1)

我发现您的代码存在一些问题。 首先,像其他人已经说过的那样,您需要添加错误处理。 其次,使用NotesEmbeddedObject类的Source属性来获取附件的原始文件名。当然,您必须自己编写代码来处理重复项。

以下是我编写的程序中的几行。

Forall i In doc.Items
  ' *** Locate attachments and detach them
  If Left$(i.Name,1)<>"$" Or Lcase(i.Name)="$file" Then
    If i.IsSummary = False Then
      If Not Isempty(i.EmbeddedObjects) Then
        If ( i.Type = RICHTEXT ) Then
          Forall obj In i.EmbeddedObjects
            If ( obj.Type = EMBED_ATTACHMENT ) Then
              Call obj.ExtractFile(basePath & obj.Source)  
            End If
          End Forall
        End If
      End If
    End If
  End If
End Forall

程序将以XML格式导出数据库中的所有文档,分离所有附件,导出任何嵌入的图像,并将这些分离/导出的文件链接到XML文件。您可以在此处找到有关此内容的更多信息:http://www.texasswede.com/websites/texasswede.nsf/Page/Notes%20XML%20Exporter

答案 2 :(得分:0)

我注意到你没引用whcih行导致错误。最佳addecrror陷阱,以获取错误发生在哪一行的信息:

在程序顶部(第1行?)..

Sub Click(Source As Button)  
On error goto handler

在子...的底部

fin:
exit sub

handler:
msgbox "Error " & Error$ & " line " & Erl 
resume fin

End Sub

帮助诊断问题。

答案 3 :(得分:0)

我是Notes的新手,所以感谢所有关于使代码更好和输入解决方案的输入!我使用下面的代码运行了一个快速测试,发现它至少可以在一个文档上工作(整个数据库上的TBD)。我发现文件名存储在$ FILE项目的Items中。此代码获取文件名,然后提取文件:

Dim item As NotesItem
 'assumes first item is $FILE
Set item = doc.Items( 0 )
Dim attachmentName As String
attachmentName = item.Values(0)
Write #fileNum%, "attachmentName= ", attachmentName
Set attachment = doc.GetAttachment(attachmentName)
Call attachment.ExtractFile _
( "c:\kcw\" + attachmentName)
相关问题