Example How To Open LibreOffice Document As ReadOnly With Use Of OleObject

This Weekend I had some time to investigate why it was not possible with some OleObject code in Xojo to open a Libreoffice / OpenOffice document in Read Only mode. I found out, the issue is how the Boolean data type is represented as True (1) or False (0) in Xojo and the differences in VBScript. In Xojo the Boolean size is one byte and in vbscript the boolean size is 2 bytes, and True is -1 in VBScript. So the Boolean data type will never be passed on to LibreOffice Through the “Bridge Service” as it expect 2 bytes and True is -1.

To solve this issue LibreOffice / OpenOffice has what they call “The automation object” the details you can find here: ( objServiceManager.Bridge_GetValueObject() )

Below you will find an example how to represent Boolean value True:

  // This code open a Libreoffice Document in Read Only Mode
    Dim objServiceManager As OLEObject
    Dim objCoreReflection As OLEObject
    Dim objDesktop As OLEObject
    Dim objDocument As OLEObject
    Dim Aargs() As Variant
    Dim FileBP as OLEObject
    objServiceManager = New OLEObject("com.sun.star.ServiceManager", True)
    'Create the CoreReflection service that is later used to create structs
    objCoreReflection = objServiceManager.createInstance("com.sun.star.reflection.CoreReflection")
    'Create the Desktop
    objDesktop = objServiceManager.createInstance("com.sun.star.frame.Desktop")
    // More info can be found here https://wiki.openoffice.org/wiki/Documentation/DevGuide/ProUNO/Bridge/Value_Objects
    Dim MyBooleanTrue As Oleobject
    MyBooleanTrue = objServiceManager.Bridge_GetValueObject()
    MyBooleanTrue.Set("boolean", -1)
    FileBP.Name = "ReadOnly"  // Case Sensitive
    FileBP.Value  = MyBooleanTrue
    // Make sure you already have created a test docoument in c:/temp/test.odt
    objDocument= objDesktop.loadComponentFromURL("file:///c:/temp/test.odt", "_blank", 0, Aargs)
    objServiceManager = nil
    MyBooleanTrue = nil
  Catch E as OLEException
    MsgBox "open office error"