Run macro other than the first in OpenOffice

Creating Extension - Shared Libraries
Forum rules
For sharing working examples of macros / scripts. These can be in any script language supported by OpenOffice.org [Basic, Python, Netbean] or as source code files in Java or C# even - but requires the actual source code listing. This forum is not for asking questions about writing your own macros.

Run macro other than the first in OpenOffice

Postby JeJe » Fri Dec 20, 2019 5:15 pm

You can only run the first macro in a module in OpenOffice. This is a (still clunky) workaround...

Code: Select all   Expand viewCollapse view
'usage
'put code in a module in your application standard library or somewhere else suitable.
'set a shortcut, toolbar or menuitem to RunMacroFromSelecttext
'type 'yourLibraryName.yourModuleName.yourSubName anywhere in your code
'select it
'call RunMacroFromSelecttext using your shortcut


Sub RunMacroFromSelecttext
   oBasicComp = getBasicWindow
   oDS = createUnoService("com.sun.star.frame.DispatchHelper")


   Selectedtext =getIdeText

   if len(Selectedtext) = 0 then exit sub

   if asc(Selectedtext) = 9 then 'get rid of tabs
      for i = 1 to len(Selectedtext)
         if mid(Selectedtext,i,1) <> chr(9) then
            exit for
         else
            mid(Selectedtext,i,1) =" "
         end if
      next

   end if

   Selectedtext=trim(Selectedtext) 'get rid of spaces
   if left(Selectedtext,1) = "'" then Selectedtext = right(Selectedtext,len(Selectedtext)-1) 'remove a comment ' if there is one

   sURL = "vnd.sun.star.script:" & Selectedtext & "?language=Basic&location=application" 'change location=application to = document if necessary
   res =oDS.executeDispatch(oBasicComp.CurrentController.Frame,sURL,"",0,array())

End Sub

   ' getBasicWindow code from ms777
   'https://forum.openoffice.org/en/forum/viewtopic.php?f=20&t=6458
function getBasicWindow() as Any
   oEnum = Stardesktop.Components.createEnumeration
   while oEnum.hasMoreElements
      oComp = oEnum.nextElement
      if HasUnoInterfaces(oComp, "com.sun.star.lang.XServiceInfo") then 'to cath the Help Window
         if oComp.supportsService("com.sun.star.script.BasicIDE") then
            oBasicComp = oComp
         endif
      endif
      wend
      getBasicWindow = oBasicComp
end function

      'getIdeText DavidHMcCracken
      'https://forum.openoffice.org/en/forum/viewtopic.php?f=25&t=98803&p=480714&hilit=ide#p480714
function getIdeText() as string
      dim dispatcher as object, frame as object
      dim clipboard as object, conv as object, cont as object, flavors as object
      dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
      frame = StarDesktop.CurrentComponent.CurrentController.Frame
      dispatcher.executeDispatch(frame, ".uno:Copy", "", 0, Array())
      clipboard = CreateUNOService("com.sun.star.datatransfer.clipboard.SystemClipboard")
      conv = CreateUNOService("com.sun.star.script.Converter")
      cont = clipboard.getContents()
      flavors = cont.getTransferDataFlavors()
      for i = LBound(flavors) To UBound(flavors)
         if flavors(i).MimeType = "text/plain;charset=utf-16" then
            getIdeText() = conv.convertToSimpleType(_
            cont.getTransferData(flavors(i)), com.sun.star.uno.TypeClass.STRING)
            exit for
         endif
      next
end function




Edit: I tried a more complicated method of using accessiblecontext to work out the sub from the selected text, and the status bar (which gives the library and module name)... but OO kept crashing so that was no good...
Openoffice 4.1.6
Windows 8
JeJe
Volunteer
 
Posts: 1410
Joined: Wed Mar 09, 2016 2:40 pm

Re: Run macro other than the first in OpenOffice

Postby RoryOF » Fri Dec 20, 2019 6:21 pm

I found that if I use /Tools /Macros /Organize Macros /OpenOffice Basic I could select any macro in a chain of subroutines and select Run from that windows and it would run.

A little clunky perhaps, but it allowed running and debugging a macro without having to enter it into a new module.
Apache OpenOffice 4.1.9 on Xubuntu 20.04.1 (mostly 64 bit version) and very infrequently on Win2K/XP
User avatar
RoryOF
Moderator
 
Posts: 32205
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Run macro other than the first in OpenOffice

Postby JeJe » Fri Dec 20, 2019 6:52 pm

Yeah, I find the Run Macros dialog a pain. I started writing my own alternative some time ago which would have some extra features, such as setting the parameters and returning the result of a function.

(Attached, half finished...

Run JeMacros/AAUser/AltRunMacroDialog to get the Dialog.
Choose the macro.
Click the parameter and set
click run to get the result.

Loads extremely slowly in LibreOffice for some reason (& not tested there) but much quicker in OO)

Edit: note string size limit for a module text for getting the parameters.
Attachments
AltRunMacros.oxt
(16.55 KiB) Downloaded 64 times
Openoffice 4.1.6
Windows 8
JeJe
Volunteer
 
Posts: 1410
Joined: Wed Mar 09, 2016 2:40 pm

Re: Run macro other than the first in OpenOffice

Postby Lupp » Sat Dec 21, 2019 1:19 pm

Concerning the running and debugging macros from the IDE (what JeJe addressed) I would like to add that for a long time now the Basic IDE of LibreOffice, if ordered to do a step or to simply run, starts the Sub/Function where the editing cursor currently is placed in. If someone still is maintaining the IDE code for AOO it should be an easy hack to implement this feature there, too.
On Windows 10: LibreOffice 7.0 and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
User avatar
Lupp
Volunteer
 
Posts: 3003
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: Run macro other than the first in OpenOffice

Postby musikai » Sat Dec 21, 2019 1:50 pm

Lupp wrote:Concerning the running and debugging macros from the IDE (what JeJe addressed) I would like to add that for a long time now the Basic IDE of LibreOffice, if ordered to do a step or to simply run, starts the Sub/Function where the editing cursor currently is placed in. If someone still is maintaining the IDE code for AOO it should be an easy hack to implement this feature there, too.


Yes, in LibreOffice that's much nicer to run single subs.

In OpenOffice I simply do this workaround:
Code: Select all   Expand viewCollapse view
Sub main
'call test1
call test2
end sub

sub test1
end sub

sub test2
end sub
Win7 Pro, Lubuntu 15.10, LO 4.4.7, OO 4.1.3
Free Project: LibreOffice Songbook Architect (LOSA)
http://struckkai.blogspot.de/2015/04/libreofficesongbookarchitect.html
musikai
Volunteer
 
Posts: 282
Joined: Wed Nov 11, 2015 12:19 am


Return to Code Snippets

Who is online

Users browsing this forum: No registered users and 2 guests