【问题标题】:How to retrieve Lotus Notes attachments?如何检索 Lotus Notes 附件?
【发布时间】:2025-12-18 03:05:03
【问题描述】:

我正在尝试从 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

【问题讨论】:

  • Dim session..etc 是什么意思?
  • Body 字段是否存在于每个文档中?很奇怪,它什么也不返回。
  • 您绝对应该看看 LotusScript 中的错误处理技术。循环处理的文档可能会出现一些问题 - 随时。只是从头顶:不同的形式(没有正文字段),损坏的文档(.isValid),无效的文件名(内部附件名称可能与客户端中显示的不同)等等。
  • @zanbri:您需要声明您使用的 NotesObjects。任何 Notes 开发人员都会认出这些行,而您一直在编写它们。 :-)

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


【解决方案1】:

这将为您提供文档中所有附件的列表

objects =  Evaluate("@AttachmentNames", doc)

【讨论】:

    【解决方案2】:

    我发现您的代码存在一些问题。 首先,就像其他人已经说过的那样,您需要添加错误处理。 其次,使用 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

    【讨论】:

      【解决方案3】:

      我注意到您没有引用导致错误的行。最好的 addecrror 捕获以获取错误发生在哪一行的信息:

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

      Sub Click(Source As Button)  
      On error goto handler
      

      在子底部...

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

      结束子

      在诊断问题时有很大帮助。

      【讨论】:

        【解决方案4】:

        我是 Notes 新手,因此感谢所有关于改进代码和解决方案的意见!我使用以下代码进行了快速测试,发现它至少适用于一个文档(整个数据库上的待定)。我发现文件名存储在 $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)
        

        【讨论】:

        • 您不能假设 $File 是第一项。这并不总是正确的。