[Solved] Macro to open a file

Creating a macro - Writing a Script - Using the API

[Solved] Macro to open a file

Postby ollie » Wed Apr 01, 2009 7:07 am

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   Expand viewCollapse view
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
ollie
 
Posts: 33
Joined: Sat Mar 28, 2009 4:04 am

Re: Macro

Postby FJCC » Wed Apr 01, 2009 2:05 pm

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   Expand viewCollapse view
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())
AOO 3.4 or 4.0 on MS Windows XP ( before 2013-08-03) or Windows 7
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
FJCC
Moderator
 
Posts: 3678
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Macro

Postby ollie » Wed Apr 01, 2009 3:11 pm

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
ollie
 
Posts: 33
Joined: Sat Mar 28, 2009 4:04 am

Re: Macro

Postby FJCC » Wed Apr 01, 2009 3:37 pm

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   Expand viewCollapse view
    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())
AOO 3.4 or 4.0 on MS Windows XP ( before 2013-08-03) or Windows 7
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
FJCC
Moderator
 
Posts: 3678
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Macro

Postby ollie » Thu Apr 02, 2009 3:34 am

Thank you very much

That is exactly what I was looking for
Problem Solved
Regards
Ollie
OOo 3.0.X on Ms Windows XP
ollie
 
Posts: 33
Joined: Sat Mar 28, 2009 4:04 am


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 7 guests