问题
I’m trying to export all of the documents and their attachments from a Lotus Notes database (with Designer 7.0). I can get the document data and can get an attachment, but only if I hard code the name. The two methods, in LotusScript, I’ve found for getting the filename programmatically aren’t working, as shown in the lower two code blocks. In the first, doc.GetFirstItem( "Body" ) returns Nothing, and in the second, there’s a Type Mismatch during execution on the Forall line. Any help on how to extract the attachments would be greatly appreciated! I’m not sure whether the attachments are stored as “attachments” or OLE, but I suspect as attachments, since they’re primarily PDFs.
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
回答1:
that will give you list of all attachments in document
objects = Evaluate("@AttachmentNames", doc)
回答2:
I see a few issues with your code. First of all, like others already said, you need to add error handling. Second, use the Source property of the NotesEmbeddedObject class to get the original file name of the attachment. You have to write code yourself to handle duplicates, of course.
Here are a couple of lines in a program I have written.
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
The program will export all documents in a database as XML, detach any attachments, export any embedded images and also link those detached/exported files to the XML files. You can find out more about it here: http://www.texasswede.com/websites/texasswede.nsf/Page/Notes%20XML%20Exporter
回答3:
I noticed you didn't quote whcih line caused the error. Best addecrror trapping to get info on which line the error occurs :
at top of procedure (1st line?) ..
Sub Click(Source As Button)
On error goto handler
At bottom of sub ...
fin:
exit sub
handler:
msgbox "Error " & Error$ & " line " & Erl
resume fin
End Sub
Helps a lot whe diagnosing issues.
回答4:
I'm new to Notes so thanks for all the input on making the code better and for solutions! I ran a quick test with the below code and found it works on at least one document (TBD on the whole database). I found that the filename is stored in Items, in the $FILE item. This code gets the filename then extracts the file:
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)
来源:https://stackoverflow.com/questions/18991147/how-to-retrieve-lotus-notes-attachments