[Solved] Calc: Macro for 'Save a Copy'

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
muaz12
Posts: 2
Joined: Fri Mar 09, 2018 10:52 pm

[Solved] Calc: Macro for 'Save a Copy'

Post by muaz12 »

I am trying to write a macro to run the 'Save a Copy' command and save a copy of the active sheet as a csv file in the same directory as the workbook. I searched and found many examples on this forum but they were all for Save As, and not Save a Copy. I couldn't find a reference in the Programmer's Guide either. Hoping someone here can help me with that.
Last edited by muaz12 on Sat Mar 10, 2018 12:55 am, edited 1 time in total.
LibreOffice Version: 5.1.6.2 on Ubuntu 16.04
User avatar
Zizi64
Volunteer
Posts: 11353
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Calc: Macro for 'Save a Copy'

Post by Zizi64 »

You must study the API functions included the functions StoreAsURL() and the StoreToURL(). (...and the usage of the export filters)

https://wiki.openoffice.org/wiki/Saving_a_document
https://wiki.openoffice.org/wiki/API/Tu ... PDF_export
viewtopic.php?f=9&t=2551
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
muaz12
Posts: 2
Joined: Fri Mar 09, 2018 10:52 pm

Re: Calc: Macro for 'Save a Copy'

Post by muaz12 »

Thank you for the amazingly quick reply!

I got it working. Here is the code if anyone is interested:

Code: Select all

Sub Main
	dim document as object
	dim cell as object
	document = ThisComponent  'assigns the current document to the variable document
	Sheets = document.Sheets  'get the container of all Sheets
	Sheet = Sheets.getByName("data")   'get the sheet named data
	Controller = document.getcurrentController
	Sheet.IsVisible = True
	Controller.setActiveSheet(Sheet)
	
	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"
	Propval(1).Value ="44,34,0,1,1"   'Delimiters: ASCII  44 = ,  34 = "
	Doc = ThisComponent
	GlobalScope.BasicLibraries.loadLibrary("Tools")
    FileName = Tools.Strings.DirectoryNameoutofPath(ThisComponent.getURL(),"/") & "/schedule.csv"
	FileURL = convertToURL(FileName)
	Doc.storeToURL(FileURL, Propval())
	Sheet = Sheets.getByName("main")   'get the sheet named main
	Controller.setActiveSheet(Sheet)
End Sub
LibreOffice Version: 5.1.6.2 on Ubuntu 16.04
Post Reply