[Solved] Code for changing into another opened document

Keyboard macros or custom scripts

[Solved] Code for changing into another opened document

Postby ptbento » Sat Feb 23, 2013 3:31 pm

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,
Last edited by Hagar Delest on Fri Jan 26, 2018 8:54 am, edited 1 time in total.
Reason: tagged solved
OpenOffice 3.4.1 on Windowx XP
ptbento
 
Posts: 32
Joined: Fri Jan 18, 2013 9:33 pm

Re: Macro code for changing into another opened document

Postby RPG » Sat Feb 23, 2013 4:06 pm

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
LibreOffice 6.1.3.1 on openSUSE Leap 15
RPG
Volunteer
 
Posts: 2141
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: Macro code for changing into another opened document

Postby ptbento » Sat Feb 23, 2013 6:37 pm

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)?
OpenOffice 3.4.1 on Windowx XP
ptbento
 
Posts: 32
Joined: Fri Jan 18, 2013 9:33 pm

Re: Macro code for changing into another opened document

Postby RPG » Sat Feb 23, 2013 7:04 pm

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
LibreOffice 6.1.3.1 on openSUSE Leap 15
RPG
Volunteer
 
Posts: 2141
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: Macro code for changing into another opened document

Postby RPG » Sat Feb 23, 2013 8:01 pm

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
LibreOffice 6.1.3.1 on openSUSE Leap 15
RPG
Volunteer
 
Posts: 2141
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: Macro code for changing into another opened document

Postby ptbento » Sat Feb 23, 2013 8:29 pm

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)
OpenOffice 3.4.1 on Windowx XP
ptbento
 
Posts: 32
Joined: Fri Jan 18, 2013 9:33 pm

Re: Macro code for changing into another opened document

Postby RPG » Sat Feb 23, 2013 9:11 pm

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
LibreOffice 6.1.3.1 on openSUSE Leap 15
RPG
Volunteer
 
Posts: 2141
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: Macro code for changing into another opened document

Postby Charlie Young » Sat Feb 23, 2013 9:31 pm

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. 
Apache OpenOffice 4.1.1
Windows XP
User avatar
Charlie Young
Volunteer
 
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: Macro code for changing into another opened document

Postby ptbento » Mon Feb 25, 2013 12:05 am

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...
OpenOffice 3.4.1 on Windowx XP
ptbento
 
Posts: 32
Joined: Fri Jan 18, 2013 9:33 pm

Re: Macro code for changing into another opened document

Postby det » Fri Jan 26, 2018 1:28 am

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
OpenOffice 4.1.5 on Mac Sierra 10.13.1
det
 
Posts: 82
Joined: Fri Mar 31, 2017 8:24 pm

Re: Macro code for changing into another opened document

Postby FJCC » Fri Jan 26, 2018 1:45 am

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.
AOO 3.4 or 4.1 on MS Windows XP ( before 2013-08-03) or Windows 7
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
FJCC
Moderator
 
Posts: 6758
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Macro code for changing into another opened document

Postby det » Fri Jan 26, 2018 5:51 am

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??
OpenOffice 4.1.5 on Mac Sierra 10.13.1
det
 
Posts: 82
Joined: Fri Mar 31, 2017 8:24 pm

Re: Macro code for changing into another opened document

Postby FJCC » Fri Jan 26, 2018 6:20 am

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")
AOO 3.4 or 4.1 on MS Windows XP ( before 2013-08-03) or Windows 7
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
FJCC
Moderator
 
Posts: 6758
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

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

Postby det » Fri Jan 26, 2018 7:54 am

Hurrah It works. Thank you so much. I always had ":" inserted as the original code had shown.
OpenOffice 4.1.5 on Mac Sierra 10.13.1
det
 
Posts: 82
Joined: Fri Mar 31, 2017 8:24 pm


Return to OpenOffice Basic, Python, BeanShell, JavaScript

Who is online

Users browsing this forum: No registered users and 2 guests