How make link in an odt close that odt before opening the link?

Imagine I have a single document open.
This document contains hyperlinks to other documents.
When I control-click on one of those hyperlinks, the new document is opened.
Now I have two documents open.

Instead I want the fist one to close (or attempt to, followed with option to save, as per normal operation); I want only the second document open and the first one closed.

How can I achieve this action?

This may be a bit dangerous - and you will need user code, imo.

-1- customize:
For the context menu applicable to Text best create a new Submenu and create therein items linking to related Sub.
-2- One of the Siub may then be something like the below example code placed in a modulke of the local ‘MyMacros’ Standard library.
Be sure to understand that the example is extremely crude and necessarily must be enriched by cecks and error catching and …

If everything done, you can select the item after a rightclick on your link, and hopefully it will not spoil everything.

Sub replaceCurrentDocument()
doc0 = ThisComponent
sel = doc0.CurrentSelection(0)
nextFile = sel.HyperlinkURL
doc0.Close(True)
doc1 = StarDesktop.loadComponentFromUrl(nextFile, "_blank", 0, Array())
End Sub

That’s interesting. Thanks for the ideas.

What do you mean spoil? What is the worst case? Crashing the app? Or worse??

If crashig the app actually is the worst case, I’m not sure. :wink:

I next to always try things I suggest myself, and when I did so in this case I actually got crashes. This at least as soon as I tried to assign the PosSize values from the original window to the new one. A working way to actually open the replacing document in the same frame as the original one I couldn’t find anyway (though it should be possiböle), and therefore I tried to create the illusion at least.

I’m afraid the related features of the API are rarely used, and their (old or new) bugs therefore not reported and consequently not fixed.

Thin ice! I cannot spend more time on this topic now - and I may have misunderstood related documentation. (And the setPosSize method actually is of StoneAge type.) Sorry!

I really appreciate the attempt! If I get an answer, I will post here.

@Lupp: I tested this with LO 6.4.3.2 on Windows and it worked without any problems. What version and OS did you use to get a crash?

doc1.getCurrentController().ComponentWindow.setPosSize(_
    30,30,400,300,com.sun.star.awt.PosSize.POSSIZE)

Mostly prereleases of V 7.0 and V 7.1, at least in one case also 6.4.4.2 (released) if I remember correctly.
“…their (old or new) bugs…” was hinting the narrow base of experience.
And the way .setPosSize gets the parameters is really archaic, imo.
My report may have a bias. However a warning should be appropriate.
Did you find a way to sucecessfully load the next document into the “_self” frame? I could only create the illusion in a few cases exactly with the help of the mentioned method. …
Wow! Just had another look into LibreOffice: XComponentLoader Interface Reference and found : "_self", ""(!) means frame himself. Never had found the special hint before. May it mean that the search flag (0 e.g.) must be replaced with an empty string? One day I will have another test.

@jimk: Wasted again some time (with 7.1 Dev alpha0 now). No crash, but trying to load with “_self” is ignored, and trying to use the current frame the way you described, the nextDoc is loaded, but into a new frame with empty name…

Same effect with V6.2.5 portable
Exact code:

Sub changeFrameContentFromFileHyperlink()
link = ThisComponent.CurrentSelection(0).HyperlinkURL
REM The link is actually a valid file link, and it works.
REM However, I never get the file into the correct target frame. 
REM The result is always as if I used "_blank".
myFrameName = "myTestFrame"
ThisComponent.CurrentController.Frame.Name = myFrameName
Print ThisComponent.CurrentController.Frame.Name
nextDoc = StarDesktop.loadComponentFromUrl(link, myFrameName, 2, Array()) ', myFrameName
Print nextDoc.CurrentController.Frame.Name
End Sub

@Lupp: Downloaded 7.0.0.2 from Index of /pre-releases/win/x86_64/, which was just added yesterday. So presumably by “7.1 Dev alpha0” you mean 7.0.0.1. Although I could not find any mention of “alpha0” so maybe I’m mistaken.

Anyway, I found the same results as before. The code in my answer loads the new document in the current frame with no additional frames. Both 23 ALL by itself and 23+8 ALL+CREATE work.

The code in your last comment, however, does not work, because it uses only the flag 2 SELF. Nor could I get "_self" or "" to work as frame names. Those attempts either resulted in additional frames or did not load the new document.

Thanks for your continuing interest.
Here it’s about 02:00 local time. I will have a nap now.
The version I mentioned was a daily Dev build I had downloaded a few days ago to verify a bugfix. It’s named 7.1.0.0alpha0+ (x64).
Read you later!

[ edit: Thanks everyone. The suggestions may work, but are too complex for my skill level in consideration with my level of need ]

I am thinking maybe the reason why the answer is hard to find is because to do what I am asking, there is a better way completely different. What I want is kind of like having a website local to my personal computer, with pages that can easily be edited. So maybe that is the direction to go?

Always tell what you actually want to achieve.
However, I have no clue how to do it - or I didn’t understand you clearly enough.

I am using LIbreOffice Write and had no intention to change. Browsers default that action of replacing the current page after clicking a link in that page. If LibreOIffice doesn’t, probably there is a good reason for that. One good reason could be that some other type of application is better suited (not just another word processor but a totally different application). So I am thinking what would that be?

Instead of closing the current frame, replace it by specifying a name. Set up this routine as explained in @Lupp’s answer.

Sub replaceCurrentDocument
	oDoc = ThisComponent
	oDoc.getCurrentController().getFrame().setName("MyFrame")
	sel = oDoc.CurrentSelection(0)
	sUrl = sel.HyperlinkURL
	SearchFlags = com.sun.star.frame.FrameSearchFlag.CREATE + _
	              com.sun.star.frame.FrameSearchFlag.ALL
	oDoc = StarDesktop.loadComponentFromURL(sUrl, "MyFrame", SearchFlags, Array())
End Sub

This is based on the section at The StarDesktop - Apache OpenOffice Wiki entitled “Replacing the Content of the Document Window.” The example code given there did not work when I tried it, but apparently the idea still works.