[Solved] Code for changing into another opened document

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
ptbento
Posts: 45
Joined: Fri Jan 18, 2013 9:33 pm

[Solved] Code for changing into another opened document

Post 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,
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
RPG
Volunteer
Posts: 2250
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: Macro code for changing into another opened document

Post 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
LibreOffice 7.1.4.2 on openSUSE Leap 15.2
ptbento
Posts: 45
Joined: Fri Jan 18, 2013 9:33 pm

Re: Macro code for changing into another opened document

Post 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)?
OpenOffice 3.4.1 on Windowx XP
RPG
Volunteer
Posts: 2250
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: Macro code for changing into another opened document

Post 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

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

Re: Macro code for changing into another opened document

Post by RPG »

Hallo

I have done a little test.

Romke

Code: Select all

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 7.1.4.2 on openSUSE Leap 15.2
ptbento
Posts: 45
Joined: Fri Jan 18, 2013 9:33 pm

Re: Macro code for changing into another opened document

Post 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)
OpenOffice 3.4.1 on Windowx XP
RPG
Volunteer
Posts: 2250
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: Macro code for changing into another opened document

Post 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
LibreOffice 7.1.4.2 on openSUSE Leap 15.2
User avatar
Charlie Young
Volunteer
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: Macro code for changing into another opened document

Post 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


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
ptbento
Posts: 45
Joined: Fri Jan 18, 2013 9:33 pm

Re: Macro code for changing into another opened document

Post 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...
OpenOffice 3.4.1 on Windowx XP
det
Posts: 86
Joined: Fri Mar 31, 2017 8:24 pm

Re: Macro code for changing into another opened document

Post 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 ... sktop[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
FJCC
Moderator
Posts: 9248
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Macro code for changing into another opened document

Post 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

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.
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
det
Posts: 86
Joined: Fri Mar 31, 2017 8:24 pm

Re: Macro code for changing into another opened document

Post 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??
OpenOffice 4.1.5 on Mac Sierra 10.13.1
FJCC
Moderator
Posts: 9248
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Macro code for changing into another opened document

Post 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

Url = convertToURL( "/Users/imac/Doc1.ods")
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
det
Posts: 86
Joined: Fri Mar 31, 2017 8:24 pm

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

Post by det »

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
Post Reply