[Solved] OOo Basic Macro to open a csv file

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
gigern
Posts: 3
Joined: Mon Feb 01, 2010 2:09 am

[Solved] OOo Basic Macro to open a csv file

Post by gigern »

I am migrating from MS Office to OO. To do that I will re-write my macros in the OO native language. I thought I could simply use the macro learn functions. It works for many tasks but not for opening a file. The code returned is preceded by REM and if I remove that I find there are no parameters recorded. I suspect there are at least 5 or so parameters needed.
I have spent most of today to look through the forum and have not found a solution.
My next step will be to open a CSV file from the internet. It works manually but I would like to incorporate it into a macro which automatically extracts relevant data and stores it in a calc sheet.
Hope someone can give me hints. Thanks
Nick
Last edited by gigern on Mon Feb 01, 2010 12:07 pm, edited 1 time in total.
OOO310m19, Windows 7
FJCC
Moderator
Posts: 9563
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: OO Basic Macro to open a csv file

Post by FJCC »

The macro recorder is not a good way to learn how to write macros. It is very limited in what it can do, as you have seen, and it uses a different method of controlling the OOo program than is typically used in human-written macros. Here is some code that opens a comma delimited file with a hard coded name.

Code: Select all

Sub CSVImport

Dim Propval(1) as New com.sun.star.beans.PropertyValue
Propval(0).Name = "FilterName"
Propval(0).Value = "Text - txt - csv (StarCalc)"
Propval(1).Name = "FilterOptions"
Propval(1).Value ="44,34,0,1,1"   'ASCII  44 = comma. See Developer's Guide Page 637
FileName = "C:\Documents and Settings\username\Desktop\Archivo.txt"  '
FileURL = convertToURL(FIleName)
oCSV = StarDesktop.loadComponentFromURL(FileURL, "_blank", 0, Propval())

End Sub
A good place to learn about writing macros is Andrew Pitonyak's English Macro Document.
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.
gigern
Posts: 3
Joined: Mon Feb 01, 2010 2:09 am

Re: OO Basic Macro to open a csv file

Post by gigern »

Thanks, that works! My remaining problem is that excel will open web pages which are not csv. When I try that with the macro the page opens in the writer instead. There is not likely a way around that, because if I try to open such a page with calc directly the program hangs. Yet if I open the page with Writer it opens it in as a table (the site is http://newsvote.bbc.co.uk/1/shared/fds/ ... efault.stm). In excel I can open it using a macro and then retrieve the values I am interested in to update my excel sheet.
Never the less I am impressed with the progress OO has made and would not be surprised if it would become the default standard in the not too distant future!
OOO310m19, Windows 7
Post Reply