[Solved] Import data from txt to Calc

Creating a macro - Writing a Script - Using the API

[Solved] Import data from txt to Calc

Postby babiniec4 » Thu Apr 28, 2011 4:26 pm

Hello. I need a macro to import selected data from txt file to the calc. I would like to import several different items from a txt file to the calc. Txt file is a list of some settings that are stored in one after another. One data position is one line. I need a macro that takes data from the selected file items such as 5-12, and pastes in A1: A8 calc. Thank you and please help.
Last edited by Hagar Delest on Thu Jun 02, 2011 12:27 am, edited 1 time in total.
Reason: tagged [Solved].
open officev3.2 on win Xp
babiniec4
 
Posts: 8
Joined: Wed Apr 27, 2011 11:40 pm

Re: Import data from txt to Calc

Postby JohnSUN-Pensioner » Thu Apr 28, 2011 9:32 pm

Just open txt-file:
Code: Select all   Expand viewCollapse view
   If NOT FileExists(sFileName) Then
      MsgBox("File " & sFileName & " not found!", 0, "Error")
      Exit Sub
   End If
REM Open them
   iNumber = Freefile
   Open sFileName For Input As iNumber

Then read line by line:
Code: Select all   Expand viewCollapse view
   While Not EOF(iNumber)
      Line Input #iNumber, sLine
'< some  actions >
   Wend
   Close #iNumber

and put content to cells
I may not have a lot to give but what I got I'll give to you...
Apache OpenOffice 4.1.4, LibreOffice 5.4.2.2 (x64) on Windows 7
If you think that I did not answer your question, make allowances for my imperfect English
User avatar
JohnSUN-Pensioner
Volunteer
 
Posts: 761
Joined: Fri Jan 14, 2011 1:21 pm
Location: Kyiv, Ukraine

Re: Import data from txt to Calc

Postby rudolfo » Fri Apr 29, 2011 11:03 am

babiniec4 wrote:Hello. I need a macro to import selected data from txt file to the calc. [... ]I need a macro that takes data from the selected file items such as 5-12, and pastes in A1: A8 calc.

You are saying selected items, but it is not fully clear what you mean with this. Is this meant in the typical sense of computer parlance as selected/highlighted with the mouse? Or does it simply mean "not all items, just some of them" and which will be specified later on by you when you run the macro?

If you mean selected by mouse it must be already opened in an application ... hopefully calc, but writer may be possible as well. In case of calc your task would rather be to apply a filter to your sheet and then copy the filtered data.

In general you have two approaches:
  • Import/process only the desired lines while reading the file (that's JohnSUN's method)
  • Read the complete file into Calc (Open as csv or text), click the appropriate settings in the Import filter dialog and finally in Calc delete the rows (and columns) that you don't need.
OpenOffice 3.1.1 (2.4.3 until October 2009) and LibreOffice 3.3.2 on Windows 2000, AOO 3.4.1 on Windows 7
There are several macro languages in OOo, but none of them is called Visual Basic or VB(A)! Please call it OOo Basic, Star Basic or simply Basic.
rudolfo
Volunteer
 
Posts: 1488
Joined: Wed Mar 19, 2008 11:34 am
Location: Germany

Re: Import data from txt to Calc

Postby babiniec4 » Wed Jun 01, 2011 11:27 pm

Thanks a lot. The code is working now. he he
open officev3.2 on win Xp
babiniec4
 
Posts: 8
Joined: Wed Apr 27, 2011 11:40 pm


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 7 guests