| View previous topic :: View next topic |
| Author |
Message |
mwise_au Newbie


Joined: 06 May 2003 Posts: 4 Location: UK
|
Posted: Wed May 07, 2003 5:04 am Post subject: Programmatically calling macros |
|
|
Hi All,
Would appreciate a clear programmatic macro call example using the url method or better.
For example this produces no errors and no result;
| Code: | sub Main
dim document as object
dim dispatcher as object
document = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
dim args1(0) as new com.sun.star.beans.PropertyValue
args1(0).Name = "xxx"
args1(0).Value = "xxxx"
dispatcher.executeDispatch(document, "macro:///Standard.Module1.helloworld", "",0, args1())
end sub
Sub helloworld
msgbox "Hello I was called programmatically, hmm how about some parameters next time... "
End Sub |
Any ideas ? Yes I know there is the method but also looking at way to invoke this externally from OO. Similar to the M$VB | Code: | | Application.Run "MyMacro", arg1, arg2 | ??
Why? potentially to return a result from a function or just invoke a macro.
Cheers,
Leahy |
|
| Back to top |
|
 |
mwise_au Newbie


Joined: 06 May 2003 Posts: 4 Location: UK
|
Posted: Wed May 14, 2003 8:50 am Post subject: Looking for help on the return arguments now ... |
|
|
The answer to my question was;
macro://Untitled1/Standard.Main.createLayout() as and example the brackets and dispatch to _SELF was the trick.
The following code is getting close, I can call Functions and Subroutines pass in parameters but getting a return is now the trouble. Also worked out how to have listeners. The listener example I have is on the Library but you could attach against the dispatch. To test place code into a HTML page and <script language="VBScript"> tag etc...
| Code: | Class VBEventListener
Private interfaces(0)
Public Property Get Bridge_ImplementedInterfaces()
Bridge_ImplementedInterfaces = interfaces
End Property
Private Sub Class_Initialize()
interfaces(0) = "com.sun.star.container.XContainerListener"
End Sub
Private Sub Class_Terminate()
On Error Resume Next
End Sub
Public Sub disposing( oEvent )
MsgBox "disposing called"
End Sub
Public Sub elementInserted( oEvent )
MsgBox "elementInserted called" & oEvent.Element
End Sub
Public Sub elementRemoved( oEvent )
MsgBox "elementRemoved called"
End Sub
Public Sub elementReplaced( oEvent )
MsgBox "elementReplaced called"
End Sub
End Class
Dim objServiceManager, objDocument, objDesktop, args(), coreReflection
Set objServiceManager= CreateObject("com.sun.star.ServiceManager")
Set objDesktop = objServiceManager.createInstance("com.sun.star.frame.Desktop")
Set objReflection = objServiceManager.createInstance("com.sun.star.reflection.CoreReflection")
Set objDocument= objDesktop.loadComponentFromURL("private:factory/scalc", "_blank", 0, args)
objDocument.addActionLock
Dim sBasicModule, sBasicModuleName
sBasicModuleName = "Main"
sBasicModule = "REM ***** BASIC *****" & chr(10) & chr(10)
sBasicModule = sBasicModule & "sub createLayout () " & chr(10)
sBasicModule = sBasicModule & "dim document as object " & chr(10)
sBasicModule = sBasicModule & "dim dispatcher as object " & chr(10)
sBasicModule = sBasicModule & "document = ThisComponent.CurrentController.Frame " & chr(10)
sBasicModule = sBasicModule & "dispatcher = createUnoService(""com.sun.star.frame.DispatchHelper"") " & chr(10)
sBasicModule = sBasicModule & "dim args4(0) as new com.sun.star.beans.PropertyValue " & chr(10)
sBasicModule = sBasicModule & "args4(0).Name = ""ToPoint"" " & chr(10)
sBasicModule = sBasicModule & "args4(0).Value = ""$B$6"" " & chr(10)
sBasicModule = sBasicModule & "dispatcher.executeDispatch(document, "".uno:GoToCell"", """", 0, args4()) " & chr(10)
sBasicModule = sBasicModule & "dim args5(0) as new com.sun.star.beans.PropertyValue " & chr(10)
sBasicModule = sBasicModule & "args5(0).Name = ""StringName"" " & chr(10)
sBasicModule = sBasicModule & "args5(0).Value = ""Sun StarOffice - Yeah !!!! (c;"" " & chr(10)
sBasicModule = sBasicModule & "dispatcher.executeDispatch(document, "".uno:EnterString"", """", 0, args5()) " & chr(10)
sBasicModule = sBasicModule & "end sub " & chr(10)
sBasicModule = sBasicModule & "function getProduct ( byval arg1 as String, arg2 as String ) " & chr(10)
sBasicModule = sBasicModule & " msgbox ""getProduct() arg1="" & arg1 & "", arg2="" & arg2 " & chr(10)
sBasicModule = sBasicModule & " arg1 = GetProductname & ""- yoh mamma !!! arg1="" & arg1 " & chr(10)
sBasicModule = sBasicModule & " getProduct = GetProductname & ""- yoh mamma !!!"" " & chr(10)
sBasicModule = sBasicModule & "end function " & chr(10)
sBasicModule = sBasicModule & "Sub disposing( oEvent ) " & chr(10)
sBasicModule = sBasicModule & " REM ---- " & chr(10)
sBasicModule = sBasicModule & "End Sub " & chr(10)
sBasicModule = sBasicModule & "Sub elementInserted( oEvent ) " & chr(10)
sBasicModule = sBasicModule & " callMethod ( oEvent.Element, true ) " & chr(10)
sBasicModule = sBasicModule & "End Sub " & chr(10)
sBasicModule = sBasicModule & "Sub elementRemoved( oEvent ) " & chr(10)
sBasicModule = sBasicModule & " REM ---- " & chr(10)
sBasicModule = sBasicModule & "End Sub " & chr(10)
sBasicModule = sBasicModule & "Sub elementReplaced( oEvent ) " & chr(10)
sBasicModule = sBasicModule & " REM ---- " & chr(10)
sBasicModule = sBasicModule & "End Sub " & chr(10)
sBasicModule = sBasicModule & "Function callMethod(methodName As String, bGetResult As Boolean) As Variant " & chr(10)
sBasicModule = sBasicModule & "On Error Goto ErrHndl " & chr(10)
sBasicModule = sBasicModule & " Dim oLibrary As Object:Dim Script As String: Dim result As Variant " & chr(10)
sBasicModule = sBasicModule & " oLibrary = BasicLibraries.getByName(""Standard"") " & chr(10)
sBasicModule = sBasicModule & " Script = ""Function executeMethod() As Variant"" & chr(10) & chr(13) " & chr(10)
sBasicModule = sBasicModule & " if (bGetResult) then " & chr(10)
sBasicModule = sBasicModule & " Script = Script & ""executeMethod() = "" & methodName " & chr(10)
sBasicModule = sBasicModule & " else " & chr(10)
sBasicModule = sBasicModule & " Script = Script & methodName " & chr(10)
sBasicModule = sBasicModule & " Script = Script & "" : executeMethod() = true"" " & chr(10)
sBasicModule = sBasicModule & " end if " & chr(10)
sBasicModule = sBasicModule & " Script = Script & chr(10) & ""End Function"" " & chr(10)
sBasicModule = sBasicModule & " if (oLibrary.hasByName(""BneTmpMethod"")) then " & chr(10)
sBasicModule = sBasicModule & " oLibrary.replaceByName(""BneTmpMethod"", Script) " & chr(10)
sBasicModule = sBasicModule & " else " & chr(10)
sBasicModule = sBasicModule & " oLibrary.insertByName(""BneTmpMethod"", Script) " & chr(10)
sBasicModule = sBasicModule & " end if " & chr(10)
sBasicModule = sBasicModule & " result = BneTmpMethod.executeMethod() " & chr(10)
sBasicModule = sBasicModule & " oLibrary.removedByName(""BneTmpMethod"" ) " & chr(10)
sBasicModule = sBasicModule & " callMethod() = result " & chr(10)
sBasicModule = sBasicModule & "Exit Function " & chr(10)
sBasicModule = sBasicModule & "ErrHndl: " & chr(10)
sBasicModule = sBasicModule & " callMethod() = ""Exception: "" & error " & chr(10)
sBasicModule = sBasicModule & "End Function " & chr(10)
sBasicModule = sBasicModule & "Sub Main() " & chr(10)
sBasicModule = sBasicModule & " callMethod ( ""BneMain.createLayout()"", true ) " & chr(10)
sBasicModule = sBasicModule & "End Sub " & chr(10)
'
' Add Code Module
'
Dim objLibrary
Set objLibrary = objDocument.BasicLibraries.GetByName("Standard")
objLibrary.InsertByName sBasicModuleName, sBasicModule
'
' Add Events
'
Dim eventCall,oListener, oLibrary
Set eventCall = createStruct ("com.sun.star.container.ContainerEvent")
eventCall.Element = "SomeElement"
Set oListener = new VBEventListener
objLibrary.addContainerListener( oListener )
oListener.elementInserted ( eventCall )
'
' Execute some code
'
Dim objFrame, objParser, objUrl, objDispatcher, methodArgs()
Set objFrame = objDocument.CurrentController.Frame
Set objParser = objServiceManager.createInstance("com.sun.star.util.URLTransformer")
Set objUrl = createStruct ( "com.sun.star.util.URL" )
' Call Subroutine
objUrl.Complete = "macro://Untitled1/Standard.Main.createLayout()"
Set objDispatcher = objFrame.queryDispatch(objUrl,"_self",0)
objDispatcher.dispatch objUrl, methodArgs
' Call Function
objUrl.Complete = "macro://Untitled1/Standard.BneMain.getProduct()?Hello Arg1,Hi Arg2"
Set objDispatcher = objFrame.queryDispatch(objUrl,"_self",0)
Dim getProductArgs(0)
set getProductArgs(0) = createStruct ( "com.sun.star.beans.PropertyValue" )
getProductArgs(0).Name = "arg1"
getProductArgs(0).Value = "did i overwrite?"
objDispatcher.dispatch objUrl, getProductArgs
msgbox "Return: name=" & getProductArgs(0).name & " value=" & getProductArgs(0).value
objDocument.removeActionLock
Function createStruct( strTypeName)
Set classSize= objReflection.forName( strTypeName)
Dim aStruct
classSize.createObject aStruct
Set createStruct= aStruct
End Function |
Any ideas on the return arguments ???? |
|
| Back to top |
|
 |
matti Newbie

Joined: 15 May 2003 Posts: 1
|
Posted: Thu May 15, 2003 1:42 am Post subject: |
|
|
The dispatch API does not return something here. The idea behind this API is that it should execute functions that are bound to UI events where return valus usually are not interesting.
Executing basic macros and scripts with return values will be possible with the OOo Scripting Framework and a StarBasic runtime for that. The Scripting Framework is still under construction, a first version is already available, but this version only supports Java and Jython. Support for StarBasic can be expected later.
From your example code it seems that you want to call macros via dispatch API in Basic. Is this a workaround for the fact that you can't call macros stored in documents directly from the outside? Can you put your macro into the global basic storage instead? Then it should be reachable for a direct call from anywhere! |
|
| Back to top |
|
 |
mwise_au Newbie


Joined: 06 May 2003 Posts: 4 Location: UK
|
Posted: Thu May 15, 2003 5:32 am Post subject: |
|
|
Yes I was looking for the Scripting Framework to support StarBasic, what I have for now is a work around. It would be preferable to have the Bridge support mapping StarBasic functions/modules etc... and then the following code would be simpler. The idea is to be able to call/invoke OOo sheet functions or macro functions and return the result. The following example is using a HTML page in IE 5 or better to host.
| Code: | <HTML>
<head>
<title>Test OpenOffice Automation</title>
<style>
button {width:120px; cursor:hand;border:1px solid #cccc99; BACKGROUND-COLOR: #f7f7e7; border-width:1pt;border-color:#cccc99; font-family:arial;font-size:9pt;}
</style>
</head>
<body>
<script language="VBScript" >
Sub runTest ()
Dim objServiceManager, objDocument, objDesktop, noargs(), objReflection
Set objServiceManager= CreateObject("com.sun.star.ServiceManager")
Set objDesktop = objServiceManager.createInstance("com.sun.star.frame.Desktop")
Set objReflection = objServiceManager.createInstance("com.sun.star.reflection.CoreReflection")
'Dim commandArgs(0)
'set commandArgs(0) = createStruct ( "com.sun.star.beans.PropertyValue", objReflection )
' commandArgs(0).Name = "hidden"
' commandArgs(0).Value = "true"
Set objDocument= objDesktop.loadComponentFromURL("private:factory/" & txtObject.value, "_blank", 0, noargs)
Dim sBasicModule, sBasicModuleName
sBasicModuleName = "Main"
sBasicModule = "REM ***** BASIC *****" & chr(10) & chr(10)
sBasicModule = sBasicModule & "function getProduct ( arg1 as String, arg2 as String ) as String " & chr(10)
sBasicModule = sBasicModule & " msgbox ""1: getProduct() arg1="" & arg1 & "", arg2="" & arg2 " & chr(10)
sBasicModule = sBasicModule & " arg1 = ""yoh mamma !!! arg1="" & arg1 " & chr(10)
sBasicModule = sBasicModule & " getProduct = ""yoh mamma !!!"" " & chr(10)
sBasicModule = sBasicModule & "end function " & chr(10)
'
' Add Code Module
'
Dim objLibrary
Set objLibrary = objDocument.BasicLibraries.GetByName("Standard")
objLibrary.InsertByName sBasicModuleName, sBasicModule
'
' Execute some code
'
Dim objFrame, objParser, objUrl, objDispatcher, sMacro
Set objFrame = objDocument.CurrentController.Frame
Set objParser = objServiceManager.createInstance("com.sun.star.util.URLTransformer")
Set objUrl = createStruct ( "com.sun.star.util.URL", objReflection )
' Call Function
sMacro = "Standard.Main.getProduct()?Hello Arg1,Hi Arg2"
objUrl.Complete = "macro://Untitled1/" & sMacro
Set objDispatcher = objFrame.queryDispatch(objUrl,"_self",0)
Dim getProductArgs(1)
set getProductArgs(0) = createStruct ( "com.sun.star.beans.PropertyValue", objReflection )
getProductArgs(0).Name = "getProduct"
getProductArgs(0).Value = "did i overwrite?"
set getProductArgs(1) = createStruct ( "com.sun.star.beans.PropertyValue", objReflection )
getProductArgs(1).Name = "arg2"
getProductArgs(1).Value = "me too?"
Dim returnValue, documentType
returnValue = objDispatcher.dispatch (objUrl, getProductArgs)
If objDocument.SupportsService("com.sun.star.sheet.SpreadsheetDocument") Then
documentType = "scalc"
ElseIf objDocument.SupportsService("com.sun.star.text.TextDocument") Then
documentType = "swriter"
ElseIf objDocument.SupportsService("com.sun.star.drawing.DrawingDocument") Then
documentType = "sdraw"
ElseIf objDocument.SupportsService("com.sun.star.formula.FormulaProperties") Then
documentType = "smath"
End If
output.innerHTML = "Type=" & documentType & ", Return=" & returnValue
'
' Clean UP
'
'objDocument.dispose()
'objServiceManager.dispose()
End Sub
Function createStruct( strTypeName, objReflection )
Set classSize = objReflection.forName( strTypeName)
Dim aStruct
classSize.createObject aStruct
Set createStruct= aStruct
End Function
</script>
<p>Open Automation Test</p>
<p><INPUT STYLE="width:200px;border-width:1pt;border-style:solid;" id=txtObject name=txtObject value="scalc" >
<button id=cmdRunTest title="Test" onclick="runTest()">Test</button>
<button onclick="txtObject.value='scalc': runTest()">Test Spreadsheet</button>
<button onclick="txtObject.value='swriter': runTest()">Test Text Document</button>
<button onclick="txtObject.value='simpress': runTest()">Test Presentation</button>
<button onclick="txtObject.value='sdraw': runTest()">Test Drawing</button>
</p>
<p> Results: <span id=output name=output>(no output)</span></p>
</p>
</body>
</HTML> |
Any more ideas? |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|