Page 1 of 1
[Solved] Macro to open a file
Posted: Wed Apr 01, 2009 7:07 am
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
Re: Macro
Posted: Wed Apr 01, 2009 2:05 pm
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())
Re: Macro
Posted: Wed Apr 01, 2009 3:11 pm
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.
Re: Macro
Posted: Wed Apr 01, 2009 3:37 pm
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())
Re: Macro
Posted: Thu Apr 02, 2009 3:34 am
by ollie
Thank you very much
That is exactly what I was looking for
Problem Solved
Regards
Ollie
Re: [Solved] Macro to open a file
Posted: Mon Mar 13, 2023 4:42 pm
by wpe
I had the error "
Read-Only Property" at
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
Re: [Solved] Macro to open a file
Posted: Mon Mar 13, 2023 11:05 pm
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())