Sub ExportAllToCsv
	document = ThisComponent

	' Use the global string tools library to generate a base filename for each CSV
	' based on the current prefixless filename
	GlobalScope.BasicLibraries.loadLibrary("Tools")
	OrigURL = document.getURL()
	BaseFilename = Tools.Strings.GetFileNameWithoutExtension(OrigURL, "/")
	DocDir = Tools.Strings.DirectoryNameOutOfPath(OrigURL, "/") + "/"

	' Work out number of sheets for looping over them later.
	Sheets = document.Sheets
	NumSheets = Sheets.Count - 1

	' Set up a propval object to store the filter properties
	Dim Propval(1) as New com.sun.star.beans.PropertyValue
	Propval(0).Name = "FilterName"
	Propval(0).Value = "Text - txt - csv (StarCalc)"
	Propval(1).Name = "FilterOptions"
	' The third number specifies the output encoding; 76 is utf8:
	Propval(1).Value ="59,34,76,1,1"   'ASCII  59 = ;  34 = "

	For I = 0 to NumSheets
		' For each sheet, assemble a filename and save using the filter
		SheetName = Sheets(I).Name
		document.getCurrentController.setActiveSheet(Sheets(I))
		Filename = DocDir + BaseFilename + "." + SheetName + ".csv"
		FileURL = convertToURL(Filename)
		document.StoreAsURL(FileURL, Propval())
	Next I

	' Libreoffice thinks our filename is now the last-exported-CSV filename, so close.
	Msgbox "Files saved as " + DocDir + BaseFilename + ".*.csv. You'll need to close the spreadsheet now."
End Sub
