[Solved] Import CSV from a .txt file with a macro

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
FJCC
Moderator
Posts: 9280
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

[Solved] Import CSV from a .txt file with a macro

Post by FJCC »

I want to import a text file (file name *.txt) with comma separated values using a macro. I import such files routinely using File -> Open and selecting the file type Text CSV (*.csv, *.txt, *.xls). In the macro I set the import filter as follows:

Propval(0).Name = "FilterName"
Propval(0).Value = "scalc: Text - txt - csv (StarCalc)"

and import using:

Doc = StarDesktop.loadComponentFromURL(Addr, "_blank", 0, Propval())

If the file has the .txt extension, this results in the file opening as a Writer document. If I change the file extension to .csv, the file opens correctly in Calc. Since the manual method works with the .txt extension, I'm hoping there is a way to force the file to open in Calc.
Last edited by FJCC on Fri Dec 05, 2008 5:08 am, edited 2 times in total.
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.
User avatar
probe1
Volunteer
Posts: 277
Joined: Mon Oct 08, 2007 1:34 am
Location: Chonburi Thailand

Re: Import CSV from a .txt file with a macro

Post by probe1 »

Your Propval is incomplete. You have to provide more information on the columns to import.

See example: insertCSV2Calc

Good luck!
Cheers
Winfried

DateTime2 extension: insert date, time or timestamp, formatted to your needs
FJCC
Moderator
Posts: 9280
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Import CSV from a .txt file with a macro

Post by FJCC »

I actually did have the FilterOptions set, I just failed to say that in my post. I've tried setting them in various ways, including exactly as shown in the document you suggested, and changing the field delimiter to different characters ( "," ";" etc.) without any luck.
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.
User avatar
probe1
Volunteer
Posts: 277
Joined: Mon Oct 08, 2007 1:34 am
Location: Chonburi Thailand

Re: Import CSV from a .txt file with a macro

Post by probe1 »

FJCC wrote:including exactly as shown in the document you suggested
Those filter settings apply to my test file, only!


You have to find the exact filter settings by:
* open the file manually, setting encoding and column type as wanted,
* then run the routine by Villeroy to have the filter settings displayed,
* use them in the macro.

If you have done so, please post the entire code, so somebody might help you.
Cheers
Winfried

DateTime2 extension: insert date, time or timestamp, formatted to your needs
FJCC
Moderator
Posts: 9280
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Import CSV from a .txt file with a macro

Post by FJCC »

I finally got it. The problem was not with the filter options but with the FilterName. The correct form is

Propval(0).Name = "FilterName"
Propval(0).Value = "Text - txt - csv (StarCalc)"

The form I used, Propval(0).Value = "scalc: Text - txt - csv (StarCalc)", works for .csv files but not .txt. I'm not sure where I got it from. The StarOffice 8 Programming Guide has a similar form that seems to have the same problem. Anyway, thanks to probe1 for the document reference. By peering at that I finally saw the absence of the "scalc:" in a properly functioning macro. I also got to dust off a bit of my college German. That really stretched my brain!
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.
Post Reply