[Solved] Macro to open a file

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
ollie
Posts: 33
Joined: Sat Mar 28, 2009 4:04 am

[Solved] Macro to open a file

Post by ollie »

Welcome beginner. What is your question or comment?
Please try to briefly and clearly tell us: What you want, What you tried, and What happened.
-----------------------------------------------------------------------------------------------------------

I want to create a macro that will open a specific file.
I have tried to record a macro but it stops working when I open the file. This is how it records but it does not specify the file or work

Code: Select all

sub OpenExp
rem ----------------------------------------------------------------------
rem define variables
dim document   as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

rem ----------------------------------------------------------------------
rem dispatcher.executeDispatch(document, ".uno:Open", "", 0, Array())
Can someone please help
Last edited by Hagar Delest on Thu Apr 02, 2009 8:22 am, edited 1 time in total.
Reason: tagged [Solved].
OOo 3.0.X on Ms Windows XP
FJCC
Moderator
Posts: 9274
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Macro

Post by FJCC »

Macro recording in OOo is rather limited. It works mostly for actions within a document (e.g. go to a Calc cell, copy it, go to another position, paste), but doesn't deal with dialogs such as opening a file. To do those things you have to work with the API directly. This code opens an OOo file. If you need to open a file in another format, such as a MS Office file, that can also be done, but you will have to tell us what type of file it is.

Code: Select all

REM edit the file name as needed
FileName = "C:\Documents and Settings\username\Desktop\TestFile.odt"
FileName = convertToURL(FileName)
Empty() = Array()
TestDoc = StarDesktop.loadComponentFromURL(FileName, "_blank", 0, Empty())
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.
ollie
Posts: 33
Joined: Sat Mar 28, 2009 4:04 am

Re: Macro

Post by ollie »

Thanks for the reply.
I have created the following macro
sub OpenExp
REM edit the file name as needed
FileName = "C:\Worldweb\Expenses\TestFile.odt"
FileName = convertToURL(Expenses Test)
Empty() = Array()
TestDoc = StarDesktop.loadComponentFromURL(Expenses Test, "_blank", 0, Empty())

end sub
File is a OOo spreadsheet and is named "Expenses Test". Dont want to open other types of files at this stage.

When I run the macro it gives the following error message "Basic Syntax error - Parenthese do not match" and it highlights "convertToURL(Expenses"

What am I doing wrong.
OOo 3.0.X on Ms Windows XP
FJCC
Moderator
Posts: 9274
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Macro

Post by FJCC »

I see how you got confused. My comment about editing the file name can be taken to refer to the variable FileName. The only thing you had to change was the file name path "C:\....". I hope this is clearer.

Code: Select all

    REM edit the file name as needed
    Target = "C:\Worldweb\Expenses\TestFile.odt"
    TargetURL = convertToURL(Target)
    Empty() = Array()
    TestDoc = StarDesktop.loadComponentFromURL(TargetURL, "_blank", 0, Empty())
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.
ollie
Posts: 33
Joined: Sat Mar 28, 2009 4:04 am

Re: Macro

Post by ollie »

Thank you very much

That is exactly what I was looking for
Problem Solved
Regards
Ollie
OOo 3.0.X on Ms Windows XP
wpe
Posts: 1
Joined: Mon Mar 13, 2023 4:36 pm

Re: [Solved] Macro to open a file

Post by wpe »

I had the error "Read-Only Property" at

Code: Select all

Empty() = Array()
when running the macro.
This worked for me:

Code: Select all

REM  *****  BASIC  *****

Sub Main

FileName = "/path/to/my/file.odt"
FileName = convertToURL(FileName)
Dim Empty() 'An (empty) array of PropertyValues
TestDoc = StarDesktop.loadComponentFromURL(FileName, "_blank", 0, Empty())

End Sub
I have founded the solution thanks to this thread: https://ask.libreoffice.org/t/loadcompo ... un/18090/2
Ubuntu 20.04
LibreOffice Version : 6.4.7.2
User avatar
Lupp
Volunteer
Posts: 3549
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: [Solved] Macro to open a file

Post by Lupp »

I don't remember how that was in 2009.
For a long time now in LibreOffice and in AOO as well Empty is one of two predefined variables I sometimes use. It represents an empty variable of base type Variant with actual type "Empty".
The other mentioned predefined Variable is Nothing which represents the Null object.
Both names can still be used for anything after an explicit Dim statement.

In the given case the variable Empty is simply unneeded. Create the empty Array you want to use as an argument as Array() directly on the respective position using the predefined function Array() which you also can use to create arrays in the sense of sequences with one or more elements (no 2D or higher dimensions). Array() has 0 elements, and Ubound(Array()) is -1.

Code: Select all

nextDocument = StarDesktop.loadComponentFromURL(FileURL, "_blank", 0, Array())
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
Post Reply