- 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...