GessWurker
asked on
Export file attachments from view
I'm trying to use slightly-modified version of a script I found at https://www.experts-exchange.com/questions/20559885/lotus-notes-attachment-and-export.html to export PDF file attachments from a view. Unfortunately, it's not working. All I'm getting is a status message: "No PDF attached".
Here's what I'm working with. Anybody see an obvious (or not so obvious) problem here?
Sub Initialize
Dim Sess As New NotesSession
Dim db As NotesDatabase
Dim doc As NotesDocument
Dim DataDirectory, shortname As String
Dim view As NotesView
Dim object As NotesEmbeddedObject
Set db = sess.CurrentDatabase
'pick view from database
Set view = db.GetView("RARPTSRCHV")
DataDirectory = Sess.GetEnvironmentString( "\\nycweb0 4\TextBase s\Reports\ ",True)
Set doc = view.GetFirstDocument
While Not (doc Is Nothing)
shortname = doc.IdNum(0)
Set object = doc.GetAttachment( "PDFText" ) '<-- PDFText is an editable rich text field
If (object Is Nothing) Then
Print "No PDF attached" '<-- that's all I'm getting back
Else
'Replace independent with a subdirectory of the DATA directory
Call object.ExtractFile(DataDir ectory & "\\PDFs\\" & shortname & ".pdf")
Call object.Remove
Call doc.Save( True, True )
End If
Set doc = view.GetNextDocument( doc )
Wend
End Sub
Here's what I'm working with. Anybody see an obvious (or not so obvious) problem here?
Sub Initialize
Dim Sess As New NotesSession
Dim db As NotesDatabase
Dim doc As NotesDocument
Dim DataDirectory, shortname As String
Dim view As NotesView
Dim object As NotesEmbeddedObject
Set db = sess.CurrentDatabase
'pick view from database
Set view = db.GetView("RARPTSRCHV")
DataDirectory = Sess.GetEnvironmentString(
Set doc = view.GetFirstDocument
While Not (doc Is Nothing)
shortname = doc.IdNum(0)
Set object = doc.GetAttachment( "PDFText" ) '<-- PDFText is an editable rich text field
If (object Is Nothing) Then
Print "No PDF attached" '<-- that's all I'm getting back
Else
'Replace independent with a subdirectory of the DATA directory
Call object.ExtractFile(DataDir
Call object.Remove
Call doc.Save( True, True )
End If
Set doc = view.GetNextDocument( doc )
Wend
End Sub
ASKER
Sorry. I almost never work in lotusscript. Can you spell out exactly the changes I need to make.
Thanks!
Thanks!
The call is supposedly returning the attachment, according to the Help. Because one doesn't have enough control, I never use this call. If you know the RT-item the attachment is in, you can walk the EmbeddedObjects of the RT-item (there may be more). By the way, are you SURE the attachment is always called "PDFText"?
Furthermore, the backslash is not a reserved character in LotusScript
Furthermore, the backslash is not a reserved character in LotusScript
ASKER
Here's what I can tell you. When the PDFs are attached, the RT field PDFText is the target, and the file are attached via notes front end, not the web.
However, when I look at the doc properties of a record in a view, I see the file attachment properties only in $FILE and I see nothing in the PDFText field.
However, when I look at the doc properties of a record in a view, I see the file attachment properties only in $FILE and I see nothing in the PDFText field.
There is a very good Example in the Domino Designer database, under EmbeddedObjects property.
Some reworking has been done, try it, not tested, so no guarantees...
Sub Initialize
Dim Sess As New NotesSession
Dim db As NotesDatabase
Dim doc As NotesDocument
Dim shortname As String
Dim DataDirectory As String
Dim view As NotesView
Dim rtitem As Variant
Dim object As NotesEmbeddedObject
Set db = sess.CurrentDatabase
'pick view from database
Set view = db.GetView("RARPTSRCHV")
DataDirectory = Sess.GetEnvironmentString( "\\nycweb0 4\TextBase s\Reports\ ",True)
Set doc = view.GetFirstDocument
Do Until doc Is Nothing
Set rtitem= doc.GetFirstItem("YourRich TextField" )
If Not(rtitem Is Nothing) Then ' should check type as well...
shortname = doc.IdNum(0)
Forall object In rtitem.EmbeddedObjects
If object.Type=EMBED_ATTACHME NT Then ' 1454
If object.name="PDFText" Then ' case sensitive!
' Set object = doc.GetAttachment( "PDFText" ) '<-- PDFText is an editable rich text field
'Replace independent with a subdirectory of the DATA directory
Call object.ExtractFile(DataDir ectory & "\PDFs\" & shortname & ".pdf")
Call object.Remove
Call doc.Save( True, True )
Exit Forall
End If
End If
End Forall
End If
Set doc = view.GetNextDocument( doc )
Loop
End Sub
Some reworking has been done, try it, not tested, so no guarantees...
Sub Initialize
Dim Sess As New NotesSession
Dim db As NotesDatabase
Dim doc As NotesDocument
Dim shortname As String
Dim DataDirectory As String
Dim view As NotesView
Dim rtitem As Variant
Dim object As NotesEmbeddedObject
Set db = sess.CurrentDatabase
'pick view from database
Set view = db.GetView("RARPTSRCHV")
DataDirectory = Sess.GetEnvironmentString(
Set doc = view.GetFirstDocument
Do Until doc Is Nothing
Set rtitem= doc.GetFirstItem("YourRich
If Not(rtitem Is Nothing) Then ' should check type as well...
shortname = doc.IdNum(0)
Forall object In rtitem.EmbeddedObjects
If object.Type=EMBED_ATTACHME
If object.name="PDFText" Then ' case sensitive!
' Set object = doc.GetAttachment( "PDFText" ) '<-- PDFText is an editable rich text field
'Replace independent with a subdirectory of the DATA directory
Call object.ExtractFile(DataDir
Call object.Remove
Call doc.Save( True, True )
Exit Forall
End If
End If
End Forall
End If
Set doc = view.GetNextDocument( doc )
Loop
End Sub
So the file isn't called PDFtext indeed. Do you know its name, or is it the only attachment?
ASKER
Only one attachment per record. Never more.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Uh oh. How to reverse the process? Should not have included "Call object.Remove" I only wanted to export NOT remove.
Now I've got all the pdfs in a directory. Either I re-attach them or I point notes to the right place. Please advise. Is it relatively easy to re-attach? The file names line up with the IdNum field + .pdf.
Now I've got all the pdfs in a directory. Either I re-attach them or I point notes to the right place. Please advise. Is it relatively easy to re-attach? The file names line up with the IdNum field + .pdf.
Oh dear, how sad, never mind... Two for the price of one eh? ;)
Reattach, using the known name and directory, and a call to EmbedObject. Did you change the True, True to True, False? if not, you should still have the original documents.
Again: untested...
Sub Initialize
Dim Sess As New NotesSession
Dim db As NotesDatabase
Dim doc As NotesDocument
Dim shortname As String
Dim DataDirectory As String
Dim view As NotesView
Dim rtitem As Variant
Dim object As NotesEmbeddedObject
Set db = sess.CurrentDatabase
'pick view from database
Set view = db.GetView("RARPTSRCHV")
DataDirectory = Sess.GetEnvironmentString( "\\nycweb0 4\TextBase s\Reports\ ",True)
Set doc = view.GetFirstDocument
Do Until doc Is Nothing
shortname = doc.IdNum(0)
If Dir$(DataDirectory & "\PDFs\" & shortname & ".pdf")<>"" Then ' file exists
Set rtitem= doc.GetFirstItem("PDFtext" )
If rtitem Is Nothing Then
Set rtitem= doc.CreateRichTextItem("PD Ftext")
End If
Call rtitem.EmbedObject(EMBED_A TTACHMENT, "", DataDirectory & "\PDFs\" & shortname & ".pdf", "")
End If
Set doc = view.GetNextDocument( doc )
Loop
End Sub
Two for the price of one eh? ;)
Reattach, using the known name and directory, and a call to EmbedObject. Did you change the True, True to True, False? if not, you should still have the original documents.
Again: untested...
Sub Initialize
Dim Sess As New NotesSession
Dim db As NotesDatabase
Dim doc As NotesDocument
Dim shortname As String
Dim DataDirectory As String
Dim view As NotesView
Dim rtitem As Variant
Dim object As NotesEmbeddedObject
Set db = sess.CurrentDatabase
'pick view from database
Set view = db.GetView("RARPTSRCHV")
DataDirectory = Sess.GetEnvironmentString(
Set doc = view.GetFirstDocument
Do Until doc Is Nothing
shortname = doc.IdNum(0)
If Dir$(DataDirectory & "\PDFs\" & shortname & ".pdf")<>"" Then ' file exists
Set rtitem= doc.GetFirstItem("PDFtext"
If rtitem Is Nothing Then
Set rtitem= doc.CreateRichTextItem("PD
End If
Call rtitem.EmbedObject(EMBED_A
End If
Set doc = view.GetNextDocument( doc )
Loop
End Sub
Two for the price of one eh? ;)
ASKER
I didn't change True, True to True, False. Hmmm...
ASKER
Can't get the file attachment script to work. It doesn't report any problems, and I see the little lightning bolt, but nothing happens. Any ideas?
ASKER
Found the problem with the re-attachment script. This was missing:
Call doc.Save(True,False)
Call doc.Save(True,False)
ASKER
Thanks for all your help sjef!
I forgot the Save... me clumsy... Good you found it!
And thanks for the points. :)
And thanks for the points. :)
Since Notes3 (I think), most attachments are placed in a RichText item, and no longer in the document itself. You should get that item, and then look for embedded objects.
> Dim DataDirectory, shortname As String
N.B. Defines DataDirectory as a Variant
Cheers!
Sjef