Personal Blog of Thomas Hampel - Creative Mythbusting in Development and Collaboration

Who am I?

Feeds

Archives

April 2025 (1)
January 2025 (1)
December 2024 (1)
November 2024 (2)
October 2024 (2)
September 2024 (1)
July 2024 (1)
May 2024 (2)
April 2024 (3)
March 2024 (1)
February 2024 (2)
January 2024 (5)
December 2023 (3)
November 2023 (2)
October 2023 (1)
September 2023 (4)
June 2023 (1)
April 2023 (3)
March 2023 (1)
February 2023 (1)
July 2022 (1)
September 2021 (1)
August 2021 (2)
May 2021 (1)
February 2021 (3)
January 2021 (1)
November 2020 (1)
October 2020 (2)
September 2020 (2)
March 2020 (1)
November 2019 (1)
August 2019 (1)
July 2019 (1)
March 2019 (1)
December 2018 (1)
November 2018 (1)
October 2018 (1)
September 2018 (1)
May 2018 (1)
January 2018 (1)
December 2017 (1)
November 2017 (1)
September 2017 (1)
March 2017 (2)
February 2017 (5)
November 2016 (1)
September 2016 (4)
April 2016 (1)
March 2016 (7)
January 2016 (1)
December 2015 (1)
November 2015 (3)
August 2015 (1)
July 2015 (2)
June 2015 (5)
May 2015 (5)
March 2015 (3)
February 2015 (2)
January 2015 (4)
December 2014 (3)
November 2014 (1)
September 2014 (4)
August 2014 (1)
May 2014 (4)
April 2014 (1)
March 2014 (2)
February 2014 (3)
January 2014 (2)
October 2013 (1)
September 2013 (1)
August 2013 (2)
July 2013 (2)
March 2013 (2)
February 2013 (4)
January 2013 (3)
December 2012 (2)
November 2012 (1)
October 2012 (2)
September 2012 (4)
August 2012 (3)
July 2012 (1)
June 2012 (6)
May 2012 (1)
February 2012 (2)
January 2012 (1)
December 2011 (4)
November 2011 (2)
September 2011 (1)
May 2011 (2)
March 2011 (1)
January 2011 (1)
November 2010 (5)
October 2010 (2)
September 2010 (2)
August 2010 (1)
July 2010 (3)
June 2010 (1)

Exporting Notes Documents

Thomas Hampel
 2 October 2012

A customer wanted to have all attachments of some selected Notes document exported to the file system and also wanted to keep an option for developers to access the metadata of the original Notes document.
Nothing easier than that, so I wrote this small script to get the job done.


First the entire document is exported into DXL, then all attachments are detached to the file system. Both parts are not rocket science, but some people might want to reuse the code.
To avoid name conflicts while detaching files a folder is created for each Notes document so all attachments of this Notes document will be stored in this subfolder.


Option
Public
Option
Declare
Dim
gCounter&
Sub
Initialize
     
Dim s As New NotesSession
     
Dim coll As NotesDocumentCollection
     
Dim BasePath$

      BasePath$ =
InputBox ("Export data to path...: ", "Export", "C:\")
     
     
'# add backslash at the end
     
If right (BasePath$,1) <> "\" Then BasePath$ = BasePath$ & "\"
     
     
Print "Using BasePath : " & BasePath$
     
     
Set coll = s.currentdatabase.Unprocesseddocuments
     
If coll Is Nothing Then
             
MessageBox "No documents selected"
     
Else
             
Print "Processing " & coll.count & " documents..."
             
Call ExportToDXL (coll, BasePath$)
             
Call ExportToFile (coll, BasePath$)
             
MessageBox "Export completed."
     
End If        
End
Sub

Function
ExportToDXL (Coll As NotesDocumentCollection, BasePath As String)
     
Dim session As New NotesSession
     
Dim stream As NotesStream
     
Dim DXLfilename$
     
Dim doc As NotesDocument
     
Dim tdoc As NotesDocument
     
Dim exporter As NotesDXLExporter
     
     
If coll Is Nothing Then Exit function
     
Set doc = coll.getfirstdocument
     
While Not doc Is Nothing
             
Set tdoc = coll.getNextDocument (doc)
             
'# Open xml file named after current database
             
Set stream = session.CreateStream
              DXLfilename$ = BasePath$ & doc.universalid &
".dxl"
             
If Not stream.Open(DXLfilename$) Then
                     
MessageBox "Cannot open " & DXLfilename$,, "Error"
                     
Exit Function
             
End If
             
             
'# kick off the exporter process
             
Set exporter = session.CreateDXLExporter
             
Call exporter.SetInput(doc)
             
Call exporter.SetOutput(stream)
             
Call exporter.Process
             
             
Set doc = tdoc
     
Wend
End
Function

Function
ExportToFile (coll As NotesDocumentCollection, BasePath As String)
        On Error GoTo ErrH
        Dim doc As NotesDocument
        Dim tdoc As NotesDocument
        Dim rtitem As variant
        Dim targetpath$, fname$
        Dim FieldList(0) As String
        Dim oba As Variant
       
        '# define which fields to scan for attachments
        FieldList (0) = "Body"
       
        If coll Is Nothing Then Exit Function
       
        Set doc = coll.getfirstdocument
        While Not doc Is Nothing
                Set tdoc = coll.getNextDocument (doc)
                If doc.Hasembedded Then
                        targetpath$ = BasePath$ & doc.universalid & "\"
                       
                        If Dir$ (BasePath$ & doc.universalid, 16) = "" Then MkDir targetpath$
                       
                        '# loop list of fields
                        ForAll f In FieldList
                                 Set rtitem = doc.GetFirstItem(f)
                                 If Not rtitem Is Nothing Then
                                        If (rtitem.Type = RICHTEXT ) Then
                                                '# make sure the field contains some objects and detach
                                                If IsArray(rtitem.embeddedObjects) Then
                                                        ForAll o In rtitem.EmbeddedObjects
                                                                If ( o.Type = EMBED_ATTACHMENT ) Then
                                                                        Fname$=o.Name
                                                                        If FileExists (fname$) Then fname$ = CStr(gCounter&) & Fname$
                                                                        Call o.ExtractFile(targetPath$ & Fname$)
                                                                        gCounter& = gCounter& + 1
                                                                End If
                                                        End ForAll
                                                End If

                                        End If
                                End If
                        End ForAll
                End If
                Set doc = tdoc
        Wend
continue:
        Exit Function
       
errH:
        Stop
        Print "Error " & Err() & " in line " & Erl() & " - " & Error
        Resume continue
End Function
Comments [1]
Tagged with: Code Development Domino
Go ElsewhereSubscribe to RSSAboutStay ConnectedAnd More
Thomas Hampel, All rights reserved.