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.
[Solved] Import CSV from a .txt file with a macro
[Solved] Import CSV from a .txt file with a macro
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.
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
Re: Import CSV from a .txt file with a macro
Your Propval is incomplete. You have to provide more information on the columns to import.
See example: insertCSV2Calc
Good luck!
See example: insertCSV2Calc
Good luck!
Re: Import CSV from a .txt file with a macro
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.
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
Re: Import CSV from a .txt file with a macro
Those filter settings apply to my test file, only!FJCC wrote:including exactly as shown in the document you suggested
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
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!
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.
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.