Page 1 of 1

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

Posted: Thu Dec 04, 2008 5:39 am
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.

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

Posted: Thu Dec 04, 2008 8:17 am
by probe1
Your Propval is incomplete. You have to provide more information on the columns to import.

See example: insertCSV2Calc

Good luck!

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

Posted: Thu Dec 04, 2008 4:25 pm
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.

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

Posted: Thu Dec 04, 2008 10:31 pm
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.

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

Posted: Fri Dec 05, 2008 5:07 am
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!