' -------------------------------------------------------- ' A macro to save a copy of current file to a TXT file ' Author: Aleksander Adamowski ' ' Useful if you want to maintain an up-to-date copy of your file which is more ' convenient to read (that is, doesn't require launching OO which is slooow... ;) ). ' Just assign it to your document's "Document has been saved" event ' (Tools->Configure, Events tab) sub Store_to_txt ' define variables: Dim oDocument Dim urlTransformer ' with accordance to DevelopersGuide.pdf, page 135 ("Mapping of Structs"): Dim url As New com.sun.star.util.URL ' get access to the document: oDocument = ThisComponent ' create the URL transformer: urlTransformer = createUnoService("com.sun.star.util.URLTransformer") ' If the document is associated with a file resource then we do the job: if oDocument.hasLocation() then ' Load the associated file's URL into url variable: url.Complete = oDocument.getLocation ' Decompose the URL: urlTransformer.parseStrict(url) ' Now we've got the file's name in url.Name. ' we decompose it using "." as the separator ' (will need it to strip file extension later): Dim nameArray() as String nameArray = split(url.Name, ".") ' Prepare an array for name components excluding file extention: Dim nameArrayNoExt(LBound(nameArray) to UBound(nameArray) - 1) as String ' Fill the "no extension" array: For i = LBound(nameArray) to UBound(nameArray) - 1 nameArrayNoExt(i) = nameArray(i) Next i nameNoExt = join(nameArrayNoExt(), ".") ' Prepare the new name (with ".txt" extension): newName = nameNoExt & ".txt" url.Name = newName ' Assemble new URL: urlTransformer.assemble(url) ' store the document to that file: dim args1(2) as new com.sun.star.beans.PropertyValue args1(1).Name = "FilterName" ' For Calc use CSV export filter, for Writer use text export filter: if ThisComponent.getDocumentInfo().MIMEType = "application/vnd.sun.xml.calc" then args1(1).Value = "Text - txt - csv (StarCalc)" elseif ThisComponent.getDocumentInfo().MIMEType = "application/vnd.sun.xml.writer" then args1(1).Value = "Text" end if args1(2).Name = "InteractionHandler" oDocument.storeToURL(url.Complete, args1()) else msgbox("not saved to TXT because no URL yet (save the document once first)!") endif end sub