OOBASIC Macro and VB .NET

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
openjack
Posts: 4
Joined: Fri Apr 04, 2008 3:56 pm

OOBASIC Macro and VB .NET

Post by openjack »

Hi all,

I'm try to explain my problem, just for check if I'm following the better way to solve it.

My VB .NET procedure opens a document with Writer, but I need to hide some functions such as Print or SaveAs. I'm able to hide some toolbar buttons ceating a new ooBasic macro, but I can't do it from VB .NET; for this reason I need to launch a ooBasic macro from VB .NET

It's the better way or there is another way?


So, I put here a few lines of code.

The following OOBASIC MACRO code is able to hide some tool bar's buttons.

Code: Select all

Sub Main
     REM *** Initialize strings
     sToolbar = "private:resource/toolbar/standardbar"
     sCmdId   = ".uno:Save"
       
     REM *** Retrieve the desktop service
     oDesktop = createUnoService("com.sun.star.frame.Desktop")

     REM *** Retrieve the current frame and layout manager
     oCurrFrame = oDesktop.getCurrentFrame()
     oLayoutManager = oCurrFrame.LayoutManager

     REM *** Try to retrieve the toolbar from the layout manager
     oToolbar = oLayoutManager.getElement( sToolbar )
       
     REM *** Retrieve settings from toolbar ***
     oToolbarSettings = oToolbar.getSettings( true )
       
     index = -1
     nCount = oToolbarSettings.getCount()
     for i = 0 to nCount-1
         oToolbarButton() = oToolbarSettings.getByIndex( i )
         nToolbarButtonCount = ubound(oToolbarButton())
         for j = 0 to nToolbarButtonCount
             if oToolbarButton(j).Name = "CommandURL" then
                 if oToolbarButton(j).Value = sCmdId then
                     index = i
                 end if
             endif
         next j
     next i

     if index <> -1 then
         REM *** Retrieve current Persistent state
         REM *** from property
         bPersistent = oToolbar.Persistent
       
         REM *** To make our changes non-persistent
         REM *** we have to set the Persistent property
         REM *** to false
         oToolbar.Persistent = false
       
         REM *** Retrieve button settings
         oButtonSettings = oToolbarSettings.getByIndex( index )
               
         REM *** Change the visibility property of the button
         for j = 0 to ubound(oButtonSettings())
             if oButtonSettings(j).Name = "IsVisible" then
                 oButtonSettings(j).Value = FALSE
             endif
         next j
               
         REM *** Replace button settings
         oToolbarSettings.replaceByIndex( index, oButtonSettings )

         REM *** Set new settings at our toolbar
         oToolbar.setSettings( oToolbarSettings )

         REM *** Reset Persistent property to old value
         oToolbar.Persistent = bPersistent
      end if
       
End Sub 

Now... this VB .NET code is able to open an Open Office document.

Code: Select all

        Dim oSM                   'Root object for accessing OpenOffice from VB
        Dim oDesk, oDoc As Object 'First objects from the API
        Dim arg()                 'Ignore it for the moment !
        Dim OpenPar(1) As Object 'a Visual Basic array, with 3 elements
        OpenPar(0) = MakePropertyValue("ReadOnly", False)
        OpenPar(1) = MakePropertyValue("Password", "secret")

        'Instanciate OOo : this line is mandatory with VB for OOo API
        oSM = CreateObject("com.sun.star.ServiceManager")
        'Create the first and most important service
        oDesk = oSM.createInstance("com.sun.star.frame.Desktop")

        'Open an existing doc (pay attention to the syntax for first argument)
        oDoc = oDesk.loadComponentFromURL("file:///c:/doc.sxw", "_blank", 0, OpenPar)


How can I merge these two code snippet?

thank u!


EDIT to MODERATORS: Sorry, I've inserted this topic into another forum too.
ms777
Volunteer
Posts: 177
Joined: Mon Oct 08, 2007 1:33 am

Re: OOBASIC Macro and VB .NET

Post by ms777 »

Hi,

the code to launch an OO Basic function stored in the document oDoc is in OO Basic

Code: Select all

sUrl = "vnd.sun.star.script:Standard.Module1.Test?language=Basic&location=document"
oScript = oDoc.getScriptProvider("").getScript(sUrl)
oScript.invoke(Array(), Array(), Array())
In the first Array passed to invoke you can put the arguments to the OOBasic function, if required

Good luck with translating to VB.NET,

ms777
openjack
Posts: 4
Joined: Fri Apr 04, 2008 3:56 pm

Re: OOBASIC Macro and VB .NET

Post by openjack »

Hi, thank you very much for your reply.

I'm trying to convert your code to VB .NET first, because I need like that, and second because when I install your code into a macro, I had an error, the openoffice doesn't know "oDoc" object.


So, this is the VB .NET code:

Code: Select all

        Dim oSM                   'Root object for accessing OpenOffice from VB
        Dim oDesk, oDoc As Object 'First objects from the API
        Dim arg()                 'Ignore it for the moment !
        Dim OpenPar(1) As Object 'a Visual Basic array, with 3 elements
        OpenPar(0) = MakePropertyValue("ReadOnly", False)
        OpenPar(1) = MakePropertyValue("Password", "secret")

        'Instanciate OOo : this line is mandatory with VB for OOo API
        oSM = CreateObject("com.sun.star.ServiceManager")
        'Create the first and most important service
        oDesk = oSM.createInstance("com.sun.star.frame.Desktop")

        'Open an existing doc (pay attention to the syntax for first argument)
        oDoc = oDesk.loadComponentFromURL("file:///c:/doc.sxw", "_blank", 0, OpenPar)

        sUrl = "vnd.sun.star.script:Standard.Module1.Main?language=Basic&location=document"
        oScript = oDoc.getScriptProvider("").getScript(sUrl)
        oScript.invoke(arg, arg, arg)

and I have this error on this line "oScript = oDoc.getScriptProvider("").getScript(sUrl)"

Code: Select all

"System.Runtime.InteropServices.COMException" in microsoft.visualbasic.dll

Information: [automation bridge] unexpected exception in UnoConversionUtilities<T>::variantToAny !

Some idea? :-)

thank u!
ms777
Volunteer
Posts: 177
Joined: Mon Oct 08, 2007 1:33 am

Re: OOBASIC Macro and VB .NET

Post by ms777 »

Hi,

can you replace

Code: Select all

oScript = oDoc.getScriptProvider("").getScript(sUrl)
by

Code: Select all

oProv = oDoc.getScriptProvider("")
oScript = oProv.getScript(sUrl)
and check in which line the eror occurs ?
openjack
Posts: 4
Joined: Fri Apr 04, 2008 3:56 pm

Re: OOBASIC Macro and VB .NET

Post by openjack »

Hi,

I did that:

Code: Select all

oProv = oDoc.getScriptProvider("")
oScript = oProv.getScript(sUrl)
The same error is on the first line ( oProv = oDoc.getScriptProvider("") ).

So, I've tried to remove the symbol between the brackets ()

Code: Select all

oProv = oDoc.getScriptProvider[color=#FF0000]()[/color]
oScript = oProv.getScript(sUrl)
and now the first line is ok, but a new error is found at the second line ... (I'm trying to translate it in English...)

"Information: Impossible to find the public member 'getScript' into the '_ComObject' type"

It seems that VB .NET didn't found "getScript" method!


This is the entire code:

Code: Select all

        Dim oSM                   'Root object for accessing OpenOffice from VB
        Dim oDesk, oDoc As Object 'First objects from the API
        Dim arg()                 'Ignore it for the moment !
        Dim OpenPar(2) As Object 'a Visual Basic array, with 3 elements
        Dim sUrl, oScript, oProv As Object
        Dim oScriptFactory, oScriptProvider

        OpenPar(0) = MakePropertyValue("ReadOnly", False)
        OpenPar(1) = MakePropertyValue("Password", "secret")
        OpenPar(2) = MakePropertyValue("Buttons", False)

        'Instanciate OOo : this line is mandatory with VB for OOo API
        oSM = CreateObject("com.sun.star.ServiceManager")
        'Create the first and most important service
        oDesk = oSM.createInstance("com.sun.star.frame.Desktop")

        'Open an existing doc (pay attention to the syntax for first argument)
        oDoc = oDesk.loadComponentFromURL("file:///c:/doc.sxw", "_blank", 0, OpenPar)

        sUrl = "vnd.sun.star.script:Standard.Module1.Main?language=Basic&location=document"

        oProv = oDoc.getScriptProvider()
        oScript = oProv.getScript(sUrl)

        oScript.invoke(arg, arg, arg)
I have c:/doc.sxw containing a standard macro called Main.

If this way is too hard to get, can I disable Print button directly from VB .NET or from a GLOBAL Macro?

thank u again!
ronny.olsson
Posts: 2
Joined: Wed Aug 29, 2012 1:52 pm

Re: OOBASIC Macro and VB .NET

Post by ronny.olsson »

This works, opens a blank document (calc) and run the macro Main

Code: Select all

        
        Dim oSM  'Root object for accessing OpenOffice from VB
        Dim oDesk, oDoc As Object 'First objects from the API
        Dim arg(1) As Object                'Ignore it for the moment !
        Dim OpenPar(1) As Object 'a Visual Basic array, with 3 elements
        'OpenPar(0) = MakePropertyValue("ReadOnly", False)
        'OpenPar(1) = MakePropertyValue("Password", "secret")

        'Instanciate OOo : this line is mandatory with VB for OOo API
        oSM = CreateObject("com.sun.star.ServiceManager")
        'Create the first and most important service
        oDesk = oSM.createInstance("com.sun.star.frame.Desktop")
        Dim MasterScriptProviderFactory As Object = oSM.createInstance("com.sun.star.script.provider.MasterScriptProviderFactory")

        'Open an existing doc (pay attention to the syntax for first argument)
        oDoc = oDesk.loadComponentFromURL("file:///c:/tmp/XMLT/blank.ods", "_blank", 0, OpenPar)

        Dim sUrl As String = "vnd.sun.star.script:Standard.Module1.Main?language=Basic&location=application"

        Dim oProvider As Object = MasterScriptProviderFactory.createScriptProvider("")
        Dim oScript As Object = oProvider.getScript(sUrl)
        oScript.invoke(arg, arg, arg)
Open Office 3.0 (Windows 7)
Post Reply