Genii Weblog
Calling Midas from VB
Wed 3 Dec 2003, 11:15 PM
Tweetby Ben Langhinrichs
I have frequently gotten asked whether it is possible to call our Midas Rich Text LSX from Visual Basic, or other places for that matter, using COM. I must admit that while I know customers have done it, I wasn't sure how myself. Someone even showed me a while back, and I forgot, so I finally sat down and figured it out again and am posting it here so I can't forget again. Now, before someone jumps down my throat, I don't think this is really COM. I think it is actually OLE, but whatever it is called, this allows you to take a script which uses Midas to read or modify rich text, and call that script from VB or another OLE client.
Existing Midas Script
Sub ExportDocumentToMSWord(filepath As String, noteid As String)
' *** Constants
Const EXPORT_DIRECTORY = "c:\export"
' *** Midas Rich Text LSX class objects
Dim rtitem As GeniiRTItem
Dim rtchunk As GeniiRTChunk
' *** Create the GeniiRTItem object
Set rtitem = New GeniiRTItem
' *** Connect to the document and export
Call rtitem.Connect("", filepath, noteid, "Body")
If Not rtitem.IsConnected Then Exit Sub
Call rtitem.ExportToHTML(EXPORT_DIRECTORY & "\MX" & noteid & ".doc", "HTML", |
CSSBorders='Yes'
AddNewlines=CRLF
AttachmentHandling='To Disk'
AttachmentDirectory='| & EXPORT_DIRECTORY & |'
AttachmentURLDirectory='| & EXPORT_DIRECTORY & |'
ImageHandling='To Disk'
ImageDirectory='| & EXPORT_DIRECTORY & |'
ImageURLDirectory='| & EXPORT_DIRECTORY & |'|)
Print "Exported to " & EXPORT_DIRECTORY & "\MN" & noteid & ".doc"
End Sub
Modified Midas Script
Sub ExportDocumentToMSWord(filepath As String, noteid As String)
' *** Constants
Const EXPORT_DIRECTORY = "c:\export"
' *** OLE server
Dim oWS As Object
' *** Midas Rich Text LSX class objects
Dim rtitem As Object
Dim rtchunk As Object
' *** Connect to Notes as an OLE server
Set oWS = CreateObject("Notes.NotesUIWorkspace")
' *** Load the LSX using its full pathname
oWS.UseLSX("c:\lotus\notes\nlsxrtc.dll")
' *** Create the GeniiRTItem object by calling its OLE name
Set rtitem = CreateObject("LSX.GeniiRTItem")
' *** Connect to the document and export
Call rtitem.Connect("", filepath, noteid, "Body")
If Not rtitem.IsConnected Then Exit Sub
Call rtitem.ExportToHTML(EXPORT_DIRECTORY & "\MX" & noteid & ".doc", "HTML", |
CSSBorders='Yes'
AddNewlines=CRLF
AttachmentHandling='To Disk'
AttachmentDirectory='| & EXPORT_DIRECTORY & |'
AttachmentURLDirectory='| & EXPORT_DIRECTORY & |'
ImageHandling='To Disk'
ImageDirectory='| & EXPORT_DIRECTORY & |'
ImageURLDirectory='| & EXPORT_DIRECTORY & |'|)
Print "Exported to " & EXPORT_DIRECTORY & "\MN" & noteid & ".doc"
Set rtitem = Nothing
Set oWS = Nothing
End Sub
Comparison using Midas' rich text comparison
Sub ExportDocumentToMSWord(filepath As String, noteid As String)
' *** Constants
Const EXPORT_DIRECTORY = "c:\export"
' *** OLE server
Dim oWS As Object
' *** Midas Rich Text LSX class objects
Dim rtitem As GeniiRTItemObject
Dim rtchunk As GeniiRTChunkObject
' *** Connect to Notes as an OLE server
Set oWS = CreateObject("Notes.NotesUIWorkspace")
' *** Load the LSX using its full pathname
oWS.UseLSX("c:\lotus\notes\nlsxrtc.dll")
' *** Create the GeniiRTItem object by calling its OLE name
Set rtitem = NewCreateObject("LSX.GeniiRTItem")
' *** Connect to the document and export
Call rtitem.Connect("", filepath, noteid, "Body")
If Not rtitem.IsConnected Then Exit Sub
Call rtitem.ExportToHTML(EXPORT_DIRECTORY & "\MX" & noteid & ".doc", "HTML", |
CSSBorders='Yes'
AddNewlines=CRLF
AttachmentHandling='To Disk'
AttachmentDirectory='| & EXPORT_DIRECTORY & |'
AttachmentURLDirectory='| & EXPORT_DIRECTORY & |'
ImageHandling='To Disk'
ImageDirectory='| & EXPORT_DIRECTORY & |'
ImageURLDirectory='| & EXPORT_DIRECTORY & |'|)
Print "Exported to " & EXPORT_DIRECTORY & "\MN" & noteid & ".doc"
Set rtitem = Nothing
Set oWS = Nothing
End Sub
Conclusion (and limitation)
So, there you have it. Change just a few lines and your script intended for LotusScript will run from Visual Basic, Excel, MS Word or wherever else you need it to run. Pretty cool, eh? (Also, a pretty simple example of how Midas's rich text comparison can show what has changed in a script)
There is one annoying limitation. This is a front end action, not a server based backend action (note the Notes.NotesUIWorkspaceto understand why). Still, for those more comfortable with VB, or for those who need to access some rich text in the middle of a script in MS Word, for example, this is an easy and effective way to use Midas outside of Lotus Notes/Domino.
Addendum
In order to make this work, you need to import the registry file attached here.
Copyright © 2003 Genii Software Ltd.
What has been said:
77.1. Tony (08/26/2004 09:10 AM)
how do you handle the event ActOnText in this example.
Thanks
Tony