Ask Your Question

macro with if condition

asked 2020-02-18 11:00:56 +0100

fbertoldi gravatar image

updated 2020-02-18 12:51:18 +0100

I have an autoopen macro to go on a specif bookmark ("here") created whenever I save a writer document. I wonder if it could be possible to disable this macro if the odt file is open from another odt file (and not from Dolphin, i.g.). So I could open the target file at the point I want (a bookmark different from "here").


@Lupp: 1) autoopen: I mean that macro runs automatically when a odt is opened

2) this is the macro (stored, so to say, not in a file, but in LibreOffice: affecting all odt files)

sub vai_qui

If ThisComponent.supportsService("") Then

oBookmarks = ThisComponent.getBookmarks()
If NOT oBookmarks.hasByName("qui") Then
Exit Sub  
End If

ViewCursor = ThisComponent.CurrentController.getviewCursor()
Bookmark = ThisComponent.Bookmarks.getByName("qui").Anchor
ViewCursor.gotorange(Bookmark, False)

ViewCursor = ThisComponent.CurrentController.getviewCursor()
Bookmark = ThisComponent.Bookmarks.getByName("qui").Anchor
ViewCursor.gotorange(Bookmark, False)

   Exit Sub  
 End If
end sub

3) I'd like add a code, if possible, to disable this macro (in the target-document) if the new document is open (not from a specific file, but) from inside LibreOffice (/LibreWriter) and not from "outside" (i.g. Dolphin). Thank you

edit retag flag offensive close merge delete


The subject you chose seems misleading to me. Conditional statements in macros are surely well known.
In your case the question is how to branch in a macro called for an opening document anyway based on info about what/who had opened it.
I also don't know the term "autoopen macro" as something with a clear meaning.

Lupp gravatar imageLupp ( 2020-02-18 12:26:36 +0100 )edit

1 Answer

Sort by » oldest newest most voted

answered 2020-02-18 12:16:45 +0100

Lupp gravatar image

updated 2020-02-18 12:18:48 +0100

You did neiter tell where the "autoopen" macro is located and how it is called nor in what way the second document should be opened "from another odt file". Will there run user code? Is the document linked? If so ...
Please give the needed details next time! We may run in time-waste otherwise.

Assuming you want to call the second document by a macro running for another one:
The macro in charge of going to a specific bookmark may choose it based on information passed by the macro opening the documnt. I don't know a standardized way to pass caller info, but you can set the .Title property by the calling macro and evaluate it by the onViewCreated macro of the opened one. .Title neither is readonly nor is it saved with the document.

Example code lines:

doc2Url    = ConvertToUrl("full file path to my second document")
doc2       = StarDeskTop.loadComponentFromUrl(doc1Url, "_blank", 0, Array())
doc2.Title = doc2.Title & "?openedby=WizardMacro"
edit flag offensive delete link more


I have answered with an EDIT. Thanks

fbertoldi gravatar imagefbertoldi ( 2020-02-18 12:49:09 +0100 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2020-02-18 11:00:56 +0100

Seen: 156 times

Last updated: Feb 18 '20