Link to home
Start Free TrialLog in
Avatar of GessWurker
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("\\nycweb04\TextBases\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(DataDirectory & "\\PDFs\\" & shortname & ".pdf")
                  Call object.Remove
                  Call doc.Save( True, True )
            End If
            Set doc = view.GetNextDocument( doc )
      Wend
End Sub
Avatar of Sjef Bosman
Sjef Bosman
Flag of France image

Hi GessWurker,

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
Avatar of GessWurker
GessWurker

ASKER

Sorry. I almost never work in lotusscript. Can you spell out exactly the changes I need to make.

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
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.
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("\\nycweb04\TextBases\Reports\",True)
     
     Set doc = view.GetFirstDocument  
     
     Do Until doc Is Nothing
          Set rtitem= doc.GetFirstItem("YourRichTextField")
          If Not(rtitem Is Nothing) Then ' should check type as well...
                 shortname = doc.IdNum(0)
                 Forall object In rtitem.EmbeddedObjects
                       If object.Type=EMBED_ATTACHMENT 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(DataDirectory & "\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
So the file isn't called PDFtext indeed. Do you know its name, or is it the only attachment?
Only one attachment per record. Never more.
ASKER CERTIFIED SOLUTION
Avatar of Sjef Bosman
Sjef Bosman
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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("\\nycweb04\TextBases\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("PDFtext")
                End If
                Call rtitem.EmbedObject(EMBED_ATTACHMENT, "", DataDirectory & "\PDFs\" & shortname & ".pdf", "")
           End If
           Set doc = view.GetNextDocument( doc )
     Loop
End Sub

Two for the price of one eh? ;)
I didn't change True, True to True, False. Hmmm...
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?
Found the problem with the re-attachment script. This was missing:

Call doc.Save(True,False)
Thanks for all your help sjef!
I forgot the Save... me clumsy... Good you found it!

And thanks for the points. :)