Page 1 of 1

[Solved] Code for changing into another opened document

PostPosted: Sat Feb 23, 2013 3:31 pm
by ptbento
I have code performing actions in doc A and I would like to go to doc B, perform actions (based on info gathered from doc A), and get back to doc A in the same place. What would be the command for this?

Thanks,

Re: Macro code for changing into another opened document

PostPosted: Sat Feb 23, 2013 4:06 pm
by RPG
Hello

Start with study the BASIC tutorial
http://wiki.openoffice.org/wiki/Documen ... /Documents

Be sure you do understand the loadcomponentfromurl
http://wiki.openoffice.org/wiki/Documen ... tarDesktop
The loadcomponentfromurl returns an object what can be used for the document you want modify

Romke

Re: Macro code for changing into another opened document

PostPosted: Sat Feb 23, 2013 6:37 pm
by ptbento
Many thanks, Romke.
When I use the command StarDesktop.loadComponentFromURL, it actually opens a new document, when I actually just wanted to switch the focus/cursor to another already opened document. In fact, if the doc is already open, it opens a new one as a copy (which only generates confusion). Is there a way just move focus/cursor into a given open document by naming it or mentioning its url or its order within the set of open files (as in Vbasic)?

Re: Macro code for changing into another opened document

PostPosted: Sat Feb 23, 2013 7:04 pm
by RPG
Hello

ptbento wrote:if the doc is already open, it opens a new one as a copy
When you use the command good it does not open a new view of the same document

Code: Select all   Expand viewCollapse view
As the second parameter, loadComponentFromURL expects a name for the frame object of the window that OpenOffice.org creates internally for its administration. The predefined _blank name is usually specified here, and this ensures that OpenOffice.org creates a new window.
Maybe change the blank frame for an empty string.

http://www.openoffice.org/api/docs/comm ... oader.html
Understanding loadcomponentfromurl and other command who are working in the same way can help you to work more easy to macro.

I'm not sure if this is the best way for you but it is on this moment the most easy way I knew on this moment.

Romke

Re: Macro code for changing into another opened document

PostPosted: Sat Feb 23, 2013 8:01 pm
by RPG
Hallo

I have done a little test.

Romke
Code: Select all   Expand viewCollapse view
sub openform2
dim sNewfileFormName
sNewfileFormName="file:///home/romgro/Documenten/opof/data/exampledata.ods"
'stardesktop.loadComponentFromURL(sNewfileFormName,"_blank",0,array()) ' _blank open a new view
stardesktop.loadComponentFromURL(sNewfileFormName,"_default",0,array()) ' sets focus to the document
end sub

Re: Macro code for changing into another opened document

PostPosted: Sat Feb 23, 2013 8:29 pm
by ptbento
Thanks a lot, Romke. I have managed to make the switch by following your suggestion of inserting an empty string as paratmeter in loadComponentFromURL, as follows:

dttcorrente2 = StarDesktop.loadComponentFromURL(Url2, "", 0, Dummy2)

Re: Macro code for changing into another opened document

PostPosted: Sat Feb 23, 2013 9:11 pm
by RPG
Hello

When you want work with macros and loadcomponentfromurl then it is better to store your macros in Mymacros. You cannot run macros stored in the document what is opened with Loadcomponentfromurl.
see
http://www.openoffice.org/api/docs/comm ... cutionMode
see MacroExecutionMode

Romke

Re: Macro code for changing into another opened document

PostPosted: Sat Feb 23, 2013 9:31 pm
by Charlie Young
RPG wrote:You cannot run macros stored in the document what is opened with Loadcomponentfromurl.

Romke


Actually, you can.

In outline (which also shows a way of switching documents):

Code: Select all   Expand viewCollapse view

Sub RunMacroInAnotherDoc()
   Dim thisDoc As Object
   Dim otherDoc As Object
   Dim Url As String
   Dim Args(1) As new com.sun.star.beans.PropertyValue
   Dim scriptProvider
   Dim s
   
   thisDoc = ThisComponent
   
         
   Url = "otherDocUrl"
   
   Args(0).Name = "MacroExecutionMode"
   Args(0).Value = 4
   Args(1).Name = "Hidden"
   Args(1).Value = False
   
   otherDoc = StarDesktop.loadComponentFromURL(Url, "_Blank", 0, Args)
   Wait 1000
   otherDoc.BasicLibraries.loadLibrary("Standard")
   Wait 1000
   Set scriptProvider = otherDoc.getScriptProvider()

   s = scriptProvider.getScript("vnd.sun.star.script:Standard.Module1.SomeMacro?language=Basic&location=document")

   s.invoke(array(), array(), array())
   StarDesktop.setActiveFrame(otherDoc.CurrentController.Frame)
   otherDoc.CurrentController.Frame.ContainerWindow.toFront()
       
   Wait 5000
   StarDesktop.setActiveFrame(thisDoc.CurrentController.Frame)
   thisDoc.CurrentController.Frame.ContainerWindow.toFront()      
End Sub


 Edit: Above code modified since original post. In order to change into another document, you need the ContainerWindow.toFront() calls in addition to the setActiveFrame. 

Re: Macro code for changing into another opened document

PostPosted: Mon Feb 25, 2013 12:05 am
by ptbento
Thank you. I said above I had managed to move cursor from one opened doc to another opened doc. Unfortunately this is not the case. As I am not enough of a programmer in OO I am not able to implement your suggestion. If only OO could record the switch from one doc to another and generate the corresponding code...

Re: Macro code for changing into another opened document

PostPosted: Fri Jan 26, 2018 1:28 am
by det
Screen Shot 2018-01-25 at 3.02.57 PM.png

I have problems with url. Copied the code from:
https://wiki.openoffice.org/wiki/Docume ... tarDesktop[code][/code]
Sub Load
Dim Doc1 As Object
Dim Url As String
Dim Dummy() 'An (empty) array of PropertyValues
Url = "file:///iMac:/Users/imac/Doc1.ods"
Doc1 = StarDesktop.loadComponentFromURL(Url, "_blank", 0, Dummy)
End Sub

I get the attached message. What am I doing wrong? Please help
Det
macOS High Sierra 10.13.2

Re: Macro code for changing into another opened document

PostPosted: Fri Jan 26, 2018 1:45 am
by FJCC
The error message says there is no document at the location defined by the URL. Are you sure that the path is correct? You could try using the literal full path as an argument for the convertToURL() function and use the output of that as the first argument of loadComponentFromURL().
Code: Select all   Expand viewCollapse view
Url = convertToURL("Full/path/to/document")
Doc1 = StarDesktop.loadComponentFromURL(Url, "_blank", 0, Dummy)

where Full/path/to/document needs to be replaced by the actual document location.

Re: Macro code for changing into another opened document

PostPosted: Fri Jan 26, 2018 5:51 am
by det
I tried your suggestion:
Sub Load
Dim Doc1 As Object
Dim Url As String
Dim Dummy() 'An (empty) array of PropertyValues
Url = convertToURL( "iMac:/Users/imac/Doc1.ods")
Doc1 = StarDesktop.loadComponentFromURL(Url, "_blank", 0, Dummy)
End Sub
I still get the same error message. Is there perhaps an issue with OO and the Mac??

Re: Macro code for changing into another opened document

PostPosted: Fri Jan 26, 2018 6:20 am
by FJCC
Are you sure the iMac: part of your path is correct? I haven't used a Mac in 25 years but a file path on my wife's system starts with /Users and that is what I would expect on a linux system. Try
Code: Select all   Expand viewCollapse view
Url = convertToURL( "/Users/imac/Doc1.ods")

Re: [Solved]Macro code for changing into another opened docu

PostPosted: Fri Jan 26, 2018 7:54 am
by det
Hurrah It works. Thank you so much. I always had ":" inserted as the original code had shown.