Ask Your Question

Control line endings with print statement

asked 2016-05-09 12:02:54 +0200

Moritz gravatar image

I have a Basic macro which writes the contents of a spreadsheet into a text file. This macro was developed on a LO installed on Windows. Now I switched to Linux and have a problem with the line endings in the generated file.

The file must have Windows line endings (\r\n or chr(13)+chr(10)), because it is used in other programs which insist on these line endings.

I tried a couple of approaches to save the \r\n line endings on a Linux installation:

sub ExportAreaToFile
  ' This prints the range "Sheet1.A1:C3" with two different methods into two different text files.

  dim oSheet as object
  dim datarangeAddress as object
  dim sCell as string
  dim sTab as string
  dim sLF as string
  dim i, j as long
  dim result as string

  sTab = chr(9)
  sLF = chr(13) + chr(10)

  ' Open file descriptors
  iNumA = FreeFile
  iNumB = FreeFile
  Open "output_A.txt" for Output as #iNumA
  Open "output_B.txt" for Output as #iNumB

  ' Get range
  oSheet = ThisComponent.Sheets.getByName("Sheet1")
  datarangeAddress = oSheet.getCellRangeByName("A1:C3").RangeAddress()

  ' Loop over our range
  for j = datarangeAddress.StartRow to datarangeAddress.EndRow
    for i = datarangeAddress.StartColumn to datarangeAddress.EndColumn
      sCell = oSheet.getCellByPosition(i, j).string
      print #iNumA, sCell; sTab;
      result = result + sCell + sTab
    next i
    print #iNumA, sLF
    result = result + sLF
  next j

  print #iNumB, result

  close #iNumA
  close #iNumB

Both alternatives do not put \r\n in the output files, despite explicitly adding these characters with print #iNumA, sLF or result = result + sLF.

How can I get this macro to print \r\n on a Linux system?

The same question was asked at but I got no answer. I believe it's a bug, which I filed under Bug #99650 and got the advice to try it here.

Thank you for your help!


edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2016-05-09 16:59:26 +0200

JohnSUN gravatar image

updated 2016-05-09 17:02:33 +0200

Use your "ALTERNATIVE B" but:

  1. Define result as Variant (not string)

    dim result as Variant

  2. Open file "for Binary"

    Open "~/output_B.txt" for Binary as #iNumB

  3. write data with PUT (not print or write)

    PUT #iNumB, , result

edit flag offensive delete link more


Thanks JohnSUN, That "workaround" solved it. I've no idea why I could not make it work when I tried it...

Moritz gravatar imageMoritz ( 2016-05-10 11:31:31 +0200 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2016-05-09 12:02:54 +0200

Seen: 56 times

Last updated: May 09 '16