| View previous topic :: View next topic |
| Author |
Message |
hzimpel Guest
|
Posted: Thu Nov 20, 2003 9:50 am Post subject: calling a macro via uno |
|
|
Hi,
is it possible to call a macro in a document via the uno api (from java level)?
Regards,
Helge |
|
| Back to top |
|
 |
DannyB Moderator


Joined: 02 Apr 2003 Posts: 3991 Location: Lawrence, Kansas, USA
|
|
| Back to top |
|
 |
hzimpel Guest
|
Posted: Fri Nov 21, 2003 11:40 am Post subject: calling a macro via uno |
|
|
Hi,
thank you for the hints you gave me.
When invoking the macro from command line with soffice.exe "macro://Unbenannt1/Standard.Module1.Macro1" it works but not with the following code. Calling ".uno:About" works with this code.
What's wrong here?
Regards,
Helge
com.sun.star.util.URL[] aURL = new com.sun.star.util.URL[1];
aURL[0] = new com.sun.star.util.URL();
// aURL[0].Complete = "macro://Unbenannt1/Standard.Module1.Macro1";
aURL[0].Complete = ".uno:About";
com.sun.star.util.XURLTransformer xParser =
(com.sun.star.util.XURLTransformer) UnoRuntime.queryInterface(
com.sun.star.util.XURLTransformer.class,
mxRemoteServiceManager.createInstanceWithContext(
"com.sun.star.util.URLTransformer",
mxRemoteContext));
xParser.parseStrict(aURL);
XModel oModel =
(XModel) UnoRuntime.queryInterface(
XModel.class,
xTemplateComponent);
XController oController = oModel.getCurrentController();
XDispatchProvider oProvider =
(XDispatchProvider) UnoRuntime.queryInterface(
XDispatchProvider.class,
oController);
com.sun.star.frame.XDispatch xDispatcher =
oProvider.queryDispatch(
aURL[0],
"",
com.sun.star.frame.FrameSearchFlag.GLOBAL);
PropertyValue[] lArguments = new PropertyValue[0];
if (xDispatcher != null)
xDispatcher.dispatch(aURL[0], lArguments); |
|
| Back to top |
|
 |
hzimpel Guest
|
Posted: Mon Nov 24, 2003 5:26 am Post subject: calling a macro via uno |
|
|
Hi,
I had success with this code:
| Code: |
com.sun.star.util.URL[] aURL = new com.sun.star.util.URL[1];
aURL[0] = new com.sun.star.util.URL();
aURL[0].Complete = "macro:///Standard.Module1.Macro1";
// aURL[0].Complete = ".uno:About";
com.sun.star.util.XURLTransformer xParser =
(com.sun.star.util.XURLTransformer) UnoRuntime.queryInterface(
com.sun.star.util.XURLTransformer.class,
mxRemoteServiceManager.createInstanceWithContext(
"com.sun.star.util.URLTransformer",
mxRemoteContext));
xParser.parseStrict(aURL);
XDesktop xDesktop =
(XDesktop) UnoRuntime.queryInterface(XDesktop.class, desktop);
XFrame xFrame = (XFrame) xDesktop.getCurrentFrame();
XDispatchProvider oProvider =
(XDispatchProvider) UnoRuntime.queryInterface(
XDispatchProvider.class,
xFrame);
com.sun.star.frame.XDispatch xDispatcher =
oProvider.queryDispatch(aURL[0], "_self", 0);
PropertyValue[] lArguments = new PropertyValue[0];
System.out.println("xDispatcher" + xDispatcher);
if (xDispatcher != null)
xDispatcher.dispatch(aURL[0], lArguments);
|
|
|
| Back to top |
|
 |
DannyB Moderator


Joined: 02 Apr 2003 Posts: 3991 Location: Lawrence, Kansas, USA
|
Posted: Mon Nov 24, 2003 9:28 am Post subject: |
|
|
That is very interesting. I'm glad you got it to work.
I cannot see an obvious reason why the straightforward translation to Basic does not work.
| Code: | Sub Main
cMacroUrlString = "macro:///DannysLibrary/Conversion/SayHelloWorld"
' cMacroUrlString = ".uno:About"
' Here is one technique to create a single element array of a struct.
oURL = createUnoStruct( "com.sun.star.util.URL" )
oURL.Complete = cMacroUrlString
aURL = Array( oURL )
' Here is another technique to create a single element array of a struct.
' Dim aUrl(0) As com.sun.star.util.URL
' aUrl(0) = createUnoStruct( "com.sun.star.util.URL" )
' aUrl(0).Complete = cMacroUrlString
oUrlParser = createUnoService( "com.sun.star.util.URLTransformer" )
oUrlParser.parseStrict( aURL() )
oFrame = StarDesktop.getCurrentFrame()
oDispatcher = oFrame.queryDispatch( aURL(0), "_self", 0 )
oDispatcher.dispatch( aURL(0), Array() )
End Sub
|
It fails on the very last line, because the variable oDispatcher does not contain an object from the 2nd to last line. _________________ Want to make OOo Drawings like the colored flower design to the left? |
|
| Back to top |
|
 |
DannyB Moderator


Joined: 02 Apr 2003 Posts: 3991 Location: Lawrence, Kansas, USA
|
Posted: Mon Nov 24, 2003 4:16 pm Post subject: |
|
|
Here is a working version in Basic.
| Code: | Sub Main
cMacroUrlString = "macro:///DannysLibrary.Conversion.SayHelloWorld"
' cMacroUrlString = ".uno:About"
oURL = createUnoStruct( "com.sun.star.util.URL" )
oURL.Complete = cMacroUrlString
oUrlParser = createUnoService( "com.sun.star.util.URLTransformer" )
oUrlParser.parseStrict( oURL )
oFrame = StarDesktop.getCurrentFrame()
oDispatcher = oFrame.queryDispatch( oURL, "_self", 0 )
oDispatcher.dispatch( oURL, Array() )
End Sub
|
There are two things wrong.
The first was my embarrasing mistake. I was using slashes in the URL where I needed to be using dots. Oops.
The second problem I'm less sure about.
In the original program, the line...
| Code: | | xParser.parseStrict(aURL); |
had me confused. Why pass an array of the URL to parseStrict? I didn't bother to look up parseStrict until just now. Apparently, it is an in/out parameter. Since java passes everything by value, you need to pass the url encapsulated in a single element array in order to pass it effectively as an in/out parameter? Is this correct? (I would have to go dig through the Java-UNO documentation to find out, and I'm too lazy.)
Why would I care about being able to call a macro via. a URL from Basic? Why not just call it directly? Well, first of all, "Because it's there." But a more practical answer would be to be able to dynamically construct the URL and call something which you did not know the name of when you wrote the macro. For instance, from Basic, I can obtain the source text of any other basic macro. Well, now I could call it without knowing the name in advance. Finally, the Basic version may be easier for others to translate into, say Delphi, or VB or other languages where they might want to call a macro across the UNO bridge.
Thanks for getting it to work and posting your code. _________________ Want to make OOo Drawings like the colored flower design to the left? |
|
| Back to top |
|
 |
hzimpel Guest
|
Posted: Tue Nov 25, 2003 2:00 am Post subject: calling a macro via uno |
|
|
In the SDK java examples you can find the following comment regarding parseStrict:
"Because it's an in/out parameter we must use an array of URL objects" |
|
| Back to top |
|
 |
ooodummy Guest
|
Posted: Tue Dec 30, 2003 12:39 am Post subject: |
|
|
hi,
i believe there are differences calling an global macro and calling an local (in the document itself) macro....calling an global macro like "uno:..." or "macro:///..." worls, but calling an local macro like "macro://filename/..." doesn't....
i believe that there must be an option to set when openning an macro-including doc, something like..."allow all macros"....with the loadproperties...the only thing i found in the docs is this:
[url]
http://api.openoffice.org/docs/common/ref/com/sun/star/document/MediaDescriptor.html#Referer
[/url]
has anyone a clue????
Greetings.....a ooodummy |
|
| Back to top |
|
 |
DannyB Moderator


Joined: 02 Apr 2003 Posts: 3991 Location: Lawrence, Kansas, USA
|
Posted: Tue Dec 30, 2003 8:32 am Post subject: |
|
|
I think you are asking two questions.
1. Can you use a URL to execute a macro in a document, even a document that has not been saved?
2. Can you open a document in such a way that macros in that document are allowed to run?
The answer to number 1 is Yes. I have recently posted an example of how to use a URL to execute a macro in an Untitled document which has not yet been saved to disk.
The answer to number 2 is also Yes. I answered a question several weeks ago showing how to pass a PropertyValue to the loadComponentFromURL which would allow macros in the document to run.
If I have more time later, I might try to find the old messages I wrote and post URL's to them. (Boy I wish we had a an organized wiki!) _________________ Want to make OOo Drawings like the colored flower design to the left? |
|
| Back to top |
|
 |
DannyB Moderator


Joined: 02 Apr 2003 Posts: 3991 Location: Lawrence, Kansas, USA
|
|
| 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
|