SDK - Registering OLE Objects as Attachments in TRIM

  • KM510209
  • 20-Sep-2008
  • 20-Sep-2008

Archived Content: This information is no longer maintained and is provided "as is" for your convenience.

Reference

Registering OLE Objects as Attachments in TRIM

Return to SDK Samples

Handling cases where documents are embedded within other documents can be tricky. TRIM Context has some in-built functionality to monitor and maintain relationships between the embedded document and its parent document, but sometimes it’s easier to extract the embedded document and save it as a separate attachment. The following code sample shows one way of doing this.

This code contribution came from a developer working with embedded objects in a Lotus environment.

:!: Note: The sample below is written in Lotus Script

'Export MS Office Library:

Option Public
Option Explicit

Sub Initialize
'** This agent will take all the selected documents in a database that uses
'** the basic Microsoft Office Document Library template and send them to
'** another database (in my case, I just used one based on the Doc Library
'** template). The issue is that MS Office docs in an Office Document Library
'** are stored as OLE embedded files, so they're hard to move from one doc
'** to another. This agent uses OLE to open the embedded files with their
'** default applications and save them to the file system, and then attach the
'** saved file to a new document in the new database. If there is no embedded
'** file, the entire rich-text Body field is copied over intact.
'**
'** Obviously, this will only work if the computer you're running this agent on
'** has MS Office installed. I have the code set up to run against "Selected
'** Documents" in a view, although you can easily change it to run against
'** all the docs in the database or some subset thereof.
'**
'** version 1.0 -- initial release
'** version 1.1 -- added an OLE command to keep Excel spreadsheets visible (for
'** some reason, they were getting saved as hidden documents otherwise)
'**
'** Julian Robichaux -- http://www.nsftools.com

On Error Goto processError

'** variables relating to this database (this database should be the MS Office Library)
Dim session As New NotesSession
Dim db As NotesDatabase
Dim dc As NotesDocumentCollection
Dim doc As NotesDocument
Dim rtitem As NotesRichTextItem
Dim oleObj As Variant

Set db = session.CurrentDatabase
Set dc = db.UnprocessedDocuments
Set doc = dc.GetFirstDocument

'** variables relating to the database we're exporting to
Dim exportDbName As String
Dim exportDb As NotesDatabase
Dim newDoc As NotesDocument
Dim body As NotesRichTextItem
Dim tempDir As String
Dim fileCount As Integer
Dim fileName As String
Dim fname As String

'** MODIFY THESE TWO STRINGS FOR YOUR OWN USE
exportDbName = "JPR\Doc Lib Test.nsf"
tempDir = "C:\windows\temp\"

'** try to open the database we're exporting to
Set exportDb = session.GetDatabase("", exportDbName, False)
If (exportDb Is Nothing) Then
Print "Cannot open export database: " & exportDbName
Exit Sub
End If

'** try to export all the selected docs
Do Until (doc Is Nothing)
Set newDoc = New NotesDocument(exportDb)
newDoc.Form = "Document"
newDoc.Categories = doc.Categories
newDoc.WebCategories = doc.Categories
newDoc.Subject = doc.Subject(0) & " (created " & Datevalue(doc.Created) & ")"

Set rtitem = doc.GetFirstItem("Body")
Set body = New NotesRichTextItem(newDoc, "Body")
fileCount = 0
fileName = ""

If Not (rtitem Is Nothing) Then
If Not (Isempty(rtitem.EmbeddedObjects)) Then
Forall o In rtitem.EmbeddedObjects
If (o.Type = EMBED_OBJECT) Then
'** if we have an embedded object in the rich text field,
'** we'll try to save it as a file and attach it to our new doc
fileCount = fileCount + 1
fileName = tempDir & "detachedOleFile" & fileCount

'** for MS Office documents, this normally works (of course,
'** you need to make sure that a proper version of Office is
'** installed on the computer you're doing this on), although
'** you'll often get an error or two as you call these methods,
'** due to variations in the different Office object models
Set oleObj = o.Activate(False)
'** this is so Excel spreadsheets don't end up being hidden
oleObj.Application.Windows(oleObj.Application.Windows.Count).Visible = True